当前位置未显示在摩托罗拉设备的地图上

时间:2019-11-29 16:41:22

标签: android google-maps

我在我的应用程序中使用google maps,代码可以正常工作并显示当前位置,我不知道为什么我的代码可以工作,但只能在三星设备中使用,而不能在摩托罗拉...我不知道我是否错过了一些许可对于其他设备..  如果有人可以帮我一些忙..那将是非常好的 这是我的代码

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);      
    getLocationPermission();
    initMap();
}
private void getLocationPermission(){
    String[] permission ={
            Manifest.permission.ACCESS_COARSE_LOCATION};
    if (ContextCompat.checkSelfPermission(this.getApplicationContext(),Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED){
        if(ContextCompat.checkSelfPermission(this.getApplicationContext(),
                Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
            mLocationPermissionsGranted = true;
            getDeviceLocation();
        }else{
            ActivityCompat.requestPermissions(this,permission,
                    LOCATION_PERMISSION_REQUEST_CODE);
        }
    }else{
        ActivityCompat.requestPermissions(this,
                permission,
                LOCATION_PERMISSION_REQUEST_CODE);
    }
}
private void initMap() {
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync((OnMapReadyCallback) this);
}
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    if (mLocationPermissionsGranted) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        mMap.setMyLocationEnabled(true);        
    }     
}
private void getDeviceLocation(){
    Log.d("MapsActivity.this", "getDeviceLocation: getting the devices current location");

    mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);

    try{
        if(mLocationPermissionsGranted){

            final Task location = mFusedLocationProviderClient.getLastLocation();
            location.addOnCompleteListener(new OnCompleteListener() {
                @Override
                public void onComplete(@NonNull Task task) {
                    if(task.isSuccessful()){
                        Log.d("MapsActivity.this", "onComplete: found location!");
                        Location currentLocation = (Location) task.getResult();

                        moveCamera(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()),
                                DEFAULT_ZOOM,"My location");

                    }else{
                        Log.d("MapsActivity.this", "onComplete: current location is null");
                        Toast.makeText(MainActivity.this, "unable to get current location", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }catch (SecurityException e){
        Log.e("MapsActivity.this", "getDeviceLocation: SecurityException: " + e.getMessage() );
    }
}

1 个答案:

答案 0 :(得分:0)

我刚刚更改了设备的配置,这三个设备之一必须工作正常