Monday, April 30, 2012

Android: Constantly Update Map With Coordinates from Web Service

Hello I am writing an app that makes the use of coordinates from a webserver. I know how to retrieve the coordinates from the webservice, but once I get them how can I constantly display them on the map. Sort of like the Location Listener updates the point on the map when the location from the location manager changes. I want to constantly update the points I receive from the webservice.
Is this done with a Service? If so how?



        public class GeoUpdateHandler implements LocationListener {  

@Override
public void onLocationChanged(Location location) {
...
GeoPoint point = new GeoPoint(lat, lng);
createMarker();
mapController.animateTo(point); // mapController.setCenter(point);
}


UPDATED: Code for Createmarker



private void createMarker() {  
GeoPoint p = mapView.getMapCenter();
OverlayItem overlayitem = new OverlayItem(p, "", "");
itemizedoverlay.addOverlay(overlayitem);
mapView.getOverlays().add(itemizedoverlay);
}


UPDATED: Code for getting coordinates from webservice...



HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(".../android/serverFile.php");
JSONObject json = new JSONObject();
try {
JSONArray postjson=new JSONArray();
postjson.put(json);
// Execute HTTP Post Request
System.out.print(json);
HttpResponse response = httpclient.execute(httppost);
// for JSON:
if(response != null)
{
InputStream is = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String jsonStr = sb.toString();
JSONObject jsonObj = new JSONObject(jsonStr);
String longitudecord = jsonObj.getString("lon");
String latitudecord = jsonObj.getString("lat");


}





No comments:

Post a Comment