【发布时间】:2014-08-20 21:38:27
【问题描述】:
在我的应用中,我会自动检测用户的当前位置并将地图以标记为中心。
我想让用户能够点击地图上的其他地方,并让标记出现在他们点击的位置,并将纬度/经度更新到该新位置。
我该怎么做
【问题讨论】:
标签: android google-maps
在我的应用中,我会自动检测用户的当前位置并将地图以标记为中心。
我想让用户能够点击地图上的其他地方,并让标记出现在他们点击的位置,并将纬度/经度更新到该新位置。
我该怎么做
【问题讨论】:
标签: android google-maps
试试这个
Marker marker;
GoogleMap mMap;
mMap.setOnMapClickListener(new OnMapClickListener() {
@Override
public void onMapClick(LatLng latlng) {
// TODO Auto-generated method stub
if (marker != null) {
marker.remove();
}
marker = mMap.addMarker(new MarkerOptions()
.position(latlng)
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
System.out.println(latlng);
}
});
【讨论】:
要使用当前位置添加标记,您必须实现 LocationListener
使用以下代码,您可以添加带有您的位置的标记并移动相机:
public void onLocationChanged(Location location) {
map.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude()))
.title("My Location"));
/* ..and Animate camera to center on that location !
* (the reason for we created this custom Location Source !) */
map.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude())));
}
为了在用户点击地图时添加制造商,您可以使用OnMapLongClickListener
@Override
public void onMapLongClick(LatLng point) {
mMap.addMarker(new MarkerOptions()
.position(point)
.snippet(""));
}
【讨论】: