多个覆盖项目在 Android 的地图视图上不可见
这是我在活动中使用的代码,用于在地图视图上显示多个标记。 我使用了一个扩展 itemizedoverlays 的自定义覆盖类。以下是该类的代码:
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 活动中使用该类。以下是我如何使用此类向我的地图视图添加叠加层:
更新
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();
}
}
屏幕上仅显示一个叠加项。可能是什么问题。我已经检查过有几个覆盖项目被添加到我的类中,但是当我将类添加到我的地图视图时,只绘制了一个覆盖项目。
我需要在地图上添加总共 81 个标记。所以我制作了 81 个覆盖项并将它们添加到覆盖层并在最后添加一次覆盖层。
先感谢您。
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
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();
}
}
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您缺少:
myMap.getOverLays().add(customOverlay);
在
You are missing:
myMap.getOverLays().add(customOverlay);
In
无论极数如何,您都会继续添加元素 0、1、2 和 3。您可能有多个标记,但它们都在同一个位置。你需要让它成为“i”的
函数
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