从onLocationChanged()更新MapView

时间:2011-09-13 10:15:18

标签: android android-mapview draw routes mylocationoverlay

我在onLocationChanged()时绘制路线时遇到问题。

所以我要做的是: 我在地图上有一个pin(基于carOverlayItem),MyLocationOverlay显示我当前的位置。我想在这两点之间划一条路。 因此,每当用户移动时(我们接收位置并触发MyLocationOverlay.onLocationChanged()方法),我就会从klm文件中获取Google的坐标,解析它并使用GeoPoint对象填充数组。在我尝试遍历该GeoPoint数组并将Overlays with Overwritten draw()方法添加到MapView

之后
public class GMapMyLocationOverlay extends MyLocationOverlay {

    private MapView mapView;
    private CarOverlayItem carOverlayItem = null;
    private GeoPoint routeNodes[];

    public GMapMyLocationOverlay(Context context, MapView mapView, CarOverlayItem carOverlayItem) {
        super(context, mapView);
        this.mapView = mapView;
        this.carOverlayItem = carOverlayItem;
    }

    @Override
    public void onLocationChanged(Location location) {
        // redraw route to the car point
        if (!carOverlayItem.isEmpty()) {
            GeoPoint fromLocation = new GeoPoint((int)(location.getLatitude() * 1e6), (int)(location.getLongitude() * 1e6));

            GMapRouteHttpRequest pointsRequest = new GMapRouteHttpRequest(fromLocation, carOverlayItem.getOverlayItem().getPoint());
            routeNodes = pointsRequest.getRoutePoints();

            // if the point is not set to be on the road, google can return empty points array
            // in this case we will be drawing straight line between car position and current 
            // user's position on map
            if (routeNodes != null && routeNodes.length > 0) {
                for (int i = 1; i < routeNodes.length; i ++) {
                    mapView.getOverlays().add(new GMapRouteOverlay(routeNodes[i-1], routeNodes[i]));
                }
            }
        }
        super.onLocationChanged(location);
    }
}

这是我的GMapRouteOverlay类

public class GMapRouteOverlay extends Overlay {

    private GeoPoint fromPoint;
    private GeoPoint toPoint;

    public GMapRouteOverlay(GeoPoint fromPoint, GeoPoint toPoint) {
        this.fromPoint = fromPoint;
        this.toPoint = toPoint;
    }

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        Projection projection = mapView.getProjection();

        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStrokeWidth(5);
        paint.setAntiAlias(true);

        Point from = new Point();
        projection.toPixels(fromPoint, from);

        Point to = new Point();
        projection.toPixels(toPoint, to);

        Path path = new Path();
        path.moveTo(from.x, from.y);
        path.lineTo(to.x, to.y);
        canvas.drawPath(path, paint);
        super.draw(canvas, mapView, shadow);
    }
}

我已经阅读了一些互联网,想出我需要在onLocationChanged()时填充routeNodes变量,然后调用mapView.invalidate()在onDraw()MapView方法中绘制路径,但遇到了一个问题我我不明白如何转移routeNodes变量和意图不是一个选项。

另外,可能是,带有onLocationChanged()方法的MyLocationOverlay正在非UI线程中运行,这就是为什么我无法在地图上绘制,但在这种情况下,我想,我应该得到一个错误,这是没有抛出。我很困惑,可以找到任何解决方案。

任何帮助都将不胜感激。

感谢。

3 个答案:

答案 0 :(得分:2)

在UI线程上使用网络数据时要小心,这在Android 4.0及更高版本中不起作用。

您需要使用AsyncTask

答案 1 :(得分:1)

实际上,我的版本也在运行,我注意到Google首先返回经度而且只返回纬度,因此我在解析klm文件时因为参数混乱而遇到问题。所以给我+1也是=)

答案 2 :(得分:0)

您可以通过调用类的静态方法(此类由MapActivity扩展)来更新mapview。

@Override
public void onLocationChanged(Location location) {
   super.onLocationChanged(location);
   MyMapClass.updateLocation(location);
}

在你班上这样做。

class MyMapClass extends MapActivity{
   private static MapView mapview;
   public void onCreate(){} //// your oncreate() method

   public static void updateLocation(Location location){
         // here you can get the location value using this location object now pass this value to draw the location or your current location
        // and then call mapview.invalidate()
   }
}