Android 标记如何实现拖放?

发布于 2024-09-26 15:05:14 字数 181 浏览 1 评论 0原文

你好? 我正在 Android 中开发 MapView 应用程序。我有三个标记,我希望稍后能够使用 Google Map API getlocation-function。为了尝试一下,我想使用拖放功能移动标记,然后检查位置。

任何人都可以通过拖放操作来处理 Android 标记,或者知道如何开始解决它?

/AK

Hi?
I am working on a MapView app in Android. I have three markers that I want to be able to use the Google Map API getlocation-function on, later on. In order to try it out I would like to move the marker with a drag and drop-function, and then check the location.

Anyone who has gotten a drag and drop to work on an android marker, or know a way to start figuring it out?

/AK

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

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

发布评论

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

评论(6

ま柒月 2024-10-03 15:05:14

实施 Google Maps Android API v2,请参阅:https://developers.google.com/maps/ Documentation/android/ 并在 GoogleMap 对象上设置 setOnMarkerDragListener。例如:

map.setOnMarkerDragListener(new OnMarkerDragListener() {
        @Override
        public void onMarkerDragStart(Marker arg0) {
            // TODO Auto-generated method stub
            Log.d("System out", "onMarkerDragStart..."+arg0.getPosition().latitude+"..."+arg0.getPosition().longitude);
        }

        @SuppressWarnings("unchecked")
        @Override
        public void onMarkerDragEnd(Marker arg0) {
            // TODO Auto-generated method stub
            Log.d("System out", "onMarkerDragEnd..."+arg0.getPosition().latitude+"..."+arg0.getPosition().longitude);

            map.animateCamera(CameraUpdateFactory.newLatLng(arg0.getPosition()));
        }

        @Override
        public void onMarkerDrag(Marker arg0) {
            // TODO Auto-generated method stub
            Log.i("System out", "onMarkerDrag...");
        }
    });

//Don't forget to Set draggable(true) to marker, if this not set marker does not drag.

map.addMarker(new MarkerOptions()
    .position(crntLocationLatLng)
    .icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_my_location))
    .draggable(true));

Implement Google Maps Android API v2, refer this: https://developers.google.com/maps/documentation/android/ and set on GoogleMap object setOnMarkerDragListener. For Ex:

map.setOnMarkerDragListener(new OnMarkerDragListener() {
        @Override
        public void onMarkerDragStart(Marker arg0) {
            // TODO Auto-generated method stub
            Log.d("System out", "onMarkerDragStart..."+arg0.getPosition().latitude+"..."+arg0.getPosition().longitude);
        }

        @SuppressWarnings("unchecked")
        @Override
        public void onMarkerDragEnd(Marker arg0) {
            // TODO Auto-generated method stub
            Log.d("System out", "onMarkerDragEnd..."+arg0.getPosition().latitude+"..."+arg0.getPosition().longitude);

            map.animateCamera(CameraUpdateFactory.newLatLng(arg0.getPosition()));
        }

        @Override
        public void onMarkerDrag(Marker arg0) {
            // TODO Auto-generated method stub
            Log.i("System out", "onMarkerDrag...");
        }
    });

//Don't forget to Set draggable(true) to marker, if this not set marker does not drag.

map.addMarker(new MarkerOptions()
    .position(crntLocationLatLng)
    .icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_my_location))
    .draggable(true));
稳稳的幸福 2024-10-03 15:05:14

这是来自 我的一本书展示了 Android 中 Google 地图上标记的拖放移动。

简而言之,它使用 onTouchEvent() 来检测用户何时触摸并保持手指靠近标记。然后,它从叠加层中删除标记,但使用 RelativeLayout 将相同的图像放置在地图顶部。然后,在“移动”触摸事件中,图像会移动(比强制重绘整个覆盖层更快)。当手指抬起时,图像将被删除,但标记将被放回覆盖层中的新位置。

Here is a sample project from one of my books showing drag-and-drop movement of markers on a Google Map in Android.

In a nutshell, it uses onTouchEvent() to detect when the user touches and holds their finger near a marker. It then removes the marker from the overlay, but puts the same image over top of the map using RelativeLayout. Then, on "move" touch events, the image is moved (faster than forcing the whole overlay to redraw). When the finger is lifted, the image is removed, but the marker is put back in the overlay at the new spot.

完美的未来在梦里 2024-10-03 15:05:14

对于地图V2。使用谷歌地图事件。不要自己写。

        mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                mVisitingMarker.setPosition(latLng);
                mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
            }
        });

        mMap.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
            @Override
            public void onMarkerDragStart(Marker arg0) {
            }

            @SuppressWarnings("unchecked")
            @Override
            public void onMarkerDragEnd(Marker arg0) {
               Log.d("System out", "onMarkerDragEnd...");
                mMap.animateCamera(CameraUpdateFactory.newLatLng(arg0.getPosition()));
            }

            @Override
            public void onMarkerDrag(Marker arg0) {
            }
        });

For MapsV2. Use the google map events. Don't write your own.

        mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                mVisitingMarker.setPosition(latLng);
                mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
            }
        });

        mMap.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
            @Override
            public void onMarkerDragStart(Marker arg0) {
            }

            @SuppressWarnings("unchecked")
            @Override
            public void onMarkerDragEnd(Marker arg0) {
               Log.d("System out", "onMarkerDragEnd...");
                mMap.animateCamera(CameraUpdateFactory.newLatLng(arg0.getPosition()));
            }

            @Override
            public void onMarkerDrag(Marker arg0) {
            }
        });
山田美奈子 2024-10-03 15:05:14

下面是《Kotlin Lover》的解答!这非常非常简单!正如你在下面看到的。

   googleMap.addMarker(
        MarkerOptions()
            .position(latLatLng).draggable(true)
    )

下面是获取结束拖动位置的liner代码

   mMap.setOnMarkerDragListener(object : GoogleMap.OnMarkerDragListener {
        override fun onMarkerDragStart(arg0: Marker) {

        }

        override fun onMarkerDragEnd(arg0: Marker) {
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(arg0.position, 1.0f))
            val message = arg0.position.latitude.toString() + "" + arg0.position.longitude.toString()
            Log.d(TAG + "_END", message)
        }

        override fun onMarkerDrag(arg0: Marker?) {
            val message = arg0!!.position.latitude.toString() + "" + arg0.position.longitude.toString()
            Log.d(TAG + "_DRAG", message)
        }
    })

Below is the answer to Kotlin Lover! It's quite very simple! As you can see below.

   googleMap.addMarker(
        MarkerOptions()
            .position(latLatLng).draggable(true)
    )

Below is the liner code for getting the end dragging location

   mMap.setOnMarkerDragListener(object : GoogleMap.OnMarkerDragListener {
        override fun onMarkerDragStart(arg0: Marker) {

        }

        override fun onMarkerDragEnd(arg0: Marker) {
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(arg0.position, 1.0f))
            val message = arg0.position.latitude.toString() + "" + arg0.position.longitude.toString()
            Log.d(TAG + "_END", message)
        }

        override fun onMarkerDrag(arg0: Marker?) {
            val message = arg0!!.position.latitude.toString() + "" + arg0.position.longitude.toString()
            Log.d(TAG + "_DRAG", message)
        }
    })
游魂 2024-10-03 15:05:14

我发现了对 CommonsWare 拖放出色功能的一些优化。

我正在用地图上的标记进行一些精确的测量,我希望我的标记准确地位于我触摸并抬起手指的位置,以便测量非常精确。

在 CommonsWare 的原版上,如果您触摸标记“附近”,标记不会到达该确切点,而是会随着手指的移动而移动。
我需要标记出现在我的手指下方的 ACTION_DOWN、ACTION_MOVE 和 ACTION_UP 上。

这是代码,如果有人需要的话。我要感谢CommonsWare提供了这个功能,这真是一个好主意。

这是我修改的部分代码。

我的MapView是mapa;

        @Override
    public boolean onTouchEvent(MotionEvent event, MapView mapView) {

        final int action=event.getAction();
        final int x=(int)event.getX();
        final int y=(int)event.getY();
        boolean result=false;

              if (action==MotionEvent.ACTION_DOWN) {
                for (OverlayItem item : mOverlays) {
                  Point p=new Point(0,0);

                  mapa.getProjection().toPixels(item.getPoint(), p);

                      //I maintain the hitTest's bounds so you can still
                      //press near the marker
                  if (hitTest(item, marker, x-p.x, y-p.y)) {
                    result=true;

                    inDrag=item;

                    mOverlays.remove(inDrag);
                    populate();

                        //Instead of using the DragImageOffSet and DragTouchOffSet
                        //I use the x and y coordenates from the Point
                    setDragImagePosition(x, y);

                    dragImage.setVisibility(View.VISIBLE);

                    break;
                  }
                }
              }
              else if (action==MotionEvent.ACTION_MOVE && inDrag!=null) {
                setDragImagePosition(x, y);

                result=true;
              }
              else if (action==MotionEvent.ACTION_UP && inDrag!=null) {
                dragImage.setVisibility(View.GONE);

                    //I get the geopoint without using any OffSet, directly with the 
                    //Point coordenates
                GeoPoint pt=mapa.getProjection().fromPixels(x,y);

                OverlayItem toDrop=new OverlayItem(pt, inDrag.getTitle(),
                                                   inDrag.getSnippet());
                orig = inDrag.getMarker(0);

                    //In my case I had down heading Arrows as markers, so I wanted my 
                    //bounds to be at the center bottom
                toDrop.setMarker(boundCenterBottom(orig));

                mOverlays.add(toDrop);
                populate();

                inDrag=null;
                result=true;
              }


           return(result || super.onTouchEvent(event, mapView));


    }

    private void setDragImagePosition(int x, int y) {
      RelativeLayout.LayoutParams lp=
        (RelativeLayout.LayoutParams)dragImage.getLayoutParams();

          //Instead of using OffSet I use the Point coordenates.
          //I want my arrow to appear pointing to the point I am moving, so 
          //I set the margins as the Point coordenates minus the Height and half the
          //width of my arrow.
      lp.setMargins(x-(dragImage.getWidth()/2),y-dragImage.getHeight(), 0, 0);
      dragImage.setLayoutParams(lp);
    }

这样,您的箭头就会出现在您按下的位置,而不是箭头从原来的位置移动。

I found a little optimization for the CommonsWare drag and drop's brilliant function.

I was doing some precise meashurements with markers on my map, and I wanted my marker to be exactly on the spot I touched and lifted my finger so the meashurment was exactly precise.

On the original from CommonsWare if you touch "near" the marker, the marker does not go to that exact point, but moves along with your finger's movement.
I needed the marker to appear just below my finger on the ACTION_DOWN, ACTION_MOVE and ACTION_UP.

Here is the code if someone need's it. I have to thank CommonsWare to make this function, it's a really good idea.

This is the part of the code I modified.

My MapView is mapa;

        @Override
    public boolean onTouchEvent(MotionEvent event, MapView mapView) {

        final int action=event.getAction();
        final int x=(int)event.getX();
        final int y=(int)event.getY();
        boolean result=false;

              if (action==MotionEvent.ACTION_DOWN) {
                for (OverlayItem item : mOverlays) {
                  Point p=new Point(0,0);

                  mapa.getProjection().toPixels(item.getPoint(), p);

                      //I maintain the hitTest's bounds so you can still
                      //press near the marker
                  if (hitTest(item, marker, x-p.x, y-p.y)) {
                    result=true;

                    inDrag=item;

                    mOverlays.remove(inDrag);
                    populate();

                        //Instead of using the DragImageOffSet and DragTouchOffSet
                        //I use the x and y coordenates from the Point
                    setDragImagePosition(x, y);

                    dragImage.setVisibility(View.VISIBLE);

                    break;
                  }
                }
              }
              else if (action==MotionEvent.ACTION_MOVE && inDrag!=null) {
                setDragImagePosition(x, y);

                result=true;
              }
              else if (action==MotionEvent.ACTION_UP && inDrag!=null) {
                dragImage.setVisibility(View.GONE);

                    //I get the geopoint without using any OffSet, directly with the 
                    //Point coordenates
                GeoPoint pt=mapa.getProjection().fromPixels(x,y);

                OverlayItem toDrop=new OverlayItem(pt, inDrag.getTitle(),
                                                   inDrag.getSnippet());
                orig = inDrag.getMarker(0);

                    //In my case I had down heading Arrows as markers, so I wanted my 
                    //bounds to be at the center bottom
                toDrop.setMarker(boundCenterBottom(orig));

                mOverlays.add(toDrop);
                populate();

                inDrag=null;
                result=true;
              }


           return(result || super.onTouchEvent(event, mapView));


    }

    private void setDragImagePosition(int x, int y) {
      RelativeLayout.LayoutParams lp=
        (RelativeLayout.LayoutParams)dragImage.getLayoutParams();

          //Instead of using OffSet I use the Point coordenates.
          //I want my arrow to appear pointing to the point I am moving, so 
          //I set the margins as the Point coordenates minus the Height and half the
          //width of my arrow.
      lp.setMargins(x-(dragImage.getWidth()/2),y-dragImage.getHeight(), 0, 0);
      dragImage.setLayoutParams(lp);
    }

With this you get your arrow appearing where you press with your, instead of the arrow moving from where it was.

守护在此方 2024-10-03 15:05:14

此处 是Android中Mapview中拖放图钉的完整代码

Here is Complete code of Drag And Drop pin in Mapview in Android

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