关闭MyLocationOverlay组件中的自动重新定位功能

时间:2012-01-04 19:19:59

标签: android google-maps-api-3 mylocationoverlay

我对Android组件MyLocationOverlay的GoogleMap API存在问题。我希望每次设备位置发生变化时关闭MyLocationOverlay自动重新定位功能(到当前用户位置)(并且用户位置超出可见部分或地图)。

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

e.g。

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()方法,因此改变默认外观(并花费时间......)

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

2 个答案:

答案 0 :(得分: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。你需要自己画画。

答案 1 :(得分: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);
     }
}

当地点变化时,地图的重新定位使用与地图动画相同的逻辑,因此通过停止动画,我能够停止地图的重新定心。希望它可以帮到你。 它对我有用。