我看了很多关于它的讨论,但我仍然没有在位置管理器的Android应用程序中显示gps坐标:
如果我理解得很好,我必须先做:
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
向位置管理员收费,之后
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) this);
更新位置 最后
Location position = locationManager.getLastKnownLocation( LocationManager.GPS_PROVIDER);
问题是这不起作用。所以我想我不太了解。请告诉我有什么好的语法。
提前感谢您的帮助!
编辑:
我在清单
中添加了这些权限 <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.location.network" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-feature android:name="android.hardware.wifi" />
答案 0 :(得分:0)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtLat = (TextView) findViewById(R.id.textview1);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
@Override
public void onLocationChanged(Location location) {
txtLat = (TextView) findViewById(R.id.textview1);
txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());
}
@Override
public void onProviderDisabled(String provider) {
Log.d("Latitude","disable");
}
@Override
public void onProviderEnabled(String provider) {
Log.d("Latitude","enable");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Latitude","status");
}
答案 1 :(得分:0)
Code snippet for getting location.
LocationManager locationManager;
locationManager = (LocationManager) getSystemService(context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;// and ask for run time permission
}
Location currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (currentLocation != null) {
mypoint = new GeoPoint(currentLocation.getLatitude(), currentLocation.getLongitude());
}
markerForCurrentLocation.setPosition(mypoint);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 1, this);
@Override
public void onLocationChanged(Location location) {
//Here you will get the current location of your GPS device
mypoint = new GeoPoint(location.getLatitude(), location.getLongitude());
markerForCurrentLocation.setPosition(mypoint);
mapController.animateTo(mypoint);
}
Method asking for runtime location permission
private void requestLocationPermission() {
//method asking for runtime location permission
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_CODE);
}
}