0

我在向用户选择的位置添加标记时遇到了很多麻烦。这是我的代码

public class ShowMapActivity extends MapActivity {

public static Context context;
private MapController mapController;
MapView mapView;
private LocationManager locationManager;
private MyOverlays itemizedoverlay;

private int latitude;
private int longitude;

private int lat;
private int lng;

public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.map); // bind the layout to the activity

    // create a map view
    //RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.map);
    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
    // Either satellite or 2d 
    mapView.setSatellite(true);
    mapController = mapView.getController();
    mapController.setZoom(14); // Zoon 1 is world view  
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
            0, new GeoUpdateHandler());

    Drawable drawable = this.getResources().getDrawable(R.drawable.point);
    itemizedoverlay = new MyOverlays(drawable);
    createMarker();

}



@Override
protected boolean isRouteDisplayed() {
    return false;
}



public class GeoUpdateHandler implements LocationListener {
//ignore this :)
    @Override
    public void onLocationChanged(Location location) {

        latitude = (int) (location.getLatitude() * 1E6);
        longitude = (int) (location.getLongitude() * 1E6);
        GeoPoint point = new GeoPoint(latitude, longitude);
        //createMarker();
        //Stop refreshing pointer
        //mapController.animateTo(point); // mapController.setCenter(point);

    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
}

private void createMarker() {
    GeoPoint p = mapView.getMapCenter();        
    OverlayItem overlayitem = new OverlayItem(p, "", "");
    itemizedoverlay.addOverlay(overlayitem);
    mapView.getOverlays().add(itemizedoverlay);
    mapView.invalidate();

}

public void onBackPressed() {
  finish();
}   


}

MyOverlays.java

public class MyOverlays extends ItemizedOverlay<OverlayItem> {

private static int maxNum = 5;
private OverlayItem overlays[] = new OverlayItem[maxNum];
//private ArrayList<OverlayItem> overlays = new ArrayList<OverlayItem>();

private int index = 0;
private boolean full = false;
private MyOverlays itemizedoverlay;

private Context context;


public MyOverlays(Drawable defaultMarker) {
    super(boundCenterBottom(defaultMarker));

    context = Application.getAppContext();
}


        @Override
        public boolean onTouchEvent(MotionEvent event, MapView mapView) 
        {   
            //---when user lifts his finger---
            if (event.getAction() == 1) {                
                GeoPoint p = mapView.getProjection().fromPixels(
                    (int) event.getX(),
                    (int) event.getY());
                    Toast.makeText(context, 
                        p.getLatitudeE6() / 1E6 + "," + 
                        p.getLongitudeE6() /1E6 , 
                        Toast.LENGTH_SHORT).show();

                    int lat =  p.getLatitudeE6();
                    int lng =  p.getLongitudeE6();

                    int latTest = p.getLatitudeE6() / 1000000;
                    int lngTest = p.getLongitudeE6() / 1000000;

                    //Add marker for selected area
                    GeoPoint point2 = new GeoPoint(latTest, lngTest);
                    OverlayItem overlayitem2 = new OverlayItem(point2, "Test, konichiwa!", "Test");
                    itemizedoverlay.addOverlay(overlayitem2);
                    mapView.getOverlays().add(itemizedoverlay);

                    SharedPreferences longSettings = context.getSharedPreferences("LONGITUDE", 0);
                    SharedPreferences latSettings = context.getSharedPreferences("LATITUDE", 0);
                    Editor e = longSettings.edit();
                    Editor e2 = latSettings.edit();
                    e.putInt("LONGITUDE", lng);
                    e2.putInt("LATITUDE", lat);

                    e.commit();
                    e2.commit();


            }                        
            return false; 
        }        




@Override
protected OverlayItem createItem(int i) {
    return overlays[i];
}

@Override
public int size() {
    if (full) {
        return overlays.length;
    } else {
        return index;
    }

}

public void addOverlay(OverlayItem overlay) {
    if (index < maxNum) {
        overlays[index] = overlay;
    } else {
        index = 0;
        full = true;
        overlays[index] = overlay;
    }
    index++;
    populate();
}


}

createMarker() 在我的 ShowMapActivity 中运行良好,但在尝试在“MyOverlays”中手动执行时却不行。我不确定我是否正确地提供了位置参数,所以我尝试了两种方式(参见 lat 和 latTest)。这似乎不是问题。

堆栈跟踪

03-14 04:24:54.596: E/AndroidRuntime(31512): FATAL EXCEPTION: main
03-14 04:24:54.596: E/AndroidRuntime(31512): java.lang.NullPointerException
03-14 04:24:54.596: E/AndroidRuntime(31512):    at     com.lol.MyOverlays.onTouchEvent(MyOverlays.java:60)
03-14 04:24:54.596: E/AndroidRuntime(31512):    at com.google.android.maps.OverlayBundle.onTouchEvent(OverlayBundle.java:63)
03-14 04:24:54.596: E/AndroidRuntime(31512):    at com.google.android.maps.MapView.onTouchEvent(MapView.java:679)
03-14 04:24:54.596: E/AndroidRuntime(31512):    at android.view.View.dispatchTouchEvent(View.java:3901)
03-14 04:24:54.596: E/AndroidRuntime(31512):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
03-14 04:24:54.596: E/AndroidRuntime(31512):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
03-14 04:24:54.596: E/AndroidRuntime(31512):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
03-14 04:24:54.596: E/AndroidRuntime(31512):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
03-14 04:24:54.596: E/AndroidRuntime(31512):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
03-14 04:24:54.596: E/AndroidRuntime(31512):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1714)
03-14 04:24:54.596: E/AndroidRuntime(31512):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1130)
03-14 04:24:54.596: E/AndroidRuntime(31512):    at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
03-14 04:24:54.596: E/AndroidRuntime(31512):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1698)
03-14 04:24:54.596: E/AndroidRuntime(31512):    at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2200)
03-14 04:24:54.596: E/AndroidRuntime(31512):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1884)
03-14 04:24:54.596: E/AndroidRuntime(31512):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 04:24:54.596: E/AndroidRuntime(31512):    at android.os.Looper.loop(Looper.java:130)
03-14 04:24:54.596: E/AndroidRuntime(31512):    at android.app.ActivityThread.main(ActivityThread.java:3835)
03-14 04:24:54.596: E/AndroidRuntime(31512):    at java.lang.reflect.Method.invokeNative(Native Method)
03-14 04:24:54.596: E/AndroidRuntime(31512):    at java.lang.reflect.Method.invoke(Method.java:507)
03-14 04:24:54.596: E/AndroidRuntime(31512):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
03-14 04:24:54.596: E/AndroidRuntime(31512):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
03-14 04:24:54.596: E/AndroidRuntime(31512):    at dalvik.system.NativeStart.main(Native Method)

有人有什么指导吗?

4

2 回答 2

0

在我看来, itemizedoverlay 从未初始化过。

即,您在顶部声明它:

private MyOverlays itemizedoverlay;

然后你尝试在仍然为空的情况下取消引用它:

itemizedoverlay.addOverlay(overlayitem2);
于 2012-03-14T05:30:32.180 回答
0

看看这个链接。它可能有用.. http://www.vogella.de/articles/AndroidLocationAPI/article.html也看看这个另一个链接

于 2012-03-14T04:46:36.857 回答