你好?我正在开发 Android 中的 MapView 应用程序。我有三个标记,我希望稍后能够使用 Google Map API getlocation-function。为了尝试一下,我想使用拖放功能移动标记,然后检查位置。
任何已经通过拖放操作在 android 标记上工作的人,或者知道如何开始弄清楚它的人?
/AK
你好?我正在开发 Android 中的 MapView 应用程序。我有三个标记,我希望稍后能够使用 Google Map API getlocation-function。为了尝试一下,我想使用拖放功能移动标记,然后检查位置。
任何已经通过拖放操作在 android 标记上工作的人,或者知道如何开始弄清楚它的人?
/AK
实现 Google Maps Android API v2,参考:https ://developers.google.com/maps/documentation/android/并在 GoogleMap 对象 setOnMarkerDragListener 上进行设置。例如:
map.setOnMarkerDragListener(new OnMarkerDragListener() {
@Override
public void onMarkerDragStart(Marker arg0) {
// TODO Auto-generated method stub
Log.d("System out", "onMarkerDragStart..."+arg0.getPosition().latitude+"..."+arg0.getPosition().longitude);
}
@SuppressWarnings("unchecked")
@Override
public void onMarkerDragEnd(Marker arg0) {
// TODO Auto-generated method stub
Log.d("System out", "onMarkerDragEnd..."+arg0.getPosition().latitude+"..."+arg0.getPosition().longitude);
map.animateCamera(CameraUpdateFactory.newLatLng(arg0.getPosition()));
}
@Override
public void onMarkerDrag(Marker arg0) {
// TODO Auto-generated method stub
Log.i("System out", "onMarkerDrag...");
}
});
//Don't forget to Set draggable(true) to marker, if this not set marker does not drag.
map.addMarker(new MarkerOptions()
.position(crntLocationLatLng)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_my_location))
.draggable(true));
对于 MapsV2。使用谷歌地图事件。不要自己写。
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
mVisitingMarker.setPosition(latLng);
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
});
mMap.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
@Override
public void onMarkerDragStart(Marker arg0) {
}
@SuppressWarnings("unchecked")
@Override
public void onMarkerDragEnd(Marker arg0) {
Log.d("System out", "onMarkerDragEnd...");
mMap.animateCamera(CameraUpdateFactory.newLatLng(arg0.getPosition()));
}
@Override
public void onMarkerDrag(Marker arg0) {
}
});
下面是对 Kotlin Lover 的回答!这非常简单!正如你在下面看到的。
googleMap.addMarker(
MarkerOptions()
.position(latLatLng).draggable(true)
)
下面是获取最终拖动位置的衬垫代码
mMap.setOnMarkerDragListener(object : GoogleMap.OnMarkerDragListener {
override fun onMarkerDragStart(arg0: Marker) {
}
override fun onMarkerDragEnd(arg0: Marker) {
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(arg0.position, 1.0f))
val message = arg0.position.latitude.toString() + "" + arg0.position.longitude.toString()
Log.d(TAG + "_END", message)
}
override fun onMarkerDrag(arg0: Marker?) {
val message = arg0!!.position.latitude.toString() + "" + arg0.position.longitude.toString()
Log.d(TAG + "_DRAG", message)
}
})
我发现对 CommonsWare 拖放的出色功能进行了一些优化。
我正在用地图上的标记进行一些精确的测量,我希望我的标记准确地位于我触摸并抬起手指的位置,这样测量就非常精确了。
在 CommonsWare 的原件上,如果您触摸“靠近”标记,则标记不会到达那个确切的点,而是随着您手指的移动而移动。我需要标记出现在我手指下方的 ACTION_DOWN、ACTION_MOVE 和 ACTION_UP 上。
如果有人需要,这是代码。我必须感谢 CommonsWare 使这个功能,这是一个非常好的主意。
这是我修改的代码的一部分。
我的 MapView 是 mapa;
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
final int action=event.getAction();
final int x=(int)event.getX();
final int y=(int)event.getY();
boolean result=false;
if (action==MotionEvent.ACTION_DOWN) {
for (OverlayItem item : mOverlays) {
Point p=new Point(0,0);
mapa.getProjection().toPixels(item.getPoint(), p);
//I maintain the hitTest's bounds so you can still
//press near the marker
if (hitTest(item, marker, x-p.x, y-p.y)) {
result=true;
inDrag=item;
mOverlays.remove(inDrag);
populate();
//Instead of using the DragImageOffSet and DragTouchOffSet
//I use the x and y coordenates from the Point
setDragImagePosition(x, y);
dragImage.setVisibility(View.VISIBLE);
break;
}
}
}
else if (action==MotionEvent.ACTION_MOVE && inDrag!=null) {
setDragImagePosition(x, y);
result=true;
}
else if (action==MotionEvent.ACTION_UP && inDrag!=null) {
dragImage.setVisibility(View.GONE);
//I get the geopoint without using any OffSet, directly with the
//Point coordenates
GeoPoint pt=mapa.getProjection().fromPixels(x,y);
OverlayItem toDrop=new OverlayItem(pt, inDrag.getTitle(),
inDrag.getSnippet());
orig = inDrag.getMarker(0);
//In my case I had down heading Arrows as markers, so I wanted my
//bounds to be at the center bottom
toDrop.setMarker(boundCenterBottom(orig));
mOverlays.add(toDrop);
populate();
inDrag=null;
result=true;
}
return(result || super.onTouchEvent(event, mapView));
}
private void setDragImagePosition(int x, int y) {
RelativeLayout.LayoutParams lp=
(RelativeLayout.LayoutParams)dragImage.getLayoutParams();
//Instead of using OffSet I use the Point coordenates.
//I want my arrow to appear pointing to the point I am moving, so
//I set the margins as the Point coordenates minus the Height and half the
//width of my arrow.
lp.setMargins(x-(dragImage.getWidth()/2),y-dragImage.getHeight(), 0, 0);
dragImage.setLayoutParams(lp);
}
这样,您的箭头就会出现在您按下的位置,而不是箭头从原来的位置移动。
这是Android Mapview中拖放引脚的完整代码