在我的應用程序中,我希望用戶能夠提供他們想要在Google Map中使用的latlng。所以,首先,我已經創建了一個活動有一個按鈕,用戶打開谷歌地圖:標記在Google Maps API v2上不可拖動
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偵聽器工作,但dragmarker偵聽器從不工作。整個週末我都在爲此工作(我在亞洲)。所以,希望有人能夠幫助我。
在此先感謝。
你不能拖動標記,或者你只是不會在'dragmarker'中獲得回調? – Simas 2014-08-31 06:12:56
嗨,我不能拖動標記 – kwytse 2014-08-31 08:03:16