Now I can show the map and markers that are defined in the program, but I need to add markers to touched location.
I think I know the way to do this, which is:
1 To set something like onTouchEventListener to the MapView;
2 Get the position information from Listener;
3 Set the marker according to the position information.
But I am too new to this to find out how to write code to achieve it, especially the step 1. I mean I don't understand how to use the solution like Add marker on touched location using google map in Android .My problem is more fundamental. I dont know how to set eventListener for my MapView and which eventListener should I use. https://developer.mapquest.com/content/mobile/android/documentation/api/com/mapquest/android/maps/Overlay.OverlayTouchEventListener.html This OverlayTouchEventListener seems to be the one but cant find an useful guide for it.
So could some one tell me how to do this in detail?
Eg.How to set the listener and which listener to choose?
Try this...
1. Create interface MyGeoPointListener.java
public interface MyGeoPointListener {
public void GetGeoPoint(GeoPoint geopoint);
}
2. Create Overlay class:
import com.mapquest.android.maps.ItemizedOverlay;
import com.mapquest.android.maps.OverlayItem;
class DynamicMarkerOverlay extends ItemizedOverlay<OverlayItem> {
private boolean isPinch = false;
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private MyGeoPointListener geoPointListener;
public DynamicMarkerOverlay(Drawable defaultMarker,
MyGeoPointListener geoPointListener) {
super(boundCenterBottom(defaultMarker));
this.geoPointListener = geoPointListener;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
return mOverlays.size();
}
@Override
public boolean onTap(GeoPoint geoPoint, MapView map) {
if (isPinch) {
return false;
} else {
if (geoPoint != null) {
if (null != geoPointListener) {
geoPointListener.GetGeoPoint(geoPoint);
}
return true;
} else {
if (null != geoPointListener) {
geoPointListener.GetGeoPoint(null);
}
return false;
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
int fingers = event.getPointerCount();
if (event.getAction() == MotionEvent.ACTION_DOWN) {
isPinch = false; // Touch DOWN, don't know if it's a pinch yet
}
if (event.getAction() == MotionEvent.ACTION_MOVE && fingers == 2) {
isPinch = true; // Two fingers, def a pinch
}
return super.onTouchEvent(event, mapView);
}
}
3. Implement MyGeoPointListener in activity
like,
public class MainActivity extends MapActivity implements MyGeoPointListener {
4. Override MyGeoPointListener:
.....
@Override
public void GetGeoPoint(GeoPoint geopoint) {
if (null != geopoint) {
String msg = "Lat: " + geopoint.getLatitudeE6() / 1E6 + " - "
+ "Lon: " + geopoint.getLongitudeE6() / 1E6;
Toast toast = Toast.makeText(MyLocationMap.this, msg,
Toast.LENGTH_SHORT);
toast.show();
List<Overlay> mapOverlays = myMap.getOverlays();
OverlayItem overlayitem = new OverlayItem(geopoint, "address_name",
"address");
Drawable icon = getResources().getDrawable(
R.drawable.location_marker);
DynamicMarkerOverlay customoverlay = new DynamicMarkerOverlay(icon,
this);
customoverlay.addOverlay(overlayitem);
mapOverlays.add(customoverlay);
myMap.getController().animateTo(geopoint);
}
}
.....
5. In OnCreate()
......
public MapView myMap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_map_layout);
.......
.......
myMap = (MapView) findViewById(R.id.map);
Drawable marker = getResources().getDrawable(R.drawable.ic_launcher);
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
myMap.getOverlays().add(new DynamicMarkerOverlay(marker, this));
......
}
6. Result: