对于新的Maps API,我注意到当您按下地图上的“我的位置”图钉时,onMapClick()
和onMarkerClick()
都不会触发。
这告诉我,某个地方应该有一个听众可以听到按下我的位置触摸事件的这些事件,但我找不到任何内容。有人有解决方案吗?
我一直在考虑这样的解决方法:
禁用标准MyLocation
图标并创建一个功能类似于我的位置图钉的普通标记。
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));
}
但我对这种方法有两个问题:
onLocationListener
,我不知道实现自己的LocationSource
是否会破坏任何标准功能。找不到任何文档或源代码。答案 0 :(得分:1)
自此问题发布以来,OnMyLocationChangeListener
已添加到API中,使得变通方法(很多)的实施变得更加容易(即不再需要自定义LocationSource
)。
因此,您可以在“我的位置”点上方绘制Marker
(带有类似图标),以便接收相应的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 ... */
}
}
}
答案 1 :(得分:0)
“打破任何东西”是什么意思?你能详细说明吗?
另一种方法是,您也可以尝试从系统位置服务中侦听位置更改。请求在您的活动中提供位置服务:
locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
locatoinManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,2000,2f,this);
您的活动扩展了LocationListener
public void onLocationChange(Location location){
yourLocationPin.position(new LatLng(location.latitude, location.longitude));
}