android中的多个叠加项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2479696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:01:27  来源:igfitidea点击:

Multiple overlay items in android

androidmapoverlay

提问by Bostjan

I seem to be having a problem with using ItemizedOverlay and OveralyItems in it.

我似乎在使用 ItemizedOverlay 和 OveralyItems 时遇到问题。

I can get the first overlayItem to appear on the map but not any items after that.

我可以让第一个 overlayItem 出现在地图上,但之后不能出现任何项目。

Code sample is on: http://www.anddev.org/multiple_overlay_items-t12171.html

代码示例位于:http: //www.anddev.org/multiple_overlay_items-t12171.html

Quick overview here:

在这里快速概述:

public class Markers extends ItemizedOverlay {

 private Context ctx;

 private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

 public Markers(Drawable defaultMarker, Context cont) {

      super(boundCenterBottom(defaultMarker));
      this.ctx = cont;
      // TODO Auto-generated constructor stub
 }

 @Override
 protected OverlayItem createItem(int i) {
      // TODO Auto-generated method stub
      return mOverlays.get(i);
 }

 @Override
 public boolean onTap(GeoPoint p, MapView mapView) {
      // TODO Auto-generated method stub
      return super.onTap(p, mapView);
 }


 @Override
 protected boolean onTap(int index) {
      // TODO Auto-generated method stub
      Toast.makeText(this.ctx, mOverlays.get(index).getTitle().toString()+", Latitude: "+mOverlays.get(index).getPoint().getLatitudeE6(), Toast.LENGTH_SHORT).show();
      return super.onTap(index);         
 }

 @Override
 public int size() {
      // TODO Auto-generated method stub
      return mOverlays.size();
 }

 public void addOverlay(OverlayItem item) {
      mOverlays.add(item);
      setLastFocusedIndex(-1);
      populate();

 }

 public void clear() {
      mOverlays.clear();
      setLastFocusedIndex(-1);
      populate();
 }
}

Samples of how it's used:

它的使用示例:

Markers usersMarker = new Markers(user,overview.this); 
GeoPoint p = new GeoPoint((int) (lat * 1E6),(int) (lon * 1E6));
OverlayItem item = new OverlayItem(p,userData[0],userData[3]);
item.setMarker(this.user);
usersMarker.addOverlay(item); 

the first marker shows up on the map but if I add any more they don't show up? Is there a problem with the populate() method? I tried calling it manually after adding all markers but it still didn't help. Please, if you have any idea what could be wrong, say so.

第一个标记出现在地图上,但如果我添加更多标记,它们就不会出现?populate() 方法有问题吗?我尝试在添加所有标记后手动调用它,但它仍然没有帮助。请,如果您有任何想法可能是错误的,请说出来。

回答by Praveen

check this sample projectit helps. add multiple addOverlay()s on ur multiple times

检查这个示例项目它有帮助。多次在 ur 上添加多个 addOverlay()

回答by Mark Borgerding

I have working code that looks almost exactly like yours, except I do not call setLastFocusedIndex in my addOverlay function. Try commenting it out and see if it works.

我的工作代码看起来几乎和你的一模一样,只是我没有在我的 addOverlay 函数中调用 setLastFocusedIndex。尝试将其注释掉,看看它是否有效。

回答by Bostjan

I have finally found an answer. I'm quite ashamed to admit it but the problem was not in the items not drawing but in me not seeing them on screen....The locations for the 2 items were supposed to be nearly identical...but one of them was calculated with a bug and was moved halfway round the world.

我终于找到了答案。我很惭愧地承认,但问题不在于没有绘制的项目,而在于我没有在屏幕上看到它们......这两个项目的位置应该几乎相同......但其中一个是用错误计算并被移动了半个地球。

So I never bothered to look or zoom out...when I did i found my other marker sitting somewehre in the Barren's sea :) Thanks to all who tried to help me...oh and the above code works :)

所以我从来没有费心去看或缩小...当我这样做时,我发现我的另一个标记坐在贫瘠的海中的某个地方:)感谢所有试图帮助我的人...哦,上面的代码有效:)