如何更改Google地图中MyLocationOverlay的默认蓝色动画标记?myLocationOverlay更改标记
4
A
回答
6
步骤#1:创建一个MyLocationOverlay
的子类。
第2步:重写drawMyLocation()
然后根据需要绘制标记。请记住,此方法不仅可以绘制标记,而且“如果用户的位置移动到屏幕边缘附近,并且我们在构造函数中给出了一个MapController
,我们将滚动以重新获取新读数”。
9
Thx @CommonsWare,你让我朝着正确的方向前进。花了一大笔钱,用这个。在http://code.google.com/android/add-ons/google-apis/reference/com/google/android/maps/MyLocationOverlay.html的Javadoc是错误的(或过时),并与你的大脑混乱的地方说:
drawMyLocation
而且,如果靠近屏幕边缘的用户的位置移动,和我们”我们在构造函数中给出了一个MapController,我们将滚动以重新获取新的读数。
这是完全错误的。首先,班上没有这样的构造函数。其次,drawMyLocation仅在当前位置位于视图内或屏幕移动时才被调用。所以如果你收到一个新的位置并且此刻没有看到标记,它将不会被重新绘制。一般而言,这是一件好事,但如果要在方法中实现mc.animateTo以保持位置居中,那是件坏事。
三的是,你不应该需要传递一个MapController,使currentLocation,因为你已经拥有的MapView,并从它那里得到的控制器..
所以我最后写我自己的类,保持以当前位置为中心,一旦位置到达mapView的边界或屏幕外:
public class CurrentLocationOverlay extends MyLocationOverlay {
// TODO: use dynamic calculation?
private final static int PADDING_ACTIVE_ZOOM = 50;
private MapController mc;
private Bitmap marker;
private Point currentPoint = new Point();
private boolean centerOnCurrentLocation = true;
private int height;
private int width;
/**
* By default this CurrentLocationOverlay will center on the current location, if the currentLocation is near the
* edge, or off the screen. To dynamically enable/disable this, use {@link #setCenterOnCurrentLocation(boolean)}.
*
* @param context
* @param mapView
*/
public CurrentLocationOverlay(Context context, MapView mapView) {
super(context, mapView);
this.mc = mapView.getController();
this.marker = BitmapFactory.decodeResource(context.getResources(), R.drawable.position);
}
@Override
protected void drawMyLocation(Canvas canvas, MapView mapView, Location lastFix, GeoPoint myLocation, long when) {
// TODO: find a better way to get height/width once the mapView is layed out correctly
if (this.height == 0) {
this.height = mapView.getHeight();
this.width = mapView.getWidth();
}
mapView.getProjection().toPixels(myLocation, currentPoint);
canvas.drawBitmap(marker, currentPoint.x, currentPoint.y - 40, null);
}
@Override
public synchronized void onLocationChanged(Location location) {
super.onLocationChanged(location);
// only move to new position if enabled and we are in an border-area
if (mc != null && centerOnCurrentLocation && inZoomActiveArea(currentPoint)) {
mc.animateTo(getMyLocation());
}
}
private boolean inZoomActiveArea(Point currentPoint) {
if ((currentPoint.x > PADDING_ACTIVE_ZOOM && currentPoint.x < width - PADDING_ACTIVE_ZOOM)
&& (currentPoint.y > PADDING_ACTIVE_ZOOM && currentPoint.y < height - PADDING_ACTIVE_ZOOM)) {
return false;
}
return true;
}
public void setCenterOnCurrentLocation(boolean centerOnCurrentLocation) {
this.centerOnCurrentLocation = centerOnCurrentLocation;
}
}
相关问题
- 1. MyLocationOverlay将标记更改为Google Maps默认
- 2. 如何更改mylocationoverlay的结果?
- 3. 更改标记
- 4. 更改标记与
- 5. 更改标记图标
- 6. 更改OpenLayers标记图标
- 7. Blogger更改ALT标记
- 8. 自动更改XML标记
- 9. 更改xml标记的值
- 10. 更改google标记的icon_image
- 11. 更改值标记PHP
- 12. 更改h3标记内容
- 13. 在woocommerce上更改标记
- 14. 动态更改HTML标记
- 15. 更改GMLib默认标记
- 16. 更改th标记值
- 17. 更改输入标记
- 18. 更改git冲突标记
- 19. 更改Google地图标记
- 20. Openlayers,更改聚簇标记
- 21. jquery更改元标记
- 22. 如何在MyLocationOverlay中将旋转箭头绘制为标记?
- 23. 更改标记图标基于瓷砖层更改 - 单张
- 24. Android动态更改标记标题
- 25. 更改Typo3中的标题标记
- 26. Onclick锚标记,更改标题跨度
- 27. 使用Jmapping更改标记图标
- 28. 如何更改标记图标?
- 29. 更改Google地图标记的图标
- 30. 在searchview中更改x标记图标
我只是想让它变成绿色而不是蓝色。我已经在android中找到资源(动画xml资源和4张图片)。改变颜色的部分很容易。但随着编程的进行,我想要完全相同的行为。有什么地方可以找到drawMyLocationMethod()的代码? – DArkO 2010-11-15 23:53:38
@DAKO:对不起,但Google API插件不是开源的。 – CommonsWare 2010-11-16 00:09:01
我还有1个关于如何做到这一点的想法。我可以用我的版本替换adroid SDK中的资源吗(类似名称,尺寸保持不变)? – DArkO 2010-11-16 14:48:43