0

Hi I need some help with this, I have already looked and try many different things. I'm trying to get my current location on android but since the location comes back null my app crashes. I really need help with this please.

I don't know if I'm being clear in here but everytime I call getLastKnownlocation it comes back null, so when I try to get the double lat= location.getLatitude() same for the longitude it won't return anything and there is where my app crashes.

Help... Here a piece of code

mMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();

String provider = locationManager.getBestProvider(criteria, true);

Location location = locationManager.getLastKnownLocation(provider);

mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

double lat = location.getLatitude();
double lng = location.getLongitude();
LatLng latLng = new LatLng(lat, lng);

at latitude is where it stops.

CAA
  • 968
  • 10
  • 27

2 Answers2

1

Do you retrieve the GoogleMap object from the fragment before calling those functions?

Something like:

//get map fragment from the static layout (in this case it has id = "map")
    MapFragment mapFragment = (MapFragment) getFragmentManager()
            .findFragmentById(R.id.map);
//get GoogleMap object, then you can start using it, for example enabling the location
    map = mapFragment.getMap();
    map.setMyLocationEnabled(true);
  • Yeah @Andrea Montanari it's being call, infact my maps opens up well with no problem, but that's just before I code it to get the current location. to be more clear if I just do this: MapFragment mapFragment = (MapFragment) getFragmentManager() .findFragmentById(R.id.map); map.setMyLocationEnabled(true); it wil open and point where I am but if I try to get the current location so the camera can move to the point I am this comes back null and make my app crash. – Raldin Hidalgo Mar 02 '15 at 15:46
  • Did you add the required permission to retrieve the position? – Andrea Montanari Mar 02 '15 at 19:53
  • Yeah I have added all of them: – Raldin Hidalgo Mar 02 '15 at 20:19
  • Ok, then I suggest you to do the following: in my working code I do these steps: gps = new GPSTracker(this); if(gps.canGetLocation()){ //retrieve coordinates double latitude = gps.getLatitude(); double longitude = gps.getLongitude(); latlng = new LatLng(latitude, longitude); //zoom in the map by level 16 map.animateCamera(CameraUpdateFactory.newLatLngZoom(latlng, 16)); You can see GPSTracker class from https://github.com/andreamontanari/android/ – Andrea Montanari Mar 03 '15 at 09:09
0

Thank you all for your help I appreciate it, I have already solved my problem. Here a piece of code that helped me get ti done:

//I needed to have this line before everything else for me to get my current 
//location from getLastKnownLocation method.

mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker").snippet("Snippet"));

//And the all the codes I had before
mMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();

String provider = locationManager.getBestProvider(criteria, true);

Location location = locationManager.getLastKnownLocation(provider);

mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

double lat = location.getLatitude();
double lng = location.getLongitude();
LatLng latLng = new LatLng(lat, lng);

mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
mMap.addMarker(new MarkerOptions()
             .position(new LatLng(lat, lng))
             .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
             .title("Here"));
MrDumb
  • 2,140
  • 1
  • 21
  • 31