you need to deal with google maps first, as you will display the map. Look at this Using Google Maps in Android on mobiForge .
Secondly, you need a class that provides gps data. Itβs easier to get location data and update the interface using a message handler. Here is an example:
public MyGPS implements LocationListener{ public LocationManager lm = null; private MainActivity SystemService = null; //lat, lng private double mLongitude = 0; private double mLatitude = 0; public MyGPS(MainActivity sservice){ this.SystemService = sservice; this.startLocationService(); } public void startLocationService(){ this.lm = (LocationManager) this.SystemService.getSystemService(Context.LOCATION_SERVICE); this.lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 5, this); } public void onLocationChanged(Location location) { location = this.lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); try { this.mLongitude = location.getLongitude(); this.mLatitude = location.getLatitude(); } catch (NullPointerException e) { Log.i("Null pointer exception " + mLongitude + "," + mLatitude, null); } } }
In your onCreate method, instantiate this class and placelistener will start listening for gps updates. But you cannot access lng and lat, since you do not know from your activity, since they are set or zero. So you need a handler that sends a message to your main action when lat and lng are set:
Change the following method:
public void onLocationChanged(Location location) { location = this.lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); try { this.mLongitude = location.getLongitude(); this.mLatitude = location.getLatitude(); Message msg = Message.obtain(); msg.what = UPDATE_LOCATION; this.SystemService.myViewUpdateHandler.sendMessage(msg); } catch (NullPointerException e) { Log.i("Null pointer exception " + mLongitude + "," + mLatitude, null); } }
In your core business, add this:
Handler myViewUpdateHandler = new Handler(){ public void handleMessage(Message msg) { switch (msg.what) { case UPDATE_LOCATION:
Since the handler is in your map, you can easily update your interface in the handler itself. Whenever gps data is available, a message is triggered and accepted by the handler.
Developing a REST API is a very interesting thing. An easy way is to have a php script on a web server that returns some json data on request. If you want to develop such a service, this tutorial can help you, link .