Google地图错误:标记的位置在拖动后不会更新 [英] Google maps error: Marker's position is not updated after drag

查看:308
本文介绍了Google地图错误:标记的位置在拖动后不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一个标记设置为可拖动状态为true。但是,当我调用 marker.getPosition()时,我总是得到相同的位置,就像标记的位置在拖动结束后没有更新。

I set up a marker an set it's draggable state to true. But when I call marker.getPosition() I am always getting the same location, like marker's position is not updated after drag end.

mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map_place))
        .getMap();

LatLng MYLOCATION = new LatLng(Double.valueOf(myLat), Double.valueOf(myLng));
marker = new MarkerOptions()
    .position(MYLOCATION)
    .title(getString(R.string.map_title_my_location)).draggable(true);
mMap.addMarker(marker).showInfoWindow();
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(MYLOCATION, 18));
close.setOnClickListener(new OnClickListener() {

  @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub
    LatLng pos = marker.getPosition();
    Toast.makeText(activity, "Lat: " + pos.latitude + "; Long: " + pos.longitude, Toast.LENGTH_LONG).show();
  }
});

作为我定义的类增值服务:

as class vars I defined:

GoogleMap mMap;
MarkerOptions marker;

任何想法?!

Any idea?!

推荐答案

这是一个老问题,但是由于我自己也有同样的问题,下面是解决方案:您需要先为地图实例设置 OnMarkerDragListener mMap.setOnMarkerDragListener(...))来更新标记位置值。您并不需要在侦听器实现方法中执行任何操作(您可以在任何地方使用 marker.getPosition()),它们只需要存在。

This is an old question but since I had the same problem myself, here's the solution: you need to set the OnMarkerDragListener for the map instance first (mMap.setOnMarkerDragListener(...)) for the markers position value to get updated. You don't really need to do anything inside the listener implementation methods (you can use marker.getPosition() anywhere you like), they just have to exist.

更新

现在在 https://developers.google.com/maps/documentation/android-api/marker#marker_drag_events

This is also documented now in https://developers.google.com/maps/documentation/android-api/marker#marker_drag_events


您可以使用OnMarkerDragListener来侦听标记上的拖动事件。要在地图上设置此侦听器,请调用GoogleMap.setOnMarkerDragListener。要拖动标记,用户必须长按标记。当用户将手指从屏幕上移开时,标记将保持在该位置。拖动标记时,onMarkerDragStart(标记)首先被调用。当标记被拖动时,onMarkerDrag(标记)被不断调用。拖动onMarkerDragEnd(Marker)结束时被调用。您可以随时通过调用Marker.getPosition()来获取标记的位置。

You can use an OnMarkerDragListener to listen for drag events on a marker. To set this listener on the map, call GoogleMap.setOnMarkerDragListener. To drag a marker, a user must long press on the marker. When the user takes their finger off the screen, the marker will stay in that position. When a marker is dragged, onMarkerDragStart(Marker) is called initially. While the marker is being dragged, onMarkerDrag(Marker) is called constantly. At the end of the drag onMarkerDragEnd(Marker) is called. You can get the position of the marker at any time by calling Marker.getPosition().

这篇关于Google地图错误:标记的位置在拖动后不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆