android - Saving Map Marker Reference -
i wondering if me, trying save reference newly created marker in concurrenthashmap, seem have no problem storing reference when try retrieve app crashes , throws nullpointer exception, below how store , retrieve marker.
storing in mmarkers
private map<latlng, marker> mmarkers = new concurrenthashmap<latlng, marker>();
latlng currpos = new latlng(map.getcameraposition().target.latitude,map.getcameraposition().target.longitude); marker marker = map.addmarker(new markeroptions() .position(currpos) ); mmarkers.put(currpos, marker); locationcount++; sharedpreferences.editor editor = settings.edit(); editor.putstring("mlat"+ integer.tostring((locationcount-1)), double.tostring(marker.getposition().latitude)); editor.putstring("mlng"+ integer.tostring((locationcount-1)), double.tostring(marker.getposition().longitude)); editor.putint("locationcount", locationcount); editor.putstring("mzoom", float.tostring(map.getcameraposition().zoom)); editor.commit();
removing fails
map.setonmaplongclicklistener(new onmaplongclicklistener() { @override public void onmaplongclick(latlng point) { marker marker = mmarkers.get(point); marker.remove(); //fails here mmarkers.remove(point); } });
i using markers latlng position key store it.
04-06 16:52:05.535: e/androidruntime(26601): fatal exception: main 04-06 16:52:05.535: e/androidruntime(26601): java.lang.nullpointerexception 04-06 16:52:05.535: e/androidruntime(26601): @ com.grim.pipboid.fragmentmap$1.onmaplongclick(fragmentmap.java:154) 04-06 16:52:05.535: e/androidruntime(26601): @ com.google.android.gms.maps.googlemap$7.onmaplongclick(unknown source) 04-06 16:52:05.535: e/androidruntime(26601): @ com.google.android.gms.maps.internal.j$a.ontransact(unknown source) 04-06 16:52:05.535: e/androidruntime(26601): @ android.os.binder.transact(binder.java:310) 04-06 16:52:05.535: e/androidruntime(26601): @ euq.a(sourcefile:93) 04-06 16:52:05.535: e/androidruntime(26601): @ maps.c.j.b(unknown source) 04-06 16:52:05.535: e/androidruntime(26601): @ maps.ay.an.c(unknown source) 04-06 16:52:05.535: e/androidruntime(26601): @ maps.ay.bc.onlongpress(unknown source) 04-06 16:52:05.535: e/androidruntime(26601): @ maps.bo.g.onlongpress(unknown source) 04-06 16:52:05.535: e/androidruntime(26601): @ maps.bo.h.c(unknown source) 04-06 16:52:05.535: e/androidruntime(26601): @ maps.bo.i.handlemessage(unknown source) 04-06 16:52:05.535: e/androidruntime(26601): @ android.os.handler.dispatchmessage(handler.java:99) 04-06 16:52:05.535: e/androidruntime(26601): @ android.os.looper.loop(looper.java:137) 04-06 16:52:05.535: e/androidruntime(26601): @ android.app.activitythread.main(activitythread.java:5227) 04-06 16:52:05.535: e/androidruntime(26601): @ java.lang.reflect.method.invokenative(native method) 04-06 16:52:05.535: e/androidruntime(26601): @ java.lang.reflect.method.invoke(method.java:511) 04-06 16:52:05.535: e/androidruntime(26601): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:795) 04-06 16:52:05.535: e/androidruntime(26601): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:562) 04-06 16:52:05.535: e/androidruntime(26601): @ dalvik.system.nativestart.main(native method)
thank reading
the user can long-click anywhere on map. not have @ precise latitude , longitude of existing marker. hence, latlng
not exist in hashmap
.
Comments
Post a Comment