how to put marker on map when location gets changed in android

南笙酒味 提交于 2019-12-04 21:20:19

At your MyLocation class within onLocationChanged method you should write instead of GeoPoint p = new GeoPoint(lat, lon); try p = new GeoPoint(lat, lon);. In your code you created new variable called p (same as global variable) and your local variable point to object. Where your local variable p doesn't point at anything (= null). Because you use your global variable p in draw and is set to null.

If you're just looking to show the current location of the user on the map, it might be easier just to use the built-in MyLocationOverlay class.

Use the following in your onCreate():

        MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView);
        mapView.getOverlays().add(myLocationOverlay);
        myLocationOverlay.enableCompass(); // if you want to display a compass also
        myLocationOverlay.enableMyLocation();

This will show the users current location and update as they move around. It doesn't keep a track though of where the user has been. (I'm not sure if this is what you're trying to accomplish or just display the current location.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!