4

我对适用于 Android 组件 MyLocationOverlay 的 GoogleMap API 有疑问。每次设备位置发生变化(并且用户位置超出可见部分或地图)时,我都想关闭 MyLocationOverlay 自动重新居中功能(到当前用户位置)。

当用户位置超出屏幕时,似乎没有标志可以关闭不需要的地图自动居中功能

例如

public class LocationTest extends MapActivity{

  private MapView map;
  private MapController controller;

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    map = (MapView) findViewById(R.id.map);
    final MyLocationOverlayUpdate overlay = new MyLocationOverlayUpdate(this, map);
    overlay.enableMyLocation();
    map.getOverlays().add(overlay);
  }

  @Override
  protected boolean isRouteDisplayed() {
    return false;
  }
}

public class MyLocationOverlayUpdate extends MyLocationOverlay {

  public MyLocationOverlayUpdate(Context context, MapView mapView) {
    super(context, mapView);
  }

  public void drawMyLocation(Canvas canvas, MapView mapView, 
              Location location, GeoPoint geoPoint, long when) {}
}

我通过网络查看了这个问题,但没有找到提供的解决方案。

发现的附加信息:

  • 我猜文档是错误的、错误的,或者只是过时的说法:

    可选地,构造函数还可以使用 MapController 并使用它来保持“我的位置”点通过在地图离开屏幕时平移地图来保持可见

因为没有构造函数接受 MapController 作为参数,似乎我别无选择keeping the "my location" dot visible

  • 我偶然发现了一种解决方法,方法是覆盖方法 drawMyLocation() 而不调用 super.drawMyLocation() 解决了重新定位地图的问题(不希望的“功能”关闭)。但是我必须重新实现 drawMyLocation() 方法,因此更改默认外观(并花费时间......)

也许有更明确的解决方案?

4

2 回答 2

0

扩展 MyLocationOverlay 类并通过覆盖 drawMyLocation 方法重新实现覆盖动画。

public class CustomMyLocationOverlay extends MyLocationOverlay {

    private static final String TAG = "Bankomatai";


    private Drawable[] slips;
    private final MapView mapView;
    private int currSlip = 0;
    private Location savedFix = null;
    Point point = new Point();
    Rect rect = new Rect();
    private Handler handler = new Handler();
    private Runnable overlayAnimationTask;


    public CustomMyLocationOverlay(Context context, MapView mapView) {
        super(context, mapView);
        this.mapView = mapView;
        slips = new Drawable[4];
        slips[0] = context.getResources().getDrawable(R.drawable.ic_maps_indicator_current_position_1);
        slips[1] = context.getResources().getDrawable(R.drawable.ic_maps_indicator_current_position_2);
        slips[2] = context.getResources().getDrawable(R.drawable.ic_maps_indicator_current_position_3);
        slips[3] = context.getResources().getDrawable(R.drawable.ic_maps_indicator_current_position_4);
        overlayAnimationTask = new Runnable() {

        @Override
        public void run() {
            currSlip = (currSlip + 1) % slips.length;
            CustomMyLocationOverlay.this.mapView.invalidate();
            handler.removeCallbacks(overlayAnimationTask);
            handler.postDelayed(overlayAnimationTask, 200);

        }

        };
        handler.removeCallbacks(overlayAnimationTask);
        handler.postAtTime(overlayAnimationTask, 100);
    }



    protected void drawMyLocation(Canvas canvas, MapView mapView, Location lastFix, GeoPoint myLocation, long when) {   
        Log.v(TAG, "draw: " + System.currentTimeMillis());      
        mapView.getProjection().toPixels(myLocation, point);
        rect.left = point.x - slips[currSlip].getIntrinsicWidth() / 2;
        rect.top = point.y - slips[currSlip].getIntrinsicHeight() / 2;
        rect.right = point.x + slips[currSlip].getIntrinsicWidth() / 2;
        rect.bottom = point.y + slips[currSlip].getIntrinsicHeight() / 2;
        slips[currSlip].setBounds(rect);
        slips[currSlip].draw(canvas);
    }
}

另外,您需要创建 4 个图标并将它们放在 res/drawable 文件夹中,名称分别为 ic_maps_indicator_current_position_1、ic_maps_indicator_current_position_2、ic_maps_indicator_current_position_3、ic_maps_indicator_current_position_4。您需要自己绘制它们。

于 2012-12-18T16:23:33.647 回答
0
public class CustomMyLocationOverlay extends MyLocationOverlay {
 @Override
     protected void drawMyLocation(Canvas canvas, MapView mapView, Location location,
             GeoPoint geoPoint, long when) {
         if (!recenter) {
             mapView.getController().stopAnimation(false);
         }

         // Now draw the location overlay.
         super.drawMyLocation(canvas, mapView, location, geoPoint, when);
     }
}

位置变化时地图的居中使用与地图动画相同的逻辑,因此通过停止动画,我能够停止地图的重新居中。希望对您有所帮助。它对我有用。

于 2013-08-01T18:14:44.407 回答