Android 地图叠加问题
有什么方法可以实现缩放侦听器,该侦听器可以在缩放级别发生变化时立即触发事件。到目前为止,我已经使用 onUserinteraction 方法手动检查缩放级别的更改。 另一件事是我想要动态添加/删除覆盖。到目前为止我所做的是在 onUserinteraction 方法中,我调用一个动态添加叠加层的函数,使用 mapOverlays.add() 函数,并且添加实际上是动态发生的。但不知何故,mapOverlays.remove()函数没有删除覆盖层
//Function for adding first set of markers
public void setOverlay()
{
worldLength = World.length;
mapOverlays = mapView.getOverlays();
drawable0 = this.getResources().getDrawable(R.drawable.marker);
itemizedOverlay0 = new MyItemizedOverlay(drawable0);
for (int i = 0; i < worldLength; i++)
{
itemizedOverlay0.addOverlay(World[i]);
}
mapOverlays.add(itemizedOverlay0);
mapView.postInvalidate();
}
//Function for adding second set of markers
public void setOverlay1()
{
mapView.setStreetView(true);
usaLength = USA.length;
mexicoLength = Mexico.length;
mapOverlays = mapView.getOverlays();
drawable1 = this.getResources().getDrawable(R.drawable.marker);
itemizedOverlay1 = new MyItemizedOverlay(drawable1);
itemizedOverlay2 = new MyItemizedOverlay(drawable1);
for (int i = 0; i < usaLength; i++) {
itemizedOverlay1.addOverlay(USA[i]);
}
for (int i = 0; i < mexicoLength; i++) {
itemizedOverlay2.addOverlay(Mexico[i]);
}
mapOverlays.add(itemizedOverlay1);
mapOverlays.add(itemizedOverlay2);
mapView.postInvalidate();
}
public void onUserInteraction() {
super.onUserInteraction();
if(mapView.getZoomLevel()>3)
{
mapOverlays.remove(itemizedOverlay0);
setOverlay1();
//the above happens
}
else if(mapView.getZoomLevel()<=3 && mapOverlays.size()>5)
{
mapOverlays.remove(itemizedOverlay1);
mapOverlays.remove(itemizedOverlay2);
//the above doesn't
setOverlay();
}
else if(mapView.getZoomLevel()<=3)
{
}
}
Is there any way i can implement a zoom listener that can trigger events as soon as the zoom level changes. So far, i have managed with the onUserinteraction method manually checking for changing zoom-level.
Another thing is i to want add/remove overlays dynamically. what i,ve done so far is in the onUserinteraction method, i call a function which adds the overlays dynamically, using mapOverlays.add() function and the addition actually does happen dynamically. But somehow mapOverlays.remove() function is not removing the overlays
//Function for adding first set of markers
public void setOverlay()
{
worldLength = World.length;
mapOverlays = mapView.getOverlays();
drawable0 = this.getResources().getDrawable(R.drawable.marker);
itemizedOverlay0 = new MyItemizedOverlay(drawable0);
for (int i = 0; i < worldLength; i++)
{
itemizedOverlay0.addOverlay(World[i]);
}
mapOverlays.add(itemizedOverlay0);
mapView.postInvalidate();
}
//Function for adding second set of markers
public void setOverlay1()
{
mapView.setStreetView(true);
usaLength = USA.length;
mexicoLength = Mexico.length;
mapOverlays = mapView.getOverlays();
drawable1 = this.getResources().getDrawable(R.drawable.marker);
itemizedOverlay1 = new MyItemizedOverlay(drawable1);
itemizedOverlay2 = new MyItemizedOverlay(drawable1);
for (int i = 0; i < usaLength; i++) {
itemizedOverlay1.addOverlay(USA[i]);
}
for (int i = 0; i < mexicoLength; i++) {
itemizedOverlay2.addOverlay(Mexico[i]);
}
mapOverlays.add(itemizedOverlay1);
mapOverlays.add(itemizedOverlay2);
mapView.postInvalidate();
}
public void onUserInteraction() {
super.onUserInteraction();
if(mapView.getZoomLevel()>3)
{
mapOverlays.remove(itemizedOverlay0);
setOverlay1();
//the above happens
}
else if(mapView.getZoomLevel()<=3 && mapOverlays.size()>5)
{
mapOverlays.remove(itemizedOverlay1);
mapOverlays.remove(itemizedOverlay2);
//the above doesn't
setOverlay();
}
else if(mapView.getZoomLevel()<=3)
{
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了实现自定义缩放处理程序并添加侦听器,您需要创建 MapView 的子类。首先,让我们为
OnZoomListener
创建接口。请注意,您可以更改接口以满足您的需要,以下只是供您开始的框架代码。现在,您需要一个子类,每当缩放发生变化时,该子类都会调用此
OnZoomListener
。 的扩展的骨架代码这是这个SO答案现在 您可以在布局中使用
MyMapView
而不是标准的MapView
。注意:代码包com.so4729255
我仅用于测试。最后在代码中添加
OnZoomListener
。下面的代码只是显示Toast以说明用法,您可以在那里做任何您需要做的事情至于您的第二个问题,正如每个人都回答的那样,调用
MapView.invalidate()
应该用以下命令重新绘制地图它的新状态,因为它将强制视图按照 API 文档重绘< /a>In order to implement your custom zoom handler and add a listener, you need to be create your subclass of the MapView. First, let's create the interface for the
OnZoomListener
. Note that you could change the interface to suit your need, the following is just a skeleton code for you to start withNow, you need a subclass that will call this
OnZoomListener
whenever the zoom changes. Here is the skeleton code for that which is an extension of this SO AnswerNow you could use
MyMapView
in your layout instead of the standardMapView
. Note: The code packagecom.so4729255
is what I use for testing only.And finally add the
OnZoomListener
in your code. The code below just show the Toast for illustration of usage, you could do whatever you need to do thereAs for your second question, as everyone has answered, calling
MapView.invalidate()
should repaint the map with its new state as it will force the View to redraw per the API documentation那么你必须使用刷新地图
well you have to refresh the map using
当缩放级别发生变化时,地图会重新绘制,因此我只需将此类功能放入叠加层的绘制方法中,如下所示:
当您删除叠加层时,请记住调用mapView.invalidate()。
When the zoom level changes, the map is redrawn so I simply put this type of functionality in my overlay's draw method, like so:
and when you remove an overlay, remember to call mapView.invalidate().