android - Latitude value set to 0 -
i set value mainactivity fragment, value become 0 in fragment. when got log in movetolocation(), value of latitude 0, when got log in setlocation, value of latitude correct passed value. why this?
mainactivity
mapfragment mapfragment = new mapfragment(); mapfragment.setlocation(111, 222, "abcaddress");
fragment
public class mapfragment extends fragment { private googlemap mmap; private double mlatitude; private double mlongitude; private string maddress; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); try { mapsinitializer.initialize(getactivity()); } catch (googleplayservicesnotavailableexception e) { log.d("", "you must update google maps."); getactivity().finish(); } } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); mmap = ((supportmapfragment)getfragmentmanager().findfragmentbyid(r.id.map)).getmap(); movetolocation(); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { return inflater.inflate(r.layout.map_page, null); } public void setlocation(double latitude, double longitude, string address) { mlatitude = latitude; mlongitude = longitude; maddress = address; } protected void movetolocation() { cameraposition location = new cameraposition.builder() .target(new latlng(mlatitude, mlongitude)).zoom(15.5f) .bearing(0).tilt(25).build(); mmap.animatecamera(cameraupdatefactory.newcameraposition(location)); markeroptions marker = new markeroptions().position(new latlng(mlatitude, mlongitude)).title(maddress); marker.snippet("snippet"); mmap.addmarker(marker); } }
you should not create new instance of mapfragment
.
to pass values, set them in argument while adding them in transaction :
bundle bundle = new bundle(); bundle.putdouble("lat", latitude); bundle.putdouble("long", longitude); bundle.putstring("address", address); myfragment.setarguments(bundle);
and fetch value in oncreate()
:
@override public view oncreate(bundle savedinstancestate) { bundle mbundle = getarguments(); if (mbundle != null) { string address = mbundle.getstring("address"); // call method setlocation(mbundle.getdouble("lat"), mbundle.getdouble("long"), mbundle.getstring("address")); } else { toast.maketext(getactivity(), "location not found", toast.length_short).show(); } }
hope helps ツ
Comments
Post a Comment