Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MyLocationOverlay problem

Tags:

android

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 exception 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);

    }
like image 316
eddie skywalker Avatar asked Dec 09 '25 01:12

eddie skywalker


1 Answers

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();
}
like image 118
pcans Avatar answered Dec 10 '25 16:12

pcans



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!