所以我只是想在 android 中创建一个通用的 mapView 并在其中放置一些地理点。我已按照 android 开发者网站上的说明进行操作,但没有成功……有人可以帮忙吗?这是我的代码。
public class HelloItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext;
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
// defines the bounds for the overlayItems
super(boundCenterBottom(defaultMarker));
mContext = context;
}
@Override
protected OverlayItem createItem(int i) {
// returns the correct ArrayList position from int i
return mOverlays.get(i);
}
@Override
public int size() {
// returns number of items in ArrayList
return mOverlays.size();
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
@Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
//这是我的主要课程
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maptastic);
((TextView)((FrameLayout)((LinearLayout)((ViewGroup) getWindow().getDecorView()).getChildAt(0)).getChildAt(0)).getChildAt(0)).setGravity(Gravity.CENTER);
setTitle("Field Trip");
Drawable drawable = this.getResources().getDrawable(R.drawable.supaaaa);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this);
//initControls();
MapView mapview = (MapView) findViewById(R.id.mapView);
mapview.setBuiltInZoomControls(true);
mcontroller = mapview.getController();
// Plot all geo points
for (Entry<Integer, FieldTripStop> cur : Statics.fieldTripStops.entrySet())
{
// get the FieldTripStop object from the current hash table entry
Statics.currentFTStop = cur.getValue();
// concatenate numbers before (all) and after (6) the decimal, since
// geopoints only accept 6 numbers past the decimal.
theLat = Statics.currentFTStop.latitude;
theLong = Statics.currentFTStop.longitude;
/*
* String manipulation method
*
* theLat = theLat.replace(".","");
* theLat = theLat.substring(0, 8);
* theLong = theLong.replace(".","");
* theLong = theLong.substring(0, 8);
* point = new GeoPoint((int)(Integer.valueOf(theLat)), (int)(Integer.valueOf(theLong)));
*/
theDLat = Double.parseDouble(theLat);
theDLong = Double.parseDouble(theLong);
//olay = new OverlayItem(point, "herp", "derp");
//olayitems.add(olay);
List<Overlay> mapOverlays = mapview.getOverlays();
point = new GeoPoint((int)(theDLat*1E6), (int)(theDLong*1E6));
OverlayItem overlayitem = new OverlayItem(point, "holda, Mundo!", "I'm in Mexico City~!");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}