Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
112 views
in Technique[技术] by (71.8m points)

java - Maps doesn't show the current location anymore

I started developing an app using google maps and it worked just fine. The map showed the current device location. Then I tried changing the map type and it worked...kinda.. The device's location doesn't show up anymore. So I deleted the line changing the map type. But the current location still isn't visible. Does anybody know why?

public class Map extends FragmentActivity implements OnMapReadyCallback {

    GoogleMap map;
    CameraPosition cameraPosition;

    private Location lastKnownLocation;


    private boolean locationPermissionGranted;
    private static final int PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;
    private static final String TAG = "--LOG--";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        getLocationPermission();




    }


    private void getLocationPermission(){

        if (ContextCompat.checkSelfPermission(this.getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
        { locationPermissionGranted = true;  }

        else {
            //Wenn die Berechtigung nicht erteilt wurde, wird nachgefragt
            ActivityCompat.requestPermissions(this,
                    new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                    PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION); }


    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        locationPermissionGranted = false;
        switch (requestCode) {
            case PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    locationPermissionGranted = true;
                }
            }
        }
        updateDeviceLocation();
    }



    private void updateDeviceLocation() {

        if (map == null) {
            return;
        }
        try {
            if (locationPermissionGranted) {
                //wenn standortzugriff erlaubt ist, wird der standort  genutzt & angezeigt
                map.setMyLocationEnabled(true);
                map.getUiSettings().setMyLocationButtonEnabled(true);}

            else {
                // wenn die Berechtigung verweigert wurde eine erneute Anfrage
                map.setMyLocationEnabled(false);
                map.getUiSettings().setMyLocationButtonEnabled(false);
                lastKnownLocation = null;
                getLocationPermission(); }
        }

        catch (SecurityException e)  {
            Log.e("Exception:  %s", e.getMessage());
        }
    }



    @Override
    public void onMapReady(GoogleMap googleMap) {
    }
}
``
question from:https://stackoverflow.com/questions/65643821/maps-doesnt-show-the-current-location-anymore

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You define map however you never set a value to it.

Try this:

@Override
public void onMapReady(GoogleMap googleMap) {
    map = googleMap;
}

Now when the map is ready, the library will run this function and googleMap is a non-null Map object accordings to the docs so then everything in your updateDeviceLocation() function should work now.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...