In my app, I want the users to provide the latlng they want to go in a Google Map. So, firstly I have created an activity with a button for user to open a google map:
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();**
}
};
Then, I have this GM001MapForAddress to initialize the google map, intended to have a draggable marker:
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);
}
};
I have my clickmap listener work but the dragmarker listener never works. I have worked on this for the entire weekend (I am in Asia). So, hopefully somebody can give me a hand on this.
Thanks in advance.
To enable the drag mode you need to long click on the marker, and then drag while holding.
If the marker is small it can be pretty hard clicking at the right point, if you have clumsy fingers like me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With