gpt4 book ai didi

android - 在 Google Maps API v2 中收听“我的位置”图标点击事件

转载 作者:太空狗 更新时间:2023-10-29 14:23:42 25 4
gpt4 key购买 nike

对于新的 Maps API,我注意到 onMapClick() 都没有也不onMarkerClick()当您按下 map 上的“我的位置”图钉时触发。

这向我表明,在按下“我的位置”触摸事件的地方应该有一个监听器监听这些事件,但我找不到任何监听器。有人对此有解决方案吗?

我一直在考虑这样的解决方法:

禁用标准的 MyLocation 图标并创建一个功能类似于 My Location pin 的普通标记。

myLocationPin = mMap.addMarker(new MarkerOptions()
.title("My Location")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.mylocation)));

并实现LocationSource.OnLocationChangedListener:

public void onLocationChanged (Location location) {
myLocationPin.position(new LatLng(location.latitude, location.longitude));
}

但是这种方法有两个问题:

  1. 我不知道如何关闭标记
  2. 我不知道如果我替换默认的 LocationSource,实现我自己的 onLocationListener 是否会破坏任何标准功能。在上面找不到任何文档或源代码。

最佳答案

由于发布了这个问题,OnMyLocationChangeListener 被添加到 API 中,使解决方法的实现(很多)更容易(即不需要自定义 LocationSource不再)。

因此,您可以在“我的位置”点顶部绘制一个标记(具有类似的图标),以便接收相应的onMarkerClick() 回调。

public class DemoMapFragment extends SupportMapFragment implements OnMyLocationChangeListener, OnMarkerClickListener { 

// Note that 'mMap' may be null if the Google Play services APK is not available.
private GoogleMap mMap;
private Marker myLocationMarker;
private static BitmapDescriptor markerIconBitmapDescriptor;
/* ... */

@Override
public void onResume() {
super.onResume();
setUpMapIfNeeded(); // Get a reference to the map
mMap.setMyLocationEnabled(true); // Enable the my-location layer
mMap.setOnMyLocationChangeListener(this);
mMap.setOnMarkerClickListener(this);
}

private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
mMap = getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
// The Map is verified. It is now safe to manipulate the map:

// Load custom marker icon
markerIconBitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.my_location_dot_icon);

// When the map is first loaded we need to add our marker on top of My Location dot
myLocationMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(mMap.getMyLocation().getLatitude(),mMap.getMyLocation().getLongitude()))
.icon(markerIconBitmapDescriptor));

// Set default zoom
mMap.moveCamera(CameraUpdateFactory.zoomTo(15f));
}
}
}

@Override
public void onMyLocationChange(Location location) {
// Remove the old marker object
myLocationMarker.remove();

// Add a new marker object at the new (My Location dot) location
myLocationMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(location().getLatitude(),location().getLongitude()))
.icon(markerIconBitmapDescriptor));
}

@Override
public boolean onMarkerClick(Marker marker) {
if (marker.equals(myLocationMarker)) {
/* My Location dot callback ... */
}
}
}

关于android - 在 Google Maps API v2 中收听“我的位置”图标点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14383498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com