Java 在 Android Beta 0.9 中使用 ItemizedOverlay 和 OverlayItem
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26362/
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
Using ItemizedOverlay and OverlayItem In Android Beta 0.9
提问by Reto Meier
Has anyone managed to use ItemizedOverlays
in Android Beta 0.9? I can't get it to work, but I'm not sure if I've done something wrong or if this functionality isn't yet available.
有没有人设法ItemizedOverlays
在 Android Beta 0.9 中使用?我无法让它工作,但我不确定我是否做错了什么,或者这个功能是否尚不可用。
I've been trying to use the ItemizedOverlay
and OverlayItem
classes. Their intended purpose is to simulate map markers (as seen in Google Maps Mashups) but I've had problems getting them to appear on the map.
我一直在尝试使用ItemizedOverlay
和OverlayItem
类。它们的预期目的是模拟地图标记(如 Google Maps Mashups 中所示),但我在让它们出现在地图上时遇到了问题。
I can add my own custom overlays using a similar technique, it's just the ItemizedOverlays
that don't work.
我可以使用类似的技术添加我自己的自定义叠加层,只是ItemizedOverlays
不起作用。
Once I've implemented my own ItemizedOverlay
(and overridden createItem
), creating a new instance of my class seems to work (I can extract OverlayItems
from it) but adding it to a map's Overlay
list doesn't make it appear as it should.
一旦我实现了我自己的ItemizedOverlay
(并被覆盖createItem
),创建我的类的新实例似乎可以工作(我可以从中提取OverlayItems
),但是将它添加到地图的Overlay
列表中并不会使它看起来像它应该的那样。
This is the code I use to add the ItemizedOverlay
class as an Overlay
on to my MapView
.
这是我用来将ItemizedOverlay
类添加Overlay
到我的MapView
.
// Add the ItemizedOverlay to the Map
private void addItemizedOverlay() {
Resources r = getResources();
MapView mapView = (MapView)findViewById(R.id.mymapview);
List<Overlay> overlays = mapView.getOverlays();
MyItemizedOverlay markers = new MyItemizedOverlay(r.getDrawable(R.drawable.icon));
overlays.add(markers);
OverlayItem oi = markers.getItem(0);
markers.setFocus(oi);
mapView.postInvalidate();
}
Where MyItemizedOverlay
is defined as:
其中MyItemizedOverlay
定义为:
public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
public MyItemizedOverlay(Drawable defaultMarker) {
super(defaultMarker);
populate();
}
@Override
protected OverlayItem createItem(int index) {
Double lat = (index+37.422006)*1E6;
Double lng = -122.084095*1E6;
GeoPoint point = new GeoPoint(lat.intValue(), lng.intValue());
OverlayItem oi = new OverlayItem(point, "Marker", "Marker Text");
return oi;
}
@Override
public int size() {
return 5;
}
}
采纳答案by eon
For the sake of completeness I'll repeat the discussion on Reto's post over at the Android Groups here.
为完整起见,我将在这里重复讨论 Reto 在Android Groups上的帖子。
It seems that if you set the bounds on your drawable it does the trick:
似乎如果您在 drawable 上设置边界,它就可以解决问题:
Drawable defaultMarker = r.getDrawable(R.drawable.icon);
// You HAVE to specify the bounds! It seems like the markers are drawn
// through Drawable.draw(Canvas) and therefore must have its bounds set
// before drawing.
defaultMarker.setBounds(0, 0, defaultMarker.getIntrinsicWidth(),
defaultMarker.getIntrinsicHeight());
MyItemizedOverlay markers = new MyItemizedOverlay(defaultMarker);
overlays.add(markers);
By the way, the above is shamelessly ripped from the demo at MarcelP.info. Also, here is a good howto.
顺便说一下,上面的内容是从MarcelP.info 的演示中无耻地撕下来的。此外,这里有一个很好的方法。
回答by zahra salmaninejad
try :
尝试 :
Drawable defaultMarker = r.getDrawable(R.drawable.icon);
defaultMarker.setBounds(0, 0, defaultMarker.getIntrinsicWidth(),
defaultMarker.getIntrinsicHeight());
MyItemizedOverlay markers = new MyItemizedOverlay(defaultMarker);
overlays.add(markers);