5

I have the following function that returns me the current location of device:

void getCurrentLocation()
{
    Location myLocation  = map.getMyLocation();
    if(myLocation!=null)
    {
        double dLatitude = myLocation.getLatitude();
        double dLongitude = myLocation.getLongitude();
        map.addMarker(new MarkerOptions().position(new LatLng(dLatitude, dLongitude))
                .title("My Location").icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_RED)));
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(dLatitude, dLongitude), 8));

    }
    else
    {
        Toast.makeText(this, "Unable to fetch the current location", Toast.LENGTH_SHORT).show();
    }
}

but some methods are showing in red like it were undefined: this

As you can notice, these methods are related with map, it work in onMapReady() function but out of it show it unrecognized. Why is that? What libraries I have to add? I declare map like this:

private MapFragment map;
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
Rene Limon
  • 614
  • 4
  • 16
  • 34

2 Answers2

6

Here is what your general code structure should look like. The important part is to assign your local map reference to the one returned in the onMapReady() callback.

public class MainActivity extends Activity 
        implements OnMapReadyCallback {

    private GoogleMap map;

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

        MapFragment mapFragment = (MapFragment) getFragmentManager()
                .findFragmentById(R.id.map);

        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap retMap) {

        map = retMap;

        setUpMap();

    }

    public void setUpMap(){

        map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        map.setMyLocationEnabled(true);
    }

    void getCurrentLocation()
    {
        Location myLocation  = map.getMyLocation();
        if(myLocation!=null)
        {
            double dLatitude = myLocation.getLatitude();
            double dLongitude = myLocation.getLongitude();
            map.addMarker(new MarkerOptions().position(new LatLng(dLatitude, dLongitude))
                    .title("My Location").icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_RED)));
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(dLatitude, dLongitude), 8));

        }
        else
        {
            Toast.makeText(this, "Unable to fetch the current location", Toast.LENGTH_SHORT).show();
        }
    }

}
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • Where I can put `getCurrentLocation()` ? – Muhammad Shahzad Dec 19 '15 at 08:53
  • @MuhammadShahzad This way of getting location is deprecated, if you want to get the current location for a map, take a look here: http://stackoverflow.com/questions/30253123/blue-dot-and-circle-is-not-shown-on-mylocation-using-android-fused-location-api – Daniel Nugent Dec 19 '15 at 17:13
  • Thank you, I'm unable to get current location in android studio, emulator 5554, I used `geo fix xxx xxxx`, all other possible ways but failed. – Muhammad Shahzad Dec 19 '15 at 17:16
  • @MuhammadShahzad It sounds like you've seen this: http://stackoverflow.com/questions/2279647/how-to-emulate-gps-location-in-the-android-emulator I would recommend using a real device for testing location based code..... – Daniel Nugent Dec 19 '15 at 17:19
1

Why are you using

 private MapFragment map;

Your map should be of type

com.google.android.gms.maps.GoogleMap

Just change

private MapFragment map;

to

private GoogleMap map;

and get the map like the following:

map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();

It will work fine.

Karthik
  • 4,950
  • 6
  • 35
  • 65
  • what´s the way to declarate google map? i was using map = (MapFragment) getFragmentManager().findFragmentById(R.id.map); to asign this but I change to Google map and give me an error because are different types – Rene Limon Jul 22 '15 at 18:46
  • @ReneLimon Updated the answer. Check the last line. – Karthik Jul 22 '15 at 18:52
  • one last question, getMap is not deprecated? – Rene Limon Jul 22 '15 at 18:55
  • @ReneLimon Yeah sorry, It's deprecated. Use `getMapAsync()` and `onMapReady()` returns the map object and It's guaranteed to be `not null`. – Karthik Jul 22 '15 at 19:14
  • @karthik actually, it can´t be onMapAsync because are diferent types I following Daniel Nugent steps – Rene Limon Jul 22 '15 at 19:35