问题
I have very limited experience with Mapview, Overlay and ItemizedOverlay. I've seen some simple code examples that use one drawable for overlays and itemizedOverlays but I'm unsure how to approach these requirements: I want more than one drawable on my map view (example: a star-icon for the map center and some other icon for other overlay items) but I want one to be unclickable (the star). Should I use both Overlay and ItemizedOverlay to achieve this?
Also, my next problem is a matter of presentation: If I have 500 items to display on a map, what is a practical way of displaying that information? Again, I have little experience developing and using map applications.
回答1:
I faced the same situation a couple of weeks ago.
You should use one ItemizedOverlay for each different drawable, and then add overlayItems to the ItemizedOverlay.
The most convenient way is to extend ItemizedOverlay, so you can define a marker and a click behavoir for each style you need.
For the second part, for performance concerns, you shouldn't populate your map with all your 500 items once at a time. I used a system that dynamically adds to the map markers that belongs to the displayed scope of map.
Here is the snippet of my ItemizedOverlay that could be useful for both of your questions :
private class MarkerItemized_Overlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext;
public MarkerItemizedOverlay(Drawable marker, Context context) {
super(boundCenterBottom(marker));
mContext = context;
}
public void addOverlay(OverlayItem overlay) {
if (!mOverlays.contains(overlay)) {
mOverlays.add(overlay);
populate();
}
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
return mOverlays.size();
}
@Override
protected boolean onTap(final int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.setNegativeButton("Annuler",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton){}
});
dialog.setPositiveButton("Naviguer",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton){ showDirections(mOverlays.get(index).getPoint()); }
});
dialog.setInverseBackgroundForced(true);
dialog.show();
return true;
}
public boolean contains(Store store) {
return mOverlays.contains(store);
}
@Override
public boolean onTouchEvent(MotionEvent e, MapView mapView) {
if (e.getAction() == MotionEvent.ACTION_UP) {
GeoPoint newCenter = mapView.getMapCenter();
int minLat, maxLat, minLng, maxLng;
minLat = mapCenter.getLatitudeE6() - mapView.getLatitudeSpan()/2;
maxLat = mapCenter.getLatitudeE6() + mapView.getLatitudeSpan()/2;
minLng = mapCenter.getLongitudeE6() - mapView.getLongitudeSpan()/2;
maxLng = mapCenter.getLongitudeE6() + mapView.getLongitudeSpan()/2;
if (newCenter.getLatitudeE6() > maxLat ||
newCenter.getLatitudeE6() < minLat ||
newCenter.getLongitudeE6() > maxLng ||
newCenter.getLongitudeE6() < minLng)
{
mapCenter = mapView.getMapCenter();
Location mapCenterLoc = new Location(providerName);
mapCenterLoc.setLatitude(newCenter.getLatitudeE6()/1E6);
mapCenterLoc.setLongitude(newCenter.getLongitudeE6()/1E6);
Store[] newClosestStores = storeDB.getClosestStores(mapCenterLoc);
for (int i = 0; i < newClosestStores.length; i++)
if (! itemizedOverlay.contains(newClosestStores[i]))
setMarker(newClosestStores[i]);
}
}
return super.onTouchEvent(e, mapView);
}
}
来源:https://stackoverflow.com/questions/8359050/using-mutliple-drawables-on-a-mapview