Issue
In my application i have multiple latitude and longitude from my web service. At this point in time, i am able to display only one coordinate on goole map using the code below.
public class CustomMap extends MapActivity {
MapView mapView;
List<Overlay> mapOverlays;
Drawable drawable;
Drawable drawable2;
CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gmap);
//setContentView(LayoutInflater.from(getParent()).inflate(R.layout.main_ou, null));
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapOverlays = mapView.getOverlays();
// second overlay
drawable2 = getResources().getDrawable(R.drawable.red);
itemizedOverlay2 = new CustomItemizedOverlay<CustomOverlayItem>(drawable2, mapView);
GeoPoint point3 = new GeoPoint((int)(-10.224609*1E6),(int)(17.506044*1E6));
CustomOverlayItem overlayItem3 = new CustomOverlayItem(point3, "Title",
"Cliquez here", null);
itemizedOverlay2.addOverlay(overlayItem3);
//itemizedOverlay2.onTap(0);
mapOverlays.add(itemizedOverlay2);
final MapController mc = mapView.getController();
mc.animateTo(point3);
mc.setZoom(16);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
Now, i want to set latitude and longitude on the map dynamically using arrays. I am using ArrayList val; to get and set data. Can someone give me a hint on how to make the class dynamic.
NEW UPDATE THE CODE BELOW DISPLAYS ONLY ONE MARKER BUT I WANT ALL MARKERS TO APPEAR
public class Map extends MapActivity {
private MapController mapControll;
private GeoPoint geoPoint=null;
private MapView mapview;
private MyItemizedOverlay userPicOverlay;
private MyItemizedOverlay nearPicOverlay;
private Drawable userPic,atmPic;
private CustomOverlayItem nearatms[] = new CustomOverlayItem[50];
public static Context context;
double latitude,longitude;
ArrayList<Article> mArticles;
CustomOverlayItem overlayItem3 ;
public ArrayList<Article> articles;
public static ArrayList<Article> article;
EditText search;
DBHelper helper;
GeoPoint point;
MapView mapView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aproximite);
context = getApplicationContext();
mapView = (MapView) findViewById(R.id.mapview);
mapView = (MapView) findViewById(R.id.mapview);
search = (EditText)findViewById(R.id.search_box);
helper = DBHelper.getInstance(this);
geoPoint = new GeoPoint((int)(latitude * 1E6),(int)(longitude * 1E6));
mapview = (MapView)findViewById(R.id.mapview);
mapControll= mapview.getController();
mapview.setBuiltInZoomControls(true);
mapview.setStreetView(true);
mapControll.setZoom(16);
mapControll.animateTo(geoPoint);
userPic = this.getResources().getDrawable(R.drawable.boutton_log);
userPicOverlay = new MyItemizedOverlay(userPic);
OverlayItem overlayItem = new OverlayItem(geoPoint, "I'm Here!!!", null);
userPicOverlay.addOverlay(overlayItem);
mapview.getOverlays().add(userPicOverlay);
atmPic = this.getResources().getDrawable(R.drawable.boutton_log);
nearPicOverlay = new MyItemizedOverlay(atmPic);
new GetLocationTask().execute();
}
private class GetLocationTask extends AsyncTask<Void, Void, Void> {
@Override
public Void doInBackground(Void... params) {
mArticles = helper.getArticlesList();
return null;
}
@Override
public void onPreExecute() {}
@Override
public void onPostExecute(Void result) {
try {
for (int i = 0; i < mArticles.size(); i++) {
point = new GeoPoint((int)(Double.parseDouble(mArticles.get(i).getLatitude())*1E6),(int)(Double.parseDouble(mArticles.get(i).getLongitude())*1E6));
nearatms[i] = new CustomOverlayItem(point ,"title",
"click here", null);
nearPicOverlay.addOverlay(nearatms[i]);
}
mapview.getOverlays().add(nearPicOverlay);
//Added symbols will be displayed when map is redrawn so force redraw now
mapview.postInvalidate();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
MyItemizedOverlay.class
public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> myOverlays ;
public MyItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
myOverlays = new ArrayList<OverlayItem>();
populate();
}
public void addOverlay(OverlayItem overlay){
myOverlays.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int i) {
return myOverlays.get(i);
}
// Removes overlay item i
public void removeItem(int i){
myOverlays.remove(i);
populate();
}
// Returns present number of items in list
@Override
public int size() {
return myOverlays.size();
}
public void addOverlayItem(OverlayItem overlayItem) {
myOverlays.add(overlayItem);
populate();
}
public void addOverlayItem(int lat, int lon, String title) {
try {
GeoPoint point = new GeoPoint(lat, lon);
OverlayItem overlayItem = new OverlayItem(point, title, null);
addOverlayItem(overlayItem);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
@Override
protected boolean onTap(int index) {
// TODO Auto-generated method stub
String title = myOverlays.get(index).getTitle();
System.out.println(index);
return super.onTap(index);
}
}
Solution
use this way..
for google map v1 --> https://stackoverflow.com/a/9630396/1168654
for Google MAP v2 check below code
call this method
for(int i = 0 ; i < arraylist.size();i++){
addMarker(mapview,arraylistLat.get(i),arraylistLong.get(i),title.get(i),arraysnippet.get(i));
}
private void addMarker(GoogleMap map, double lat, double lon,
String string, String string2) {
map.addMarker(new MarkerOptions().position(new LatLng(lat, lon))
.title(string).snippet(string2));
}
Answered By - Dhaval Parmar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.