Draw over google map v2 android

风格不统一 提交于 2019-12-12 17:27:54

问题


I created an android app to draw free shapes over google map v2. The idea of the app is that I combines two apps, one is to draw free shapes and the other is normal google map v2 app.

This link is my last question about this app and it contains the code

The app works well with me but now I have a new problem. My problem is that when I draw a line over a specific location on the map and convert control to map and drag it, I found that the line keep in its place in the view and the map moves under the line and this leads the line to be in another location not the location that I want.

Is there are any way to make the line to be steady in the location I draw in it and when I drag the map the line dragged with its location?

Hope anyone got my mean.


回答1:


For example if you are drawing line on your mapview using canvas then you need to get x,y points of start and end point.

Then by following code you can change that x,y points into latitude and longitude.

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

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

Then resgister listener on your mapvierw like this.

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.
    }


});

Hope I can make you clear and you can understand what I want to say.



来源:https://stackoverflow.com/questions/17968390/draw-over-google-map-v2-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!