我想显示我的当前位置的经度和纬度.. 为此,而不是在Google中搜索,我搜索了SO。在获取当前位置的Android中的问题
我只想显示当前位置的经纬度。
见下面我的代码:
public class LocationGetter extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
LocationManager mlocManager =
(LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new LocationManagerHelper();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 100,mlocListener);
if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
tv.append("Latitude:- " + LocationManagerHelper.getLatitude()
+ '\n');
tv.append("Longitude:- " + LocationManagerHelper.getLongitude()
+ '\n');
Log.i("MyLocation1",Double.toString(LocationManagerHelper.getLatitude())+" "+Double.toString(LocationManagerHelper.getLongitude()));
} else {
tv.setText("GPS is not turned on...");
}
/** set the content view to the TextView */
setContentView(tv);
}
/* Class My Location Listener */
public static class LocationManagerHelper implements LocationListener {
private static double latitude;
private static double longitude;
@Override
public void onLocationChanged(Location loc) {
latitude = loc.getLatitude();
longitude = loc.getLongitude();
Log.i("MyLocation",Double.toString(latitude)+" "+Double.toString(longitude));
}
@Override
public void onProviderDisabled(String provider) { }
@Override
public void onProviderEnabled(String provider) { }
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
public static double getLatitude() {
return latitude;
}
public static double getLongitude() {
return longitude;
}
}
}
我也清单文件添加权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
看到输出我得到:
其中i我错了吗?我不明白,它是一个简单的代码,为什么它不工作?
您是在模拟器上输入“gpx”数据还是在设备上启用了GPS? – Swaroop 2011-04-07 09:45:24
我在模拟器上使用它。我也试过这个:telnet localhost 5554,然后geo fix 12 72 ..它不工作。我哪里错了? – 2011-04-07 09:47:26
您是否尝试过使用Eclipse ADT的DDMS仿真器控件?手动修复一个点? – Swaroop 2011-04-07 10:04:20