1

Everything seems to be working fine. It can find my location, but it won't call the onLocationChanged() method and create a marker for me. Any ideas?

public class MainActivity extends FragmentActivity implements LocationListener
{
Context context = this;
GoogleMap googlemap;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initMap();

    LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
    String provider = lm.getBestProvider(new Criteria(), true);
    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 100, 0, this);
}

public void onLocationChanged(Location location) {
    LatLng current = new LatLng(location.getLatitude(), location.getLatitude());
    Date date = new Date();

    googlemap.addMarker(new MarkerOptions()
            .title("Current Pos")
            .snippet(new Timestamp(date.getTime()).toString())
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
            .position(current)
            );
}

public void onStatusChanged(String provider, int status, Bundle extras) {
}

public void onProviderEnabled(String provider) {
}

private void initMap(){
    SupportMapFragment mf = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    googlemap = mf.getMap();

    googlemap.setMyLocationEnabled(true);
    googlemap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
}
Jane Doh
  • 2,883
  • 3
  • 15
  • 17

3 Answers3

2

Your Activity needs to implement the LocationSource interface, and you need to register your GoogleMap to use the MainActivity class as its location source:

Change your class declaration to:

public class MainActivity extends FragmentActivity implements LocationListener, LocationSource

And set the GoogleMap's LocationSource

//This is how you register the LocationSource for the map
googleMap.setLocationSource(this);

See this answer for more a complete example.

Community
  • 1
  • 1
DiscDev
  • 38,652
  • 20
  • 117
  • 133
0

Do you have wifi turned on? Everything seems to be well defined.

joaoprudencio
  • 684
  • 1
  • 6
  • 6
  • Yes, I have turned it off though too to just use the GPS. Should I change anything in here: LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE); or lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 100, 0, this): – Jane Doh Feb 15 '13 at 01:51
0

Maybe your wifi signal is weak and you're having connection problems? Try getting location updates through the GPS provider:

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 0, this);

In fact, I always include both so that the user can get his location from either provider. Also, try including a log statement in your onLocationChanged callback. If it turns out that method is in fact being called, then you know your problem is with adding the marker, not your location retrieval.

Halogen
  • 551
  • 6
  • 11
  • A simple debug log statement should do: Log.d("Name_of_class_here", "Location has updated!"); This'll print as a debug statement in your logcat if the line is ever reached. – Halogen Feb 15 '13 at 02:13
  • Nope, debug isnt getting called. so locationChanged() is somehow not getting called. DO I have to do something else with the listener? – Jane Doh Feb 15 '13 at 02:22
  • 1
    Hmmm... I don't see anything wrong with your implementation of the listener... you said "It can find my location, but it won't call the onLocationChanged() method" - what did you mean by that? – Halogen Feb 15 '13 at 02:31
  • I can go outside and press the button for current location and it will go to my current location. So it is getting updates. Should I implement the listener in another class? Or should I Set something different for the criteria? is 'this' suitable in the requestLocationUpdates? – Jane Doh Feb 15 '13 at 02:34
  • 1
    Are you sure your phone isn't responding to a previous location update near your current location? If you move a substantial distance (say, a block) and press it again, will it recognize that you've moved? I've never used the Activity itself as a locationListener - I always just create an anonymous class for it. I can't see anything wrong with the way you've implemented it but I'd try making a separate class for it and see if that works. And the criteria look fine to me. – Halogen Feb 15 '13 at 02:42