有什么方法可以实现缩放监听器,一旦缩放级别改变就可以触发事件。到目前为止,我已经使用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)
{
}
}
答案 0 :(得分:1)
为了实现自定义缩放处理程序并添加侦听器,您需要创建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
。以下是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();
}
}
}
现在,您可以在布局中使用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()
时应该重新映射具有新状态的地图,因为它会强制视图按the API documentation重绘
答案 1 :(得分:0)
你必须使用
刷新地图mapview.invalidate()
postInvalidate在UI线程上发布无效请求,而a 调用invalidate()会立即使View无效
答案 2 :(得分:0)
当缩放级别改变时,地图会重新绘制,所以我只是将这种类型的功能放在我的叠加层绘制方法中,如下所示:
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()。