【问题标题】:Limit user to one marker on map将用户限制为地图上的一个标记
【发布时间】:2017-07-01 02:08:44
【问题描述】:

我在我的 Android 应用中使用 Google Maps V2 API。我已经包含了一个方法,它允许用户使用 onMapLongClick 放置一个可拖动的标记。我还计划包含将其与 SearchView 小部件和 Google Places 相关联的功能。在这方面,我想限制用户只能在地图上放置一个标记。如果他们再次调用任一方法来放置标记,我希望现有标记将其位置更新为新输入。我已经对此进行了几天的研究,但对于将用户可以放置的标记数量限制为 1,我无法提出任何意见。关于如何实现这一点的任何想法?这是我现在使用的方法...

//onMapLongClick, create marker
@Override    
public void onMapLongClick(LatLng point) {
    map.addMarker(new MarkerOptions()
    .position(point)
    .title("Your Destination")           
    .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))
    .draggable(true));
}

【问题讨论】:

  • 这很容易。就像第一个用户现在在 onMapClick() 添加一个标记,如果用户 onMapclick() 再次将您的旧点更新为新点

标签: android google-maps-android-api-2


【解决方案1】:

我没有在android中使用过地图,但是尝试一下这样的东西,只是提供一个想法

Marker m; //reference to the marker 

   //onMapLongClick, if marker exists update its position, else create marker
@Override
public void onMapLongClick(LatLng point) {
  if (m) { //if marker exists (not null or whatever)
    m.setPosition(point);
  } else {
    m = map.addMarker(new MarkerOptions()
        .position(point)
        .title("Your Destination")
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))
        .draggable(true));
  }
}

【讨论】:

  • 哇!你真的超越了这个答案。我将为彻底性投票+1,一旦我尝试过,我会跟进你。我可以使用“点”作为使用 LatLng 从用户的 onMapLongClick 或 Google Places 更新的字段,对吗?
  • 效果很好!哇!谢谢你,让我觉得自己不能自己想出来有点笨。我会安静地坐在这里喝我的苏格兰威士忌。
猜你喜欢
  • 2020-12-31
  • 1970-01-01
  • 1970-01-01
  • 2015-09-19
  • 2014-08-27
  • 1970-01-01
  • 2011-05-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多