here is the code that i am using in my Activity to show multiple markers on the mapview. i have used a customoverlay class which extends itemizedoverlays. Here is the code for that class:
public class view1CustomOverlays extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
boolean isClickable = false;
Context context = null;
Drawable orangeMarker = null;
public view1CustomOverlays(Drawable defaultMarker, Context c,
boolean isActive, Drawable inActiveMarker) {
super(boundCenterBottom(defaultMarker));
context = c;
isClickable = isActive;
orangeMarker = inActiveMarker;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
return mOverlays.size();
}
}
i am using this class in the main UI activity. Here is how i am using this class to add overlays to my mapview:
UPDATE
priva开发者_如何学JAVAte void view1LoadDataOnMap(String[] poleCord) {
try {
Drawable redFlag = getResources().getDrawable(R.drawable.red);
Drawable greenFlag = getResources().getDrawable(R.drawable.green);
Drawable orangeFlag = getResources().getDrawable(R.drawable.orange);
int noOfPoles = poleCord.length / 4;
List<Overlay> list = map.getOverlays();
list.clear();
view1CustomOverlays customOverlay = null;
for (int i = 0; i < noOfPoles; i=i+4) {
Float lat = Float.parseFloat(poleCord[i]);
Float lng = Float.parseFloat(poleCord[i+1]);
String poleNumber = poleCord[i+2];
String ticketId = poleCord[i+3];
customOverlay = new view1CustomOverlays(greenFlag, this, true,
orangeFlag);
GeoPoint p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
OverlayItem overlayItem = new OverlayItem(p, poleNumber, null);
customOverlay.addOverlay(overlayItem);
Log.i("adding overlay",overlayItem.toString());
}
list.add(customOverlay);
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (NotFoundException e) {
e.printStackTrace();
}
}
Only one overlayitem is showing on the screen. What could be the problem. i have checked that there are several overlayitems being added to my class but when i add to the class to my mapview only one overlayitem is being drawn.
There are in total 81 markers that i need to add on the map. So i am making 81 overlayitems and adding them to the overlay and adding the overlay once at the end.
thank you in advance.
You are missing:
myMap.getOverLays().add(customOverlay);
In
private void view1LoadDataOnMap(String[] poleCord) {
try {
Drawable redFlag = getResources().getDrawable(R.drawable.red);
Drawable greenFlag = getResources().getDrawable(R.drawable.green);
Drawable orangeFlag = getResources().getDrawable(R.drawable.orange);
int noOfPoles = poleCord.length / 4;
List<Overlay> list = map.getOverlays();
list.clear();
view1CustomOverlays customOverlay = null;
for (int i = 0; i < noOfPoles; i++) {
Float lat = Float.parseFloat(poleCord[0]);
Float lng = Float.parseFloat(poleCord[1]);
String poleNumber = poleCord[2];
String ticketId = poleCord[3];
customOverlay = new view1CustomOverlays(greenFlag, this, true,
orangeFlag);
GeoPoint p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
OverlayItem overlayItem = new OverlayItem(p, poleNumber, null);
customOverlay.addOverlay(overlayItem);
myMap.getOverLays().add(customOverlay);
Log.i("adding overlay",overlayItem.toString());
}
list.add(customOverlay);
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (NotFoundException e) {
e.printStackTrace();
}
}
Irrespective of the noOfPoles, you keep on adding elements 0,1,2 and 3. You probably have multiple markers but they're all in the same place. You need to make it a function of 'i'
like
for (int i = 0; i < noOfPoles; i++) {
Float lat = Float.parseFloat(poleCord[ i * 4]);
Float lng = Float.parseFloat(poleCord[i * 4 + 1]);
String poleNumber = poleCord[i * 4 + 2];
String ticketId = poleCord[i * 4 + 3];
....
}
精彩评论