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");
}
UPDATE WITH TIMER:
public void startService(){
timer.scheduleAtFixedRate(new TimerTask(){
@Override
public void run() {
//Call the method that contacts the server and
//adds the coordinates to a list. Get those values from the list.
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.kre8tiveinspired.com/android/serverFile.php");
JSONObject json = new JSONObject();
try {
// JSON data:
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);
// grabbing the menu object
String longitudecord = jsonObj.getString("lon");
String latitudecord = jsonObj.getString("lat");
latcord = latitudecord;
loncord = longitudecord;
}
}catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Handler handler = new Handler();
handler.post(new Runnable(){
public void run(){
double aDouble = Double.parseDouble(loncord);
double bDouble = Double.parseDouble(latcord);
int lat = (int) (aDouble * 1E6);
int lng = (int) (bDouble * 1E6);
GeoPoint point = new GeoPoint(lat, lng);
createMarker();
mapController.animateTo(point); // mapController.setCenter(point);
//add the overlays to the map
// and call invalidate for the mapview.
mapView.invalidate();
}
});
}
}, 0, 1000); // Here im calling the timer every 10 seconds
Well what you can do is to use a
timerto populate the map. Basically, you can have one method which contacts the server, fetches the coordinates and adds them into an arraylist. Now this method can be called from inside the timer (which can run every few seconds, depending on your needs). Also, inside this timer class, you can re-populate the map. Some code for this:If you would want to look at an example project that uses this idea, head here, and have a look at the timer inside
onResume(). It uses another class calledFetchParsethat contacts the server and gets the updated list of coordinates.