从 onLocationChanged() 更新 MapView

发布于 2024-12-04 05:19:48 字数 3046 浏览 1 评论 0原文

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

所以我想做的是: 我在地图上有一个图钉(基于 carOverlayItem),而 MyLocationOverlay 显示了我当前的位置。我想在这两点之间画一条路线。 因此,每次用户移动时(我们接收位置并触发 MyLocationOverlay.onLocationChanged() 方法),我都会从 klm 文件中的 Google 获取坐标,解析它并用 GeoPoint 对象填充数组。在我尝试迭代 GeoPoint 数组并向 MapView 添加带有 Overwriting draw() 方法的 Overlays 之后

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 线程中运行,这就是为什么我无法在地图上绘制的原因,但在这种情况下,我认为我应该得到一个错误,但不会抛出该错误。我很困惑,可以找到任何解决方案。

任何帮助将不胜感激。

谢谢。

I have an issue drawing a route when onLocationChanged().

So what I'm trying to do is:
I have a pin (based on carOverlayItem) on the map and MyLocationOverlay showing my current position. I want to draw a route between those two points.
So, each time when user moves (we receive location and MyLocationOverlay.onLocationChanged() method is triggered), I'm fetching coordinates from Google in klm file, parsing it and filling an array with GeoPoint objects. After I'm trying to iterate through that GeoPoint array and add Overlays with Overwritten draw() method to 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);
    }
}

And here is my GMapRouteOverlay class

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);
    }
}

I've read some internet and came up with idea that I need to fill routeNodes variable when onLocationChanged() and then call mapView.invalidate() to draw a route in onDraw() MapView method, but faced an issue that I don't know how to transfer routeNodes variable and intents is not an option here as I understand.

Also, may be, that MyLocationOverlay with onLocationChanged() method are running in not UI thread and that is why I can't draw on the map, but in this case, I think, I should get an error, which is not thrown. I'm confused and can find any solution.

Any help would be appreciated.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

べ繥欢鉨o。 2024-12-11 05:19:48

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

您需要使用AsyncTask

Be careful when consuming Network data on UI thread, this doesn't work in android 4.0 and above.

You need to use AsyncTask

为你鎻心 2024-12-11 05:19:48

实际上,我的版本也可以工作,我注意到谷歌首先返回经度,然后才返回纬度,所以我遇到了问题,因为解析 klm 文件时参数混乱。所以我也+1 =)

Actually, my version is working too, I noticed that Google returns longitude first and only then latitude, so I had problem because of mess with parameters while parsing klm file. So +1 for me too =)

疾风者 2024-12-11 05:19:48

您可以通过调用类的静态方法(该类由 MapActivity 扩展)来更新地图视图,如下所示。

@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()
   }
}

you can update the mapview by calling the static method of class(this class was extends by MapActivity) like this way.

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

and in the your class do this way.

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()
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文