I'm doing an app, with google maps, but when I try to add "my-location" button, as the reference says doesn't work...
thats how I do:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); locManager = (LocationManager)getSystemService(LOCATION_SERVICE); providersList = locManager.getAllProviders(); provider =locManager.getProvider(providersList.get(0)); precision = provider.getAccuracy(); req = new Criteria(); req.setAccuracy(Criteria.ACCURACY_FINE); inside = false; map.getUiSettings().setMyLocationButtonEnabled(true); buildPolygon(); drawPolygon(); startLocalization(); }
I used map.getUiSettings().setMyLocationButtonEnabled(true);
as shows in the reference of google. I don't know what's going on..
There are two ways to get the current location of any Android device: Android's Location Manager API. Fused Location Provider: Google Play Services Location APIs.
Open your phone's Settings app. Under "Personal," tap Location access. At the top of the screen, turn Access to my location on or off.
The myLocationButtonEnabled
is true by default and shown when the setMyLocationEnabled
layer is enabled.
try this:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); // Add this line map.setMyLocationEnabled(true); locManager = (LocationManager)getSystemService(LOCATION_SERVICE); providersList = locManager.getAllProviders(); provider =locManager.getProvider(providersList.get(0)); precision = provider.getAccuracy(); req = new Criteria(); req.setAccuracy(Criteria.ACCURACY_FINE); inside = false; //map.getUiSettings().setMyLocationButtonEnabled(true); buildPolygon(); drawPolygon(); startLocalization(); }
setMyLocationEnabled
DocumentationsetMyLocationButtonEnabled
Documentation
just add map.setMyLocationEnabled(true);
after create your map
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
to be like this
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); map.setMyLocationEnabled(true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With