【发布时间】:2017-10-13 18:34:30
【问题描述】:
我正在尝试获取用户的当前位置并在 MAP 上显示一个标记。这是我的代码
public class MapsActivity extends AppCompatActivity implements LocationListener, OnMapReadyCallback {
private GoogleMap mMap;
LocationManager locationManager;
String provider;
Location mLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
provider = locationManager.getBestProvider(new Criteria(), false);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mLocation=locationManager.getLastKnownLocation(provider);
if (mLocation != null) {
Log.i("Location Status: ", "Location Found");
} else if (mLocation == null) {
Log.i("Location Status: ", "Location Not Found");
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if(mLocation!=null) {
try {
Double lat = mLocation.getLatitude();
Double lng = mLocation.getLongitude();
mMap.addMarker(new MarkerOptions().position(new LatLng(lat, lng)).title("My Location"));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 12));
} catch (Exception ex) {
Log.e("Location Error:","Exception was "+ex);
}
}
}
@Override
protected void onResume() {
super.onResume();
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
@Override
public void onLocationChanged(Location location) {
mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("My Location"));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 12));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(),"Please enable your location",Toast.LENGTH_LONG).show();
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
}
现在,问题是大多数时候我没有得到任何位置,地图显示没有任何标记,并且在日志中显示“位置状态:未找到位置”,如果位置是,则设置为显示空值。 我试图重建并再次运行。现在在一台设备上检测到该位置,但在其他设备上显示相同的尴尬输出。我希望它每次都能正常工作,但它的行为很奇怪,因为相同的代码在相同的设备上以相同的设置在不同的时间显示两种不同的行为。 附言在设备上启用位置。 任何形式的帮助或提示将不胜感激。谢谢!
【问题讨论】:
标签: java android google-maps google-maps-api-2