您好,我一直在尝试从书中的一个示例中运行此代码,但我得到的只是传递给变量的空值,因此我只能得到这样的消息:“您的当前位置是:找不到位置
清单文件如下所示
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
main.xml文件是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/myLocationText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>最后是MainActivity.java
package com.snooze.android.geopositioning;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
}
public void updateWithNewLocation(Location location)
{
String latLongString;
TextView myLocationText;
myLocationText = (TextView)findViewById(R.id.myLocationText);
if (location != null)
{
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + String.valueOf(lat) + "\nLong:" + String.valueOf(lng);
}
else
{
latLongString = "No location found";
}
myLocationText.setText("Your Current Position is:\n" + latLongString);
}
}这是我的第一个项目,所以我对其中的一些工作原理并不熟悉,但是我按照书中所说的那样复制了所有的东西,但它不起作用。在许多网站上尝试了各种方法,以及从这个forum....but得到的答案都无济于事。我认为坐标没有传递给变量。请帮帮忙
发布于 2011-04-25 02:29:33
您需要添加android.permission.ACCESS_FINE_LOCATION或android.permission.ACCESS_COARSE_LOCATION权限才能在清单文件中使用GPS
发布于 2011-04-25 03:43:54
试试来自commonsguy的这个,它很容易实现/定制,而且工作起来很有魅力;
发布于 2011-04-25 02:23:03
你应该在你的代码中使用缩进和段落,使它更具可读性。
此外,为了显示地图,有一个“MapActivity”,它已经为你提供了一些功能(比如缩放)。
要做到这一点,你还需要Android Manifest中的“Google API”库,以及互联网权限(以获取卡片)。你应该阅读this tutorial,了解如何正确地做所有这些事情。
https://stackoverflow.com/questions/5772230
复制相似问题