我想在谷歌地图中添加一些标记。首先,我在设备的当前位置放置了一个标记。它工作正常。但是当我想添加另一个标记时,它不会显示任何标记。这是我试过的代码:
if(isLocationEnabled(this)){
if (mLocationPermissionGranted) {
getDeviceLocatoin();
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) {
return;
}
mMap.setMyLocationEnabled(true);
LatLng location = new LatLng(23.742801, 90.432196);
addMarker(location,"Bashabo");
}else {
getLocationPermission();
}
}
这是getDeviceLocation()方法:
private void getDeviceLocatoin(){
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
try{
if(mLocationPermissionGranted){
Task location = mFusedLocationProviderClient.getLastLocation();
location.addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if(task.isSuccessful()){
Log.d(TAG,"onComplege : Found Location");
Location currentLocation = (Location)task.getResult();
mMap.addMarker(new MarkerOptions().position(new LatLng(currentLocation.getLatitude(),currentLocation.getLongitude()))
.title("Marker in current location")).showInfoWindow();
moveCamera(new LatLng(currentLocation.getLatitude(),currentLocation.getLongitude()),13);
Log.d(TAG,"onComplege : Current Location is null");
}
}
});
}
}catch (SecurityException e){
Log.e(TAG, "getDeviceLocation : Security Exception" + e.getMessage());
}
}
这是addMarker()方法:
private void addMarker(LatLng position, String title){
MarkerOptions location = new MarkerOptions();
location.position(position);
location.title(title);
mMap.addMarker(location).showInfoWindow();
}
请帮帮我
答案 0 :(得分:0)
它会在您启动应用时显示标记,因为您在代码中提供了精确的lat和lng。它没有显示任何新标记的原因可能是您在使用应用时无法启用位置服务或无线网络。尝试在您的设备上进行操作,并告诉我它是否有效。