我想顯示用戶的當前位置並允許用戶在地圖上放置一個標記。當用戶點擊他放置的標記時,標記將消失,並且他可以再次在地圖上放置另一個標記。Android的項目覆蓋!在地圖上放置並移除標記
以下代碼在顯示用戶的當前位置並在地圖上放置一個標記時正常工作。它也能夠移除標記。但是,一旦我嘗試在移除地圖後重新放置地圖上的標記。該程序崩潰!有誰能告訴我原因嗎?從日食
class InterestingLocations extends ItemizedOverlay {
private ArrayList<OverlayItem> locations = new ArrayList<OverlayItem>();
private GeoPoint center = null;
private Context mContext ;
private boolean placed=false;
public InterestingLocations(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
// create locations of interest
GeoPoint me = new
GeoPoint((int)(lat*1000000),(int)(lng*1000000));
locations.add(new OverlayItem(me,
"Me!", "Me! Me! Me!"));
center=me;
populate();
}
public InterestingLocations(Drawable marker, Context context)
{
super(marker);
mContext= context;
// create locations of interest
GeoPoint me = new
GeoPoint((int)(lat*1000000),(int)(lng*1000000));
locations.add(new OverlayItem(me,
"Me!", "Me!"));
center=me;
populate();
}
// We added this method to find the middle point of the cluster
// Start each edge on its opposite side and move across with each point.
// The top of the world is +90, the bottom -90,
// the west edge is -180, the east +180
@Override
public void draw(Canvas canvas, MapView mapview, boolean shadow) {
// Here is where we can eliminate shadows by setting to false
super.draw(canvas, mapview, shadow);
}
@Override
protected OverlayItem createItem(int i) {
return locations.get(i);
}
@Override
public int size() {
return locations.size();
}
public boolean onTap (final GeoPoint p, final MapView mapView){
boolean tapped = super.onTap(p, mapView);
Log.i("onTap", "onTap 1");
if (tapped){
Log.i("Hit item!!", "Hited");
//do what you want to do when you hit an item
if(p!=me2){
Log.i("Hit item!!", "Hited and p!=me2");
removeOverlay();
placed=false;
}
}
else{
if(placed==false&&placeAble==true){
Log.i("placed=="+placed, "placed==false");
addOverlay(new OverlayItem(p, "Dest", "Destination"));
destLat = (float)p.getLatitudeE6()/1000000f;
destLng = (float)p.getLongitudeE6()/1000000f;
placed=true;
}
else{
Log.i("placed=="+placed, "here is else");
}
//do what you want to do when you DONT hit an item
}
return true;
}
//You must have this method, even if it doesn't visibly do anything
@Override
protected boolean onTap(int index) {
Log.i("Hit item!!", "Hited"+ index);
return true;
}
public void addOverlay(OverlayItem overlay) {
locations.add(overlay);
populate();
}
public void removeOverlay() {
locations.clear();
GeoPoint me = new GeoPoint((int)(lat*1000000),(int)(lng*1000000));
locations.add(new OverlayItem(me, "Me!", "Me! Me! Me!"));
populate();
}
}
錯誤:
06-04 18:03:03.859: E/global(29049): Deprecated Thread methods are not supported.
06-04 18:03:03.859: E/global(29049): java.lang.UnsupportedOperationException
06-04 18:03:03.859: E/global(29049): at java.lang.VMThread.stop(VMThread.java:85)
06-04 18:03:03.859: E/global(29049): at java.lang.Thread.stop(Thread.java:1280)
06-04 18:03:03.859: E/global(29049): at java.lang.Thread.stop(Thread.java:1247)
06-04 18:03:03.859: E/global(29049): at com.knowithk.kpass.KPassActivity$1.run(KPassActivity.java:46)
06-04 18:03:17.429: E/MapActivity(29049): Couldn't get connection factory client
06-04 18:03:32.773: E/AndroidRuntime(29049): FATAL EXCEPTION: main
06-04 18:03:32.773: E/AndroidRuntime(29049): java.lang.ArrayIndexOutOfBoundsException
06-04 18:03:32.773: E/AndroidRuntime(29049): at com.google.android.maps.ItemizedOverlay.maskHelper(ItemizedOverlay.java:562)
06-04 18:03:32.773: E/AndroidRuntime(29049): at com.google.android.maps.ItemizedOverlay.setFocus(ItemizedOverlay.java:365)
06-04 18:03:32.773: E/AndroidRuntime(29049): at com.google.android.maps.ItemizedOverlay.focus(ItemizedOverlay.java:539)
06-04 18:03:32.773: E/AndroidRuntime(29049): at com.google.android.maps.ItemizedOverlay.onTap(ItemizedOverlay.java:455)
06-04 18:03:32.773: E/AndroidRuntime(29049): at com.knowithk.kpass.taxifare.MyMap$InterestingLocations.onTap(MyMap.java:333)
06-04 18:03:32.773: E/AndroidRuntime(29049): at com.google.android.maps.OverlayBundle.onTap(OverlayBundle.java:83)
06-04 18:03:32.773: E/AndroidRuntime(29049): at com.google.android.maps.MapView$1.onSingleTapUp(MapView.java:356)
06-04 18:03:32.773: E/AndroidRuntime(29049): at com.google.android.maps.GestureDetector.onTouchEvent(GestureDetector.java:533)
06-04 18:03:32.773: E/AndroidRuntime(29049): at com.google.android.maps.MapView.onTouchEvent(MapView.java:683)
06-04 18:03:32.773: E/AndroidRuntime(29049): at android.view.View.dispatchTouchEvent(View.java:3938)
06-04 18:03:32.773: E/AndroidRuntime(29049): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
06-04 18:03:32.773: E/AndroidRuntime(29049): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
06-04 18:03:32.773: E/AndroidRuntime(29049): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
06-04 18:03:32.773: E/AndroidRuntime(29049): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
06-04 18:03:32.773: E/AndroidRuntime(29049): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
06-04 18:03:32.773: E/AndroidRuntime(29049): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1733)
06-04 18:03:32.773: E/AndroidRuntime(29049): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1151)
06-04 18:03:32.773: E/AndroidRuntime(29049): at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
06-04 18:03:32.773: E/AndroidRuntime(29049): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1717)
06-04 18:03:32.773: E/AndroidRuntime(29049): at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2215)
06-04 18:03:32.773: E/AndroidRuntime(29049): at android.view.ViewRoot.handleMessage(ViewRoot.java:1886)
06-04 18:03:32.773: E/AndroidRuntime(29049): at android.os.Handler.dispatchMessage(Handler.java:99)
06-04 18:03:32.773: E/AndroidRuntime(29049): at android.os.Looper.loop(Looper.java:130)
06-04 18:03:32.773: E/AndroidRuntime(29049): at android.app.ActivityThread.main(ActivityThread.java:3687)
06-04 18:03:32.773: E/AndroidRuntime(29049): at java.lang.reflect.Method.invokeNative(Native Method)
06-04 18:03:32.773: E/AndroidRuntime(29049): at java.lang.reflect.Method.invoke(Method.java:507)
06-04 18:03:32.773: E/AndroidRuntime(29049): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
06-04 18:03:32.773: E/AndroidRuntime(29049): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
06-04 18:03:32.773: E/AndroidRuntime(29049): at dalvik.system.NativeStart.main(Native Method)
如果您發佈stacktrace(logcat中的錯誤),我們可以更容易地爲您提供幫助。 – Reinier
非常感謝您的建議。 – crystalWing
第333行的代碼是什麼?順便說一句,你可能想改變(p!= me2)爲(!p.equals(me2))。 – Reinier