2

我创建了一个 android 应用程序来在谷歌地图 v2 上绘制自由形状。该应用的想法是我结合了两个应用程序,一个是绘制自由形状,另一个是普通的谷歌地图v2应用程序。

这个链接是我关于这个应用程序的最后一个问题,它包含代码

该应用程序很适合我,但现在我遇到了一个新问题。我的问题是,当我在地图上的特定位置绘制一条线并将控件转换为地图并拖动它时,我发现该线保持在视图中的位置并且地图移动到该线下方,这导致该线在另一个位置,而不是我想要的位置。

有什么方法可以使线条在我绘制的位置保持稳定,当我拖动地图时,线条与它的位置一起拖动?

希望有人明白我的意思。

4

1 回答 1

1

例如,如果您正在使用画布在地图视图上绘制线条,那么您需要获取起点和终点的 x、y 点。

然后通过以下代码,您可以将 x,y 点更改为纬度和经度。

public boolean onTouchEvent(MotionEvent event)
{
    int X = (int)event.getX();          
    int Y = (int)event.getY();

    GeoPoint geoPoint = mapView.getProjection().fromPixels(X, Y);
}

然后像这样在你的 mapvierw 上注册监听器。

map.setOnCameraChangeListener(new OnCameraChangeListener() {

    @Override
    public void onCameraChange(CameraPosition arg0) {
        // Move camera.
     Here remove your view from screen and then get lat long of visible region by       passing x,y points of 4 regions in `mapView.getProjection().fromPixels(x,y)` and then check if latitude and longitude of your line within range if yes then drawline by following code.

     float pisteX;
     float pisteY;
     Projection projection = this.mapView.getProjection(); 
     Point pt = new Point();
     GeoPoint gie = new GeoPoint(latitude,longitude);
     Rect rec = mapView.getScreenRect(new Rect());
     projection.toPixels(gie, pt);
     pisteX = pt.x-rec.left; // car X screen coord
     pisteY = pt.y-rec.top; // car Y screen coord

     Now draw line between this two (x,y) points.
    }


});

希望我能让你清楚,你能明白我想说什么。

于 2013-07-31T11:37:28.790 回答