【发布时间】:2014-09-27 00:49:14
【问题描述】:
我想显示我当前位置的纬度和经度.. 为此,我没有在 Google 中搜索,而是在 SO 中搜索。
我只想显示我当前所在位置的经纬度。
请参阅下面的代码:
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>
查看我得到的输出:
我哪里出错了?我不明白,它是一个简单的代码,为什么它不起作用?
【问题讨论】:
-
您是在模拟器上输入“gpx”数据还是在设备上启用了 GPS?
-
我在模拟器上使用它。我也试过这个:telnet localhost 5554 然后geo fix 12 72 ..它不工作。我哪里错了?
-
您是否也尝试使用 Eclipse ADT 的 DDMS Emulator 控件?手动修复一个点?
-
没有。我不知道那件事。你能告诉我这件事吗?如何解决一个点?
-
如果您安装了 Eclipse ADT 插件,请查看developer.android.com/guide/developing/debugging/ddms.html
标签: android android-layout gps android-maps