本文介绍了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.

更新

现在在

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

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

08-15 16:06