Marking Current Location on Map, Android

I followed some tutorials to create an application that shows the current position of the user on the map with a marking. But for some reasons I can't get to work the marking part? The other parts work well, but whenever I add the marking code the application crashes.
Here is the code:

public class LocationActivity extends MapActivity {

    private MapView mapView;
    private LocationManager lm;
    private LocationListener ll;
    private MapController mc;
    GeoPoint p = null;
    Drawable defaultMarker = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mapView = (MapView) findViewById(R.id.mapView);
        // show zoom in/out buttons
        mapView.setBuiltInZoomControls(true);
        // Standard view of the map(map/sat)
        mapView.setSatellite(false);
        // get controller of the map for zooming in/out
        mc = mapView.getController();
        // Zoom Level
        mc.setZoom(18);
        MyLocationOverlay myLocationOverlay = new MyLocationOverlay();
        List<Overlay> list = mapView.getOverlays();
        list.add(myLocationOverlay);
        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        ll = new MyLocationListener();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
        // Get the current location in start-up
        GeoPoint initGeoPoint = new GeoPoint((int) (lm.getLastKnownLocation(
                LocationManager.GPS_PROVIDER).getLatitude() * 1000000),
                (int) (lm.getLastKnownLocation(LocationManager.GPS_PROVIDER)
                        .getLongitude() * 1000000));
        mc.animateTo(initGeoPoint);
    }

    protected class MyLocationOverlay extends com.google.android.maps.Overlay {

        @Override
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
                long when) {
            Paint paint = new Paint();
            super.draw(canvas, mapView, shadow);
            // Converts lat/lng-Point to OUR coordinates on the screen.
            Point myScreenCoords = new Point();
            mapView.getProjection().toPixels(p, myScreenCoords);
            paint.setStrokeWidth(1);
            paint.setARGB(255, 255, 255, 255);
            paint.setStyle(Paint.Style.STROKE);
            Bitmap bmp = BitmapFactory.decodeResource(getResources(),
                    R.drawable.push);
            canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
            canvas.drawText("I am here...", myScreenCoords.x, myScreenCoords.y,
                    paint);
            return true;
        }
    }

    private class MyLocationListener implements LocationListener {

        public void onLocationChanged(Location argLocation) {
            GeoPoint myGeoPoint = new GeoPoint(
                    (int) (argLocation.getLatitude() * 1000000),
                    (int) (argLocation.getLongitude() * 1000000));
            /*
             * it will show a message on location change
             * Toast.makeText(getBaseContext(), "New location latitude ["
             * +argLocation.getLatitude() + "] longitude [" +
             * argLocation.getLongitude()+"]", Toast.LENGTH_SHORT).show();
             */
            mc.animateTo(myGeoPoint);
        }

        public void onProviderDisabled(String provider) {}

        public void onProviderEnabled(String provider) {}

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

    protected boolean isRouteDisplayed() {
        return false;
    }
}

here is the logcat:

    01-19 05:31:43.011: DEBUG/AndroidRuntime(759): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
01-19 05:31:43.011: DEBUG/AndroidRuntime(759): CheckJNI is ON
01-19 05:31:43.411: DEBUG/AndroidRuntime(759): --- registering native functions ---
01-19 05:31:43.431: INFO/jdwp(759): received file descriptor 19 from ADB
01-19 05:31:43.431: INFO/jdwp(759): Ignoring second debugger -- accepting and dropping
01-19 05:31:44.531: INFO/ActivityManager(583): Starting activity: Intent { flg=0x10000000 cmp=pro.googlemapp/.LocationActivity }
01-19 05:31:44.641: DEBUG/AndroidRuntime(759): Shutting down VM
01-19 05:31:44.641: DEBUG/dalvikvm(759): DestroyJavaVM waiting for non-daemon threads to exit
01-19 05:31:44.641: DEBUG/dalvikvm(759): DestroyJavaVM shutting VM down
01-19 05:31:44.641: DEBUG/dalvikvm(759): HeapWorker thread shutting down
01-19 05:31:44.651: DEBUG/dalvikvm(759): HeapWorker thread has shut down
01-19 05:31:44.651: DEBUG/jdwp(759): JDWP shutting down net...
01-19 05:31:44.651: DEBUG/jdwp(759): +++ peer disconnected
01-19 05:31:44.651: INFO/dalvikvm(759): Debugger has detached; object registry had 1 entries
01-19 05:31:44.661: DEBUG/dalvikvm(759): VM cleaning up
01-19 05:31:44.681: INFO/ActivityManager(583): Start proc pro.googlemapp for activity pro.googlemapp/.LocationActivity: pid=770 uid=10025 gids={3003}
01-19 05:31:44.761: DEBUG/dalvikvm(759): LinearAlloc 0x0 used 676436 of 4194304 (16%)
01-19 05:31:44.801: INFO/jdwp(770): received file descriptor 20 from ADB
01-19 05:31:44.822: INFO/dalvikvm(770): ignoring registerObject request in thread=3
01-19 05:31:44.851: INFO/jdwp(770): Ignoring second debugger -- accepting and dropping
01-19 05:31:44.851: ERROR/jdwp(770): Failed writing handshake bytes: Broken pipe (-1 of 14)
01-19 05:31:44.851: INFO/dalvikvm(770): Debugger has detached; object registry had 0 entries
01-19 05:31:45.320: ERROR/ActivityThread(770): Failed to find provider info for com.google.settings
01-19 05:31:45.320: ERROR/ActivityThread(770): Failed to find provider info for com.google.settings
01-19 05:31:45.340: ERROR/ActivityThread(770): Failed to find provider info for com.google.settings
01-19 05:31:45.781: DEBUG/LocationManager(770): Constructor: service = android.location.ILocationManager$Stub$Proxy@4379d9f0
01-19 05:31:45.791: WARN/GpsLocationProvider(583): Duplicate add listener for uid 10025
01-19 05:31:45.791: DEBUG/GpsLocationProvider(583): setMinTime 0
01-19 05:31:45.791: DEBUG/GpsLocationProvider(583): startNavigating
01-19 05:31:45.831: INFO/jdwp(770): received file descriptor 27 from ADB
01-19 05:31:46.001: INFO/MapActivity(770): Handling network change notification:CONNECTED
01-19 05:31:46.001: ERROR/MapActivity(770): Couldn't get connection factory client
01-19 05:31:46.451: DEBUG/dalvikvm(770): GC freed 4539 objects / 298952 bytes in 118ms
01-19 05:31:46.470: DEBUG/AndroidRuntime(770): Shutting down VM
01-19 05:31:46.470: WARN/dalvikvm(770): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
01-19 05:31:46.481: ERROR/AndroidRuntime(770): Uncaught handler: thread main exiting due to uncaught exception
01-19 05:31:46.541: ERROR/AndroidRuntime(770): java.lang.NullPointerException
01-19 05:31:46.541: ERROR/AndroidRuntime(770):     at com.google.android.maps.PixelConverter.toPixels(PixelConverter.java:58)
01-19 05:31:46.541: ERROR/AndroidRuntime(770):     at com.google.android.maps.PixelConverter.toPixels(PixelConverter.java:48)
01-19 05:31:46.541: ERROR/AndroidRuntime(770):     at pro.googlemapp.LocationActivity$MyLocationOverlay.draw(LocationActivity.java:101)
01-19 05:31:46.541: ERROR/AndroidRuntime(770):     at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:42)
01-19 05:31:46.541: ERROR/AndroidRuntime(770):     at com.google.android.maps.MapView.onDraw(MapView.java:476)
01-19 05:31:46.541: ERROR/AndroidRuntime(770):     at android.view.View.draw(View.java:6274)
01-19 05:31:46.541: ERROR/AndroidRuntime(770):     at android.view.ViewGroup.drawChild(ViewGroup.java:1526)
01-19 05:31:46.541: ERROR/AndroidRuntime(770):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
01-19 05:31:46.541: ERROR/AndroidRuntime(770):     at android.view.ViewGroup.drawChild(ViewGroup.java:1524)
01-19 05:31:46.541: ERROR/AndroidRuntime(770):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
01-19 05:31:46.541: ERROR/AndroidRuntime(770):     at android.view.View.draw(View.java:6277)
01-19 05:31:46.541: ERROR/AndroidRuntime(770):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
01-19 05:31:46.541: ERROR/AndroidRuntime(770):     at android.view.ViewGroup.drawChild(ViewGroup.java:1526)
01-19 05:31:46.541: ERROR/AndroidRuntime(770):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
01-19 05:31:46.541: ERROR/AndroidRuntime(770):     at android.view.ViewGroup.drawChild(ViewGroup.java:1524)
01-19 05:31:46.541: ERROR/AndroidRuntime(770):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
01-19 05:31:46.541: ERROR/AndroidRuntime(770):     at android.view.View.draw(View.java:6277)
01-19 05:31:46.541: ERROR/AndroidRuntime(770):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
01-19 05:31:46.541: ERROR/AndroidRuntime(770):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1883)
01-19 05:31:46.541: ERROR/AndroidRuntime(770):     at android.view.ViewRoot.draw(ViewRoot.java:1332)
01-19 05:31:46.541: ERROR/AndroidRuntime(770):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1097)
01-19 05:31:46.541: ERROR/AndroidRuntime(770):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1613)
01-19 05:31:46.541: ERROR/AndroidRuntime(770):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-19 05:31:46.541: ERROR/AndroidRuntime(770):     at android.os.Looper.loop(Looper.java:123)
01-19 05:31:46.541: ERROR/AndroidRuntime(770):     at android.app.ActivityThread.main(ActivityThread.java:4203)
01-19 05:31:46.541: ERROR/AndroidRuntime(770):     at java.lang.reflect.Method.invokeNative(Native Method)
01-19 05:31:46.541: ERROR/AndroidRuntime(770):     at java.lang.reflect.Method.invoke(Method.java:521)
01-19 05:31:46.541: ERROR/AndroidRuntime(770):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
01-19 05:31:46.541: ERROR/AndroidRuntime(770):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
01-19 05:31:46.541: ERROR/AndroidRuntime(770):     at dalvik.system.NativeStart.main(Native Method)
01-19 05:31:46.551: INFO/Process(583): Sending signal. PID: 770 SIG: 3
01-19 05:31:46.581: INFO/dalvikvm(770): threadid=7: reacting to signal 3
01-19 05:31:46.661: INFO/dalvikvm(770): Wrote stack trace to '/data/anr/traces.txt'
01-19 05:31:46.871: INFO/ARMAssembler(583): generated scanline__00000077:03515104_00000000_00000000 [ 27 ipp] (41 ins) at [0x2c69c8:0x2c6a6c] in 973448 ns
01-19 05:31:46.911: INFO/ARMAssembler(583): generated scanline__00000077:03515104_00001001_00000000 [ 64 ipp] (84 ins) at [0x2c6a70:0x2c6bc0] in 1985378 ns
01-19 05:31:49.881: INFO/Process(770): Sending signal. PID: 770 SIG: 9
01-19 05:31:49.931: INFO/ActivityManager(583): Process pro.googlemapp (pid 770) has died.
01-19 05:31:49.941: WARN/GpsLocationProvider(583): Unneeded remove listener for uid 1000
01-19 05:31:49.941: DEBUG/GpsLocationProvider(583): stopNavigating
01-19 05:31:49.951: INFO/WindowManager(583): WIN DEATH: Window{438891c0 pro.googlemapp/pro.googlemapp.LocationActivity paused=false}
01-19 05:31:50.111: WARN/UsageStats(583): Unexpected resume of com.android.launcher while already resumed in pro.googlemapp
01-19 05:31:50.200: WARN/InputManagerService(583): Got RemoteException sending setActive(false) notification to pid 770 uid 10025


Asked by: Catherine881 | Posted: 24-01-2022






Answer 1

the way you construct the MyLocationOverlay might have some problem.

MyLocationOverlay mylocationOverlay = new MyLocationOverlay(this, mapView);

according to the google code api you need to pass a context and a mapview param.

and then put these code after that

mylocationOverlay.enableMyLocation();
mapView.getOverlays().add(locationOverlay);

also put

mylocationOverlay.enableMyLocation(); in the onResume() method

Hope this could help you.

Answered by: Kevin654 | Posted: 25-02-2022



Answer 2

The GeoPoint "p" in the MyLocationOverlay is null. When you replay "GeoPoint initGeoPoint" with "p" you can solve this problem.

Answered by: Anna528 | Posted: 25-02-2022



Answer 3

This is something which might help you in sloving your byg:

http://code.google.com/p/android/issues/detail?id=2035

Answered by: Aida617 | Posted: 25-02-2022



Answer 4

In your logs look at this line 01-19 05:31:46.541: ERROR/AndroidRuntime(770): at pro.googlemapp.LocationActivity$MyLocationOverlay.draw(LocationActivity.java:101)

This refers to the GeoPoint p. At the beginning you set the value of p to null (as "asdf" says) and never change its value to a GeoPoint and that is why you get java.lang.NullPointerException.

For example you can set the value of p like this: p = new GeoPoint(19240000,-99120000); and of course this must happen before this line mapView.getProjection().toPixels(p, myScreenCoords); of your code.

Good luck

Answered by: Joyce775 | Posted: 25-02-2022



Answer 5

This is because Geopoint p value is null in class MyLocationListener. So when you called mapView.getProjection().toPixels(p, myScreenCoords); because p is null it is unable to convert toPixels and throws Exception. You can overcome on this by just declare your GeoPoint p as static in your LocationActivity.

Answered by: Roland649 | Posted: 25-02-2022



Similar questions

android - Why this code cant make overlay item appear in my current location

Hey i want to add an image to appear in my current location and here is my code i added overlay items but i want to add method or something like this to make an overlay item appear in my current location , i added the bmp to add the item in my curent location please can any one check it out !!!! so Please why it cant appear in my current location ?? why it cant appear the overlay item in my current location Despite they a...


android - how to add overlay item in my current location in this code

Here is my code !! i want to add overlay item in my current location !! but seems a problem i cant can any one check what i have to add to my code to just put overlay item appear in my current location Please Check it up . package tryanabtry.opa; import java.util.List; import android.app.PendingIntent; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitm...


android - Specifying screen location of an image overlay

In Android, I know how to overlay two images and to animate them. The question is how to overlay the second image at a specific location over the first image. As an example, the first image is the image of an appliance with a power switch, the second image is the power LED that will glow when animated. I would like to know how I can specify the location of the power LED in the overlay so that it covers the ...


android - How to get location (on screen) of row in listview

I have implemented some gesture detection code so that I can detect when a row in my listview (which is situated in a FrameLayout) has been swiped. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if (e2.getX() - e1.getX() &gt; SWIPE_MIN_DISTANCE &amp;&amp; Math.abs(velocityX) &gt; SWIPE_THRESHOLD_VELOCITY) { int itemId = MainActivity.this.getListView()....


Add marker on touched location using google map in Android

How do I add a marker on a particular location in the map? I saw this code that shows the coordinates of the touched location. And I want a marker to pop or be shown in that same location every time it is touched. How do I do this? public boolean onTouchEvent(MotionEvent event, MapView mapView) { if (event.getAction() == 1) { GeoPoint p = mapView.getProjection().fromPi...


android - How to change DB location for content

I want to change setting in native application (i.e. SMS) by pointing it to different content location. Ex - At given fraction of time, native application is pointing to list of SMS (com.android.providers.telephony.databases.mmssms.db). I want to point to different location, that might be com.android.providers.telephony.databases.mmssms1.db


android - Adding a child view at particular location in parent view

I need to draw a child view containing a rectangle and button on top of the content view at particular location on the parent (content view). How can I do this? Did not find an explicit way to set origin of child view?


android - How to mock location on device?

How can I mock my location on a physical device (Nexus One)? I know you can do this with the emulator in the Emulator Control panel, but this doesn't work for a physical device.


android - What is the best way to wait for a GPS location fix?

Using my Android application, when a user does a certain action, a background service is started that fetches the current GPS location and saves it in a database in addition of doing some other stuff. In that service, I use the requestLocationUpdates() from the LocationManager class and wait until the onLocationChanged() of my LocationListener (which is implemented by the...


gwt - Gears or HTML5 Location API on Android 1.5

I am trying to use gwt-mobile-webkit, particularly its location api. It works well with iPhone (both device and simulator) and Firefox and on G1 with 1.6 Android, however, it does not work on G2 with Android 1.5 on it. In result I am getting onFailure callback with Permission Denied error. So it seems, that there is some geolocation API (gears or HTML5) in the browser available, but it just does not want to ask use...


java - Android Location Error

I've singled out my problem to a few lines of code lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 5.0f, this); lat = location.getLatitude(); lng = location.getLongitude(); //lat = new Double(40.431682); //lng = new Double(-74.2021819); pLocation = new G...


Error getting GPS location on Droid + android 2.0 sdk

I am trying develop an application which shows user current location. To find user's current location i am using following code: import android.location.Location; import android.location.LocationListener; import android.location.LocationProvider; import android.os.Bundle; public class MyLocationListener implements LocationListener { private static double latitude; private static double longitude; public...


Android GPS location updates doesn't work

I am trying to get the GPS location of my HTC magic using the following code : public class TestGPS extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final TextView tv1 = (TextView)findViewById(R.id.tv1); final TextView tv2 = (TextView)findViewBy...






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top