这是我在Activity中使用的代码,用于在mapview上显示多个标记。 我使用了一个自定义覆盖类,它扩展了逐项覆盖。以下是该类的代码:
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();
}
}
我在主UI活动中使用此类。以下是我使用此类为我的mapview添加叠加层的方法:
更新
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=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();
}
}
屏幕上只显示一个叠加层。可能是什么问题呢。我已经检查过我的班级中添加了几个叠加项,但是当我将该类添加到我的mapview时,只会绘制一个叠加层。
我需要在地图上添加总共81个标记。所以我制作81个叠加项并将它们添加到叠加层并在最后添加叠加层。
提前谢谢。
答案 0 :(得分:0)
你错过了:
myMap.getOverLays().add(customOverlay);
在
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();
}
}
答案 1 :(得分:0)
无论noOfPoles如何,你都会继续添加元素0,1,2和3.你可能有多个标记,但它们都在同一个地方。你需要使它成为'i'
的功能像
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];
....
}