3

Sorry for being newbie. I just want to create an app that will get current location thru gps, whether location is change or not.

I can manage to get the location

onCreate code:

    map = (MapView) findViewById(R.id.map);
    map.setTileSource(TileSourceFactory.MAPNIK);
    map.setBuiltInZoomControls(true);
    map.setMultiTouchControls(true);


    myLocationOverlay = new MyLocationNewOverlay(getApplicationContext(), map);
    map.getOverlays().add(myLocationOverlay);
    myLocationOverlay.enableMyLocation();
    myLocationOverlay.enableFollowLocation();

but cannot implement to get real-time coordinates implemented on onLocationChanged. It will be much appreciated to have a whole source code that will show the solution. I get easily lost in part by part code.

Thank you in advance.

Mellorine
  • 265
  • 1
  • 5
  • 13

1 Answers1

2

http://developer.android.com/reference/android/location/LocationListener.html

have your activity implement the location listener interface, register the listener with the location service manager.

try {
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0, 0, this);
}
catch (Exception ex) {
    //do something useful here
}

there's a full example here Background service with location listener in android

Community
  • 1
  • 1
spy
  • 3,199
  • 1
  • 18
  • 26
  • good catch, needed for a) permission denial and b) the case where the device doesn't have gps (some devices will throw) – spy Mar 24 '16 at 11:57
  • thank you sir :) if you have time sir, can you look my another question, http://stackoverflow.com/questions/36243088/osmdroid-create-road-path-based-on-user-destination-using-a-table-of-latitude-l – Mellorine Mar 27 '16 at 02:18