Google地图不会显示标记

问题描述 投票:1回答:1

我想在谷歌地图中添加一些标记。首先,我在设备的当前位置放置了一个标记。它工作正常。但是当我想添加另一个标记时,它不会显示任何标记。这是我试过的代码:

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();
}

请帮帮我

android google-maps
1个回答
0
投票

它会在您启动应用时显示标记,因为您在代码中提供了精确的lat和lng。它没有显示任何新标记的原因可能是您在使用应用程序时未启用位置服务或WiFi。尝试在您的设备上执行此操作,并让我知道它是否有效。

© www.soinside.com 2019 - 2024. All rights reserved.