开发者

MyLocationOverlay problem

开发者 https://www.devze.com 2023-02-16 23:16 出处:网络
private void initMyLocation() { myLocOverlay = new MyLocationOverlay(this, myMap); myLocOverlay.enableCompass();
private void initMyLocation() {
        myLocOverlay = new MyLocationOverlay(this, myMap);
        myLocOverlay.enableCompass();
        myLocOverlay.enableMyLocation();
        myMap.getOverlays().add(myLocOverlay);
        if (myLocOverlay == null)
    { 
        assignFirstLoc();

    }


if (myLocOverlay != null)
            {
                Log.e("error","1");
                p = new GeoPoint((int) (myLocOverlay.getMyLocation().getLatitudeE6()), (int) (myLocOverlay.getMyLocation().getLongitudeE6()));
                mc = myMap.getController();
                mc.animateTo(p);
                mc.setCenter(p); 

            } 

I saw "error 1" message in the logcat but after that I took an null pointer exce开发者_如何学编程ption I dont know why, I believe I check myLocOverlay is null or not with "if" block.. Any suggesstions?

Edit:

This is my assignedGFirstLoc method that assigned the firtst values:

private void assignFirstLoc()
    {
        final String coordinates[] = {"41.00527", "28.97696"};
        double lat2 = Double.parseDouble(coordinates[0]);
        double lng2 = Double.parseDouble(coordinates[1]);
        p = new GeoPoint((int) (lat2 * 1E6), (int) (lng2 * 1E6));
        myMap.getController().setZoom(16);
        myMap.getController().animateTo(p);
        myMap.getController().setCenter(p);

    }


MyLocationOverlay can only be used to show a location coming from sensors. You cannot set it. You got a NullPointer because MyLocationOverlay.getMyLocation() returns null. Sensors didn't have the time to catch the geolocation yet.

If you want to show the location when the sensor got the data you can do this:

mMyLocationOverlay.runOnFirstFix(new Runnable() { public void run() {
        mapView.getController().animateTo(mMyLocationOverlay.getMyLocation());
}});

If you want to display an item in an arbitray location (with latitude and longitude values) you need to implement your own version of ItemizedOverlay You can find an exemple in the SDK documentation: Google Map View tutorial, Part2 AddingOverlayItem

If you just want to show an arbitrary location on the map, you don't need an overlay. just call your function and it will work:

private void initMyLocation() {
    assignFirstLoc();
}


It looks like you are not passing any coordinates into GeoPoint. Unless it is somewhere else in your code. Something like this works

String coordinates[] = {"41.146064", "-80.642861"};
    double lat = Double.parseDouble(coordinates[0]);
    double lng = Double.parseDouble(coordinates[1]);

    p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
0

精彩评论

暂无评论...
验证码 换一张
取 消