33

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..

Shudy
  • 7,806
  • 19
  • 63
  • 98

6 Answers6

57

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 Documentation
setMyLocationButtonEnabled Documentation

jnthnjns
  • 8,962
  • 4
  • 42
  • 65
  • 3
    REallly!!! Oh god.. facepalm! Lot of thanks... it works perfect know. Didn't see this MyLocationEnale. Again, Thanks!!! – Shudy Apr 22 '13 at 15:17
  • 1
    Happens to the best of us from time to time :). Glad I could help. Best of luck! – jnthnjns Apr 22 '13 at 15:20
  • 1
    In my case, adding setMyLocationEnabled(true) does not help, unless at the same time location is fetched / listened to by my code. Somehow this triggers the blue dot to appear on the map. – Yar Jul 12 '14 at 10:56
3

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);
Mina Fawzy
  • 20,852
  • 17
  • 133
  • 156
2

Add the following line.

Kotlin:

map.isMyLocationEnabled = true

map is a GoogleMap object.

(More detailed answer(s) in this question: https://stackoverflow.com/a/15142091/11211963 )

Marci
  • 899
  • 10
  • 13
0

For me, map.setMyLocationEnabled(true);
It worked fine for below marshmallow devices and above marshmallow devices, I gave Location permission manually in-app settings. Later My Location Button got visible.

creativecoder
  • 1,470
  • 1
  • 14
  • 23
0

The above answers didn't work.

override fun onMapReady(googleMap: GoogleMap?) {
    this.googleMap = googleMap

    setupMap()
}

private fun setupMap() {
    if (ActivityCompat.checkSelfPermission(context!!,
            Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        // Before enabling the My Location layer, you must request location permission from the user.
        googleMap?.isMyLocationEnabled = true
        // *** Use this method ***
        googleMap?.uiSettings?.isMyLocationButtonEnabled = true

        // See https://developers.google.com/maps/documentation/android-sdk/location
        googleMap?.setOnMyLocationButtonClickListener(this)
        googleMap?.setOnMyLocationClickListener(this)
    } else {
        // Show rationale and request permission.
        requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
            LOCATION_REQUEST_CODE)
    }
}
CoolMind
  • 26,736
  • 15
  • 188
  • 224
0

You need permission to make it visible

if (ActivityCompat.checkSelfPermission(Maps_Activity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(Maps_Activity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

   requestPermission();

   return;
}

mMap.setMyLocationEnabled(true);

    
Rohaitas Tanoli
  • 624
  • 4
  • 16