应用程序应该获取我当前的位置并在地图上标记,而不是我的项目只是崩溃。这是代码:
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
}
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location myLocation = locationManager.getLastKnownLocation(provider);
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
double latitude = myLocation.getLatitude();
double longitude = myLocation.getLongitude();
LatLng Me = new LatLng(latitude, longitude);
mMap.moveCamera(CameraUpdateFactory.newLatLng(Me));
mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!"));
}
任何想法如何修复它,或者这里有什么问题? (它的GoogleMaps项目,所有需要的权限都被授予了btw)
答案 0 :(得分:0)
您可以使用LocationManager:
locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude=location.getLatitude();
longitude=location.getLongitude();
Log.d("GPS","lat : "+latitude);
Log.d("GPS","long : "+longitude);
}
然后,您只需在this教程中显示地图中的位置即可。
我希望它有所帮助!