Reputation: 133
I have a set of lat and lon values in MYSQL DB. I retrieved it in android app using JSON (retrieved from PHP code) and I am able to show those locations on map. But I donot know how to place markers on those locations. Please help me.
Following is my code :
try {
HttpClient client = new DefaultHttpClient();
URI website = new URI("http://192.168.1.15/latlonret1.php");
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response
.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.separator");
while ((l = in.readLine()) != null) {
sb.append(l + nl);
}
in.close();
data = sb.toString();
// return data;
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try {
JSONArray jArray = new JSONArray(data);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
JSONObject json_data1 = jArray.getJSONObject(i);
returnString =json_data.getString("lat") + "\n";
returnString1 =json_data1.getString("lon") + "\n";
System.out.println(returnString);
System.out.println(returnString1);
}
Intent viewIntent =new Intent(Androidmap.this,Mapview.class);
Bundle bundle = new Bundle();
bundle.putString("stuff", returnString);
bundle.putString("stuff1", returnString1);
viewIntent.putExtras(bundle);
startActivity(viewIntent);
}
Upvotes: 0
Views: 1307
Reputation: 5186
After you retreive the location(lat.long) from the JSON that you get from google
Follow these steps
REtrieve the location objects one by one and for every location create a GEOPOINT and mark it on the map as follows
for(Location l:locs){ Log.d("PAIRED-VALUES:",String.valueOf(l.getLatitude())+","+String.valueOf(l.getLongitude())); GeoPoint point =new GeoPoint((int)(l.getLatitude()*1E6), (int)(l.getLongitude()*1E6));
controller.animateTo(point);
controller.setZoom(17);
Map_OverLays=map.getOverlays();
itemizedoverlay=new HelloItemizedOverlay(draw, context);
overlayitem = new OverlayItem(point,l.getExtras().getString("TITLE"),l.getExtras().getString("VICINITY"));
itemizedoverlay.addOverlay(overlayitem);
Map_OverLays.add(itemizedoverlay);
map.invalidate();
}
AND HERES THE ITEMIZED OVERLAY CLASS
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
@SuppressWarnings("rawtypes")
public class HelloItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> Map_Overlays=new ArrayList<OverlayItem>();
//private ArrayList<OverlayItem> Map_Overlays1=new ArrayList<OverlayItem>();
Context mcontext;
public HelloItemizedOverlay(Drawable default_marker) {
super(boundCenterBottom(default_marker));
// TODO Auto-generated constructor stub
}
public HelloItemizedOverlay(Drawable default_marker,Context context) {
super(boundCenterBottom(default_marker));
mcontext=context;
//populate();
// TODO Auto-generated constructor stub
}
@Override
protected boolean onTap(int arg0) {
OverlayItem item=Map_Overlays.get(arg0);
AlertDialog.Builder dialog = new AlertDialog.Builder(mcontext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
public void addOverlay(OverlayItem item)
{
Log.d("NumberOfTimes" , item.getSnippet());
Map_Overlays.add(item);
populate();
}
@Override
protected OverlayItem createItem(int arg0) {
// TODO Auto-generated method stub
return Map_Overlays.get(arg0);
}
@Override
public int size() {
// TODO Auto-generated method stub
return Map_Overlays.size();
}
}
Upvotes: 0
Reputation: 4222
Use ItemizedOverlays to draw points on a map. Here's a link to Lars Vogel's tutorial
http://www.vogella.com/articles/AndroidGoogleMaps/article.html#maps_overlays
Upvotes: 1