Android 地图叠加问题

发布于 2024-12-05 16:07:53 字数 2001 浏览 0 评论 0原文

有什么方法可以实现缩放侦听器,该侦听器可以在缩放级别发生变化时立即触发事件。到目前为止,我已经使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

断桥再见 2024-12-12 16:07:53

为了实现自定义缩放处理程序并添加侦听器,您需要创建 MapView 的子类。首先,让我们为 OnZoomListener 创建接口。请注意,您可以更改接口以满足您的需要,以下只是供您开始的框架代码。


public interface OnZoomListener {
    /***
     * /**
     * Called when there is a zoom changes 
     * @param mapView Reference to the current map view
     * @param currentZoom The current zoom, set to -1 initially
     * @param newZoom The new zoom level
     */
     public void onZoomChanged(MapView mapView, int currentZoom, int newZoom);
}

现在,您需要一个子类,每当缩放发生变化时,该子类都会调用此OnZoomListener。 的扩展的骨架代码


public class MyMapView extends MapView {
    int oldZoomLevel=-1;
    OnZoomListener onZoomListener;

    public MyMapView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public MyMapView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyMapView(Context context, String apiKey) {
        super(context, apiKey);
    }

    public void setOnZoomListener(OnZoomListener onZoomListener) {
        this.onZoomListener = onZoomListener;
    }

    @Override
    public void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        int newZoom = this.getZoomLevel();

        if (newZoom != oldZoomLevel) {
            // dispatch the listeners
            if(oldZoomLevel != -1 &&  onZoomListener != null) {
                onZoomListener.onZoomChanged(this, oldZoomLevel, newZoom);
            }

            // update the new zoom level
            oldZoomLevel = getZoomLevel();
        }
    }
}

这是这个SO答案现在 您可以在布局中使用 MyMapView 而不是标准的 MapView。注意:代码包 com.so4729255 我仅用于测试。


  <com.so4729255.MyMapView
                android:id="@+id/mapview"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:enabled="true"
                android:apiKey="YOUR API KEY"
                android:clickable="true"
        />

最后在代码中添加 OnZoomListener 。下面的代码只是显示Toast以说明用法,您可以在那里做任何您需要做的事情


 MyMapView mapView = (MyMapView)this.findViewById(R.id.mapview);
        mapView.setOnZoomListener(new OnZoomListener() {
            public void onZoomChanged(MapView mapView, int currentZoom,
                    int newZoom) {
                // implement your handler here
                Toast t = Toast.makeText(WebViewMain.this, "Zoom has changed from " + currentZoom + " to " + 
                        newZoom, 500);
                t.show();
            }
        });

至于您的第二个问题,正如每个人都回答的那样,调用 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 with


public interface OnZoomListener {
    /***
     * /**
     * Called when there is a zoom changes 
     * @param mapView Reference to the current map view
     * @param currentZoom The current zoom, set to -1 initially
     * @param newZoom The new zoom level
     */
     public void onZoomChanged(MapView mapView, int currentZoom, int newZoom);
}

Now, 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 Answer


public class MyMapView extends MapView {
    int oldZoomLevel=-1;
    OnZoomListener onZoomListener;

    public MyMapView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public MyMapView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyMapView(Context context, String apiKey) {
        super(context, apiKey);
    }

    public void setOnZoomListener(OnZoomListener onZoomListener) {
        this.onZoomListener = onZoomListener;
    }

    @Override
    public void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        int newZoom = this.getZoomLevel();

        if (newZoom != oldZoomLevel) {
            // dispatch the listeners
            if(oldZoomLevel != -1 &&  onZoomListener != null) {
                onZoomListener.onZoomChanged(this, oldZoomLevel, newZoom);
            }

            // update the new zoom level
            oldZoomLevel = getZoomLevel();
        }
    }
}

Now you could use MyMapView in your layout instead of the standard MapView. Note: The code package com.so4729255 is what I use for testing only.


  <com.so4729255.MyMapView
                android:id="@+id/mapview"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:enabled="true"
                android:apiKey="YOUR API KEY"
                android:clickable="true"
        />

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 there


 MyMapView mapView = (MyMapView)this.findViewById(R.id.mapview);
        mapView.setOnZoomListener(new OnZoomListener() {
            public void onZoomChanged(MapView mapView, int currentZoom,
                    int newZoom) {
                // implement your handler here
                Toast t = Toast.makeText(WebViewMain.this, "Zoom has changed from " + currentZoom + " to " + 
                        newZoom, 500);
                t.show();
            }
        });

As 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

情愿 2024-12-12 16:07:53

那么你必须使用刷新地图

mapview.invalidate()

postInvalidate 在 UI 线程上发布无效请求,而
调用 invalidate() 会立即使视图无效

well you have to refresh the map using

mapview.invalidate()

postInvalidate posts an invalidate request on the UI-thread, while a
call to invalidate() invalidates the View immediately

没有你我更好 2024-12-12 16:07:53

当缩放级别发生变化时,地图会重新绘制,因此我只需将此类功能放入叠加层的绘制方法中,如下所示:

e@Override
public void draw(Canvas canvas, MapView mapv, boolean shadow)
{       
    int zoom = mapv.getZoomLevel();

    switch(zoom)
    {
        case 19:
            setMarkersForZoomLevel19();
            break;
        case 18:
            setMarkersForZoomLevel18();
            break;
        case 17:
            setMarkersForZoomLevel17();
            break;
        case 16:
            setMarkersForZoomLevel16();
            break;
        default:
            // Hide the markers or remove the overlay from the map view.                
            mapv.getOverlays().clear();
    }       

    // Putting this call here rather than at the beginning, ensures that
    // the Overlay items are drawn over the top of canvas stuff e.g. route lines.
    super.draw(canvas, mapv, false);        
}

当您删除叠加层时,请记住调用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:

e@Override
public void draw(Canvas canvas, MapView mapv, boolean shadow)
{       
    int zoom = mapv.getZoomLevel();

    switch(zoom)
    {
        case 19:
            setMarkersForZoomLevel19();
            break;
        case 18:
            setMarkersForZoomLevel18();
            break;
        case 17:
            setMarkersForZoomLevel17();
            break;
        case 16:
            setMarkersForZoomLevel16();
            break;
        default:
            // Hide the markers or remove the overlay from the map view.                
            mapv.getOverlays().clear();
    }       

    // Putting this call here rather than at the beginning, ensures that
    // the Overlay items are drawn over the top of canvas stuff e.g. route lines.
    super.draw(canvas, mapv, false);        
}

and when you remove an overlay, remember to call mapView.invalidate().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文