MapView 在触摸时添加图钉

发布于 2024-09-19 00:23:06 字数 3158 浏览 7 评论 0原文

我设法让地图显示在屏幕上。现在,用户将在地图上移动并按下它。按下一个点后,我需要在屏幕上按下的位置添加一个图钉。如果用户决定去另一个点,当按下所选点时,第一个图钉将消失,并且将在新位置上绘制一个新的图钉,

我这样做是这样的:

public class LocationSelectionActivity extends MapActivity 
{    
     GeoPoint p;
     List<Overlay> listOfOverlays;
     MapOverlay mapOverlay;

    private MapView mapView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.locationselection);

        mapView = (MapView) findViewById(R.id.mapView);
        mapView.setSatellite(false);

        final MapController mc = mapView.getController();
        String coordinates[] = {"46.540606", "22.454542"};

        double lat = Double.parseDouble(coordinates[0]);
        double lng = Double.parseDouble(coordinates[1]);

         p = new GeoPoint(
            (int) (lat * 1E6), 
            (int) (lng * 1E6));

        mc.animateTo(p);
        mc.setZoom(10); 


        ZoomControls zoomControls = (ZoomControls) findViewById(R.id.zoomcontrols);
        zoomControls.setOnZoomInClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                        mc.zoomIn();
                }
        });
        zoomControls.setOnZoomOutClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                        mc.zoomOut();
                }
        });

      //---Add a location marker---
        mapOverlay = new MapOverlay();
        listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);

        mapView.invalidate();
    }




   class MapOverlay extends com.google.android.maps.Overlay
    {
        @Override
        public boolean draw(Canvas canvas, MapView mapView, 
        boolean shadow, long when) 
        {
            super.draw(canvas, mapView, shadow);                   
            //---translate the GeoPoint to screen pixels---
            Point screenPts = new Point();
            mapView.getProjection().toPixels(p, screenPts);

            //---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.map_flag);            
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-48, null);

            return true;
        }

        @Override
        public boolean onTouchEvent(MotionEvent event, MapView mapView) 
        {   
            //---when user lifts his finger---
            if (event.getAction() == 1) {                
                GeoPoint p = mapView.getProjection().fromPixels(
                    (int) event.getX(),
                    (int) event.getY());
                    Toast.makeText(getBaseContext(), 
                        p.getLatitudeE6() / 1E6 + "," + 
                        p.getLongitudeE6() /1E6 , 
                        Toast.LENGTH_SHORT).show();

            }                            
            return false;
        }        

    } 

这将绘制初始图钉(map_flag资源)。单击地图时,我会看到带有坐标的 Toast...剩下要做的就是再次单击,删除旧的图钉并添加新的图钉。

但怎么办呢?

I managed to get the map shown on the screen. Now the user will move around the map and press on it. After pressing on a point I need to add a push pin on screen on the pressed location. If the user decides to go for another point, when pressing on the chosen point, the first pushpin would disapear and a new one will be drawn on the new location

I did like this:

public class LocationSelectionActivity extends MapActivity 
{    
     GeoPoint p;
     List<Overlay> listOfOverlays;
     MapOverlay mapOverlay;

    private MapView mapView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.locationselection);

        mapView = (MapView) findViewById(R.id.mapView);
        mapView.setSatellite(false);

        final MapController mc = mapView.getController();
        String coordinates[] = {"46.540606", "22.454542"};

        double lat = Double.parseDouble(coordinates[0]);
        double lng = Double.parseDouble(coordinates[1]);

         p = new GeoPoint(
            (int) (lat * 1E6), 
            (int) (lng * 1E6));

        mc.animateTo(p);
        mc.setZoom(10); 


        ZoomControls zoomControls = (ZoomControls) findViewById(R.id.zoomcontrols);
        zoomControls.setOnZoomInClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                        mc.zoomIn();
                }
        });
        zoomControls.setOnZoomOutClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                        mc.zoomOut();
                }
        });

      //---Add a location marker---
        mapOverlay = new MapOverlay();
        listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);

        mapView.invalidate();
    }




   class MapOverlay extends com.google.android.maps.Overlay
    {
        @Override
        public boolean draw(Canvas canvas, MapView mapView, 
        boolean shadow, long when) 
        {
            super.draw(canvas, mapView, shadow);                   
            //---translate the GeoPoint to screen pixels---
            Point screenPts = new Point();
            mapView.getProjection().toPixels(p, screenPts);

            //---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.map_flag);            
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-48, null);

            return true;
        }

        @Override
        public boolean onTouchEvent(MotionEvent event, MapView mapView) 
        {   
            //---when user lifts his finger---
            if (event.getAction() == 1) {                
                GeoPoint p = mapView.getProjection().fromPixels(
                    (int) event.getX(),
                    (int) event.getY());
                    Toast.makeText(getBaseContext(), 
                        p.getLatitudeE6() / 1E6 + "," + 
                        p.getLongitudeE6() /1E6 , 
                        Toast.LENGTH_SHORT).show();

            }                            
            return false;
        }        

    } 

This draws the initial pushpin (the map_flag resource). When clicking on the map I get the Toast with the coordonates... all is left to do is on a new click, erase the old pin and add a new one.

But how to do it ?

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

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

发布评论

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

评论(3

独自←快乐 2024-09-26 00:23:06

我会使用 ItemizedOverlay,而不是常规的 Overlay。然后,您需要做的就是更新 OverlayItem 对象集合并在 ItemizedOverlay 上调用 populate()

这是示例项目,显示了拖放功能使用 ItemizedOverlay 的图钉。

I would use an ItemizedOverlay, rather than a regular Overlay. Then, all you need to do is update your collection of OverlayItem objects and call populate() on the ItemizedOverlay.

Here is a sample project showing drag-and-drop of a pin using ItemizedOverlay.

陌生 2024-09-26 00:23:06

这段代码工作正常。

@Override 
      public boolean onTap(GeoPoint p, MapView mapView) {
            Log.d("tap event ", "tap called");
            mapOverlays = mapView.getOverlays();
            drawable  =getResources().getDrawable(R.drawable.marker);
            itemizedOverlay = new SitesOverlay(drawable);
            int lat=(int)p.getLatitudeE6();
            int lng=(int)p.getLongitudeE6();


            GeoPoint point = new GeoPoint(lat,lng);
            Log.d("tap event ", "tapcalled"+lat+""+lng);
            OverlayItem overlayitem = new OverlayItem(point, "", "");

            items.add(overlayitem);
            populate();
            Log.d("tap event ", "populated");
            //      mapOverlays.add(itemizedOverlay);   


            return true;
        }

This code is working fine .

@Override 
      public boolean onTap(GeoPoint p, MapView mapView) {
            Log.d("tap event ", "tap called");
            mapOverlays = mapView.getOverlays();
            drawable  =getResources().getDrawable(R.drawable.marker);
            itemizedOverlay = new SitesOverlay(drawable);
            int lat=(int)p.getLatitudeE6();
            int lng=(int)p.getLongitudeE6();


            GeoPoint point = new GeoPoint(lat,lng);
            Log.d("tap event ", "tapcalled"+lat+""+lng);
            OverlayItem overlayitem = new OverlayItem(point, "", "");

            items.add(overlayitem);
            populate();
            Log.d("tap event ", "populated");
            //      mapOverlays.add(itemizedOverlay);   


            return true;
        }
橘味果▽酱 2024-09-26 00:23:06
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) 
{   
    //---when user lifts his finger---
    if (event.getAction() == 1) {                
        p = mapView.getProjection().fromPixels(
            (int) event.getX(),
            (int) event.getY());
            Toast.makeText(getBaseContext(), 
                p.getLatitudeE6() / 1E6 + "," + 
                p.getLongitudeE6() /1E6 , 
                Toast.LENGTH_SHORT).show();
    }                            
    return false;
}        

这会起作用。使 OnTouchEvent 的 GeoPoint 公开...

@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) 
{   
    //---when user lifts his finger---
    if (event.getAction() == 1) {                
        p = mapView.getProjection().fromPixels(
            (int) event.getX(),
            (int) event.getY());
            Toast.makeText(getBaseContext(), 
                p.getLatitudeE6() / 1E6 + "," + 
                p.getLongitudeE6() /1E6 , 
                Toast.LENGTH_SHORT).show();
    }                            
    return false;
}        

This will work. Make OnTouchEvent's GeoPoint Public...

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