Location Button doesn't work GoogleMaps v2

此生再无相见时 提交于 2019-12-24 13:02:57

问题


I'm trying to get my current location with the location button on Google Maps but I have some problems. With the new runtime permissions (API level 23), I don't know if i am doing well or not. With that code below, when I use a virtual device API 23, the location button doesn't appear and with a virtual device API 22, it appears but don't do anything. So what can be the problem ?

    public class MapsActivity extends FragmentActivity implements
        OnMapReadyCallback{

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

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

    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
        mMap.setMyLocationEnabled(true);
    } else {
        // Show rationale and request permission.
    }

}

}

In the Manifest file :

<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

Thanks in advance..


回答1:


In api level 23, you need check permissions programmatically. You are checking the location permission and if it's granted, setting the location enabled. You can ask for permission if it is not granted.

if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
        == PackageManager.PERMISSION_GRANTED) {
    mMap.setMyLocationEnabled(true);
} else {
    ActivityCompat.requestPermissions(MapActivity.this,
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_CODE);
}

Then you can handle the result by overriding this method.

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case REQUEST_CODE: {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //do something
            } else {
                finish();
            }
            break;
        }
    }
}

Also you can give permission from the emulator's settings for your application.

Let me know after trying. Good luck.

EDIT: You need to implement a clicklistener to mylocation button.

final GoogleMap mMap = map; 
map.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
        @Override
        public boolean onMyLocationButtonClick() {
            LatLng loc = new LatLng(mMap.getMyLocation().getLatitude(),mMap.getMyLocation().getLongitude());
            mMap.moveCamera(CameraUpdateFactory.newLatLng(loc));
            return true;
        }
    });


来源:https://stackoverflow.com/questions/35348604/location-button-doesnt-work-googlemaps-v2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!