【发布时间】:2012-08-05 09:48:48
【问题描述】:
我编写了用于查找某个地方的纬度和经度的代码,它在我使用 Eclipse 的 PC 上运行良好,但在我的 Android 手机上却无法运行。
public class MainActivity extends Activity {
protected LocationManager locationManager;
protected Button retrieveLocationButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1,
1000, new MyLocationListener());
retrieveLocationButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showCurrentLocation();
}
});
}
protected void showCurrentLocation() {
Location location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude());
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG)
.show();
}
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude());
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG)
.show();
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(MainActivity.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(MainActivity.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(MainActivity.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
}
}
【问题讨论】:
-
你能给我们多一点继续吗?代码等
-
每当你提到你写了一些代码时,通过发布一些相关的代码让我们知道你写了什么。
-
如果您正在使用设备,请确保您的设备的 GPS 以及网络位置已打开,因为有时获取 GPS 需要一些时间,直到它只会返回 0.0。
-
@AdeelPervaiz 是的,我打开了 GPS。如何查看网络位置是启用还是禁用?
-
通过适当的解决方案查看此答案 [stackoverflow.com/questions/11720785/… [1]: stackoverflow.com/questions/11720785/…
标签: android gps locationmanager android-location locationlistener