在我的应用中,我希望用户在Google地图中提供他们想要的内容。所以,首先我创建了一个带有按钮的活动,供用户打开谷歌地图:
private Button.OnClickListener opengmap = new Button.OnClickListener() {
public void onClick(View v) {
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
Toast.makeText(v.getContext(), v.getContext().getString(R.string.T1001_NoGPSDetection), Toast.LENGTH_LONG).show();
} else {
strprovider = locationManager.getBestProvider(criteria, false);
location = locationManager.getLastKnownLocation(strprovider);
}
**gmap = new GM001MapForAddress(v.getContext(), location.getLatitude(), location.getLongitude(), R.id.map);
gmap.initialize();**
}
};
然后,我有这个GM001MapForAddress来初始化谷歌地图,打算有一个可拖动的标记:
public class GM001MapForAddress {
public Context context;
public GoogleMap googleMap;
public MarkerOptions markeroption;
public Marker marker;
public CameraUpdate center;
public Double latitude, longitude;
public Integer id;
public Activity activity;
public GM001MapForAddress(Context context, Double latitude, Double longitude, Integer id) {
this.context = context;
this.latitude = latitude;
this.longitude = longitude;
this.id = id;
}
public void initialize() {
activity = (Activity) context;
googleMap = ((MapFragment) activity.getFragmentManager().findFragmentById(id)).getMap();
googleMap.setOnMapClickListener(clickmap);
googleMap.setOnMarkerDragListener(dragmarker);
//googleMap.setMyLocationEnabled(true);
LatLng latlng = new LatLng(latitude, longitude);
marker = googleMap.addMarker(new MarkerOptions().position(latlng).draggable(true));
if (marker.isDraggable()) {
System.out.println("HAHA " + latlng);
}
marker.setDraggable(true);
googleMap.setOnMarkerDragListener(dragmarker);
center= CameraUpdateFactory.newLatLng(new LatLng(latitude, longitude));
CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
googleMap.moveCamera(center);
googleMap.animateCamera(zoom);
googleMap.getFocusedBuilding();
}
public OnMarkerDragListener dragmarker = new OnMarkerDragListener() {
@Override
public void onMarkerDrag(Marker marker) {
// TODO Auto-generated method stub
System.out.println("here1!!");
}
@Override
public void onMarkerDragEnd(Marker arg0) {
// TODO Auto-generated method stub
System.out.println("here2!!");
LatLng dragPosition = arg0.getPosition();
double dragLat = dragPosition.latitude;
double dragLong = dragPosition.longitude;
System.out.println("halatlng: " + dragLat + " " + dragLong);
Log.i("info", "on drag end :" + dragLat + " dragLong :" + dragLong);
arg0.setPosition(dragPosition);
googleMap.addMarker(new MarkerOptions().position(dragPosition).draggable(true));
}
@Override
public void onMarkerDragStart(Marker marker) {
// TODO Auto-generated method stub
System.out.println("here3!!");
}
};
public OnMapClickListener clickmap = new OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
// TODO Auto-generated method stub
googleMap.animateCamera(CameraUpdateFactory.newLatLng(point));
System.out.println("latdflng: " + point);
}
};
我的clickmap监听器工作正常,但拖拉机监听器永远不会工作。我整个周末都在研究这个问题(我在亚洲)。所以,希望有人可以帮我解决这个问题。
提前致谢。
答案 0 :(得分:10)
要启用拖动模式,您需要长按标记,然后按住拖动。
如果标记很小,如果你的手指像我一样笨拙,可能很难点击正确的点。