android - Map Fragment doesn't load with same GPS location and Map Type -
i'm new android programming. i'm working on android project need access google maps. i've navigation drawer couple of options include google map location too.
so whenever try load map first time works fine , loads map current gps location , map type defined.
but whenever try switch other fragment , come map fragment, gps function , map type doesn't work.
help needed. in advance. i'm trying possible fix it. :)
my code below.
what i'm trying here whenever user touches somewhere, marker placed on location.
public class addcustomerlocationfragment extends fragment implements locationlistener { private mapview mapview; private googlemap map; private marker marker; private boolean markeravailable; private bundle bundle; button btnreset, btnsetlocation; locationmanager locationmanager; private view view; private double[] addcustomerlocation = new double[10]; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.fragment_addcustomer_location, container, false); mapsinitializer.initialize(getactivity()); mapview = (mapview) v.findviewbyid(r.id.map); btnreset = (button) v.findviewbyid(r.id.btnreset); btnsetlocation = (button) v.findviewbyid(r.id.btnsetlocation); mapview.oncreate(bundle); map.setmylocationenabled(true); locationmanager = (locationmanager) getactivity() .getsystemservice( context.location_service); if (locationmanager.isproviderenabled(locationmanager.gps_provider)) { getgps(); } else { locationmanager.requestlocationupdates( locationmanager.network_provider, 1000, 10, this); } map.setmaptype(googlemap.map_type_hybrid); map.getuisettings().setmylocationbuttonenabled(true); map.getuisettings().setcompassenabled(true); map.setonmapclicklistener(new onmapclicklistener() { public void onmapclick(latlng point) { toast.maketext( getactivity(), "latitude:" + point.latitude + ", longitude:" + point.longitude, toast.length_short).show(); addmarker(point); markeravailable = true; } }); btnsetlocation.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub toast.maketext( getactivity(), "your current loaction set to: latitude:" + addcustomerlocation[0] + ", longitude:" + addcustomerlocation[1], toast.length_short) .show(); } }); btnreset.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub marker.remove(); toast.maketext(getactivity(), "marker reset", toast.length_short).show(); markeravailable = false; } }); // add marker // return v; } private void getgps() { // todo auto-generated method stub onlocationchanged(locationmanager .getlastknownlocation(locationmanager.passive_provider)); } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); bundle = savedinstancestate; setretaininstance(true); } @override public void onresume() { super.onresume(); mapview.onresume(); getgps(); } @override public void onpause() { super.onpause(); mapview.onpause(); } private void outgps() { // todo auto-generated method stub locationmanager.removeupdates(this); } @override public void ondestroy() { mapview.ondestroy(); super.ondestroy(); } @override public void onlowmemory() { super.onlowmemory(); mapview.onlowmemory(); } // @override public void onlocationchanged(location location) { // todo auto-generated method stub latlng latlng = null; if (!markeravailable) { latlng = new latlng(location.getlatitude(), location.getlongitude()); cameraupdate cameraupdate = cameraupdatefactory.newlatlngzoom( latlng, 18f); map.animatecamera(cameraupdate); addmarker(latlng); } @override public void onproviderdisabled(string provider) { // todo auto-generated method stub } @override public void onproviderenabled(final string provider) { // todo auto-generated method stub } @override public void onstatuschanged(string provider, int status, bundle extras) { // todo auto-generated method stub } private void addmarker(latlng point) { map.clear(); marker = map.addmarker(new markeroptions().position( new latlng(point.latitude, point.longitude)).title( "your location!?")); } }
here code display google map within fragment. i've put onlongclicklisteners place markers on map, can change onclick.
public class locationfragment extends fragment { mapview mmapview; private googlemap googlemap; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflat , return layout view v = inflater.inflate(r.layout.fragment_location, container, false); mmapview = (mapview) v.findviewbyid(r.id.map); mmapview.oncreate(savedinstancestate); mmapview.onresume();// needed map display try { mapsinitializer.initialize(getactivity().getapplicationcontext()); } catch (exception e) { e.printstacktrace(); } googlemap = mmapview.getmap(); setupmap(); addingmarkers(); addingcircles(); setonlongclicklistenerforaddingnewmarkers(); return v; } private void setupmap() { // show location button on google maps googlemap.setmylocationenabled(true); locationmanager locationmanager = mainactivity.locationmanager; criteria criteria = new criteria(); final location location = locationmanager.getlastknownlocation(locationmanager.getbestprovider(criteria, false)); if (location != null) { googlemap.animatecamera(cameraupdatefactory.newlatlngzoom( new latlng(location.getlatitude(), location.getlongitude()), 13)); cameraposition cameraposition = new cameraposition.builder() .target(new latlng(location.getlatitude(), location.getlongitude())) // sets center of map location user .zoom(17) // sets zoom .bearing(90) // sets orientation of camera east .tilt(40) // sets tilt of camera 30 degrees .build(); // creates cameraposition builder googlemap.animatecamera(cameraupdatefactory.newcameraposition(cameraposition)); } } private void addingmarkers() { // adding marker locations - marker shipra mall, close house. zoom out see marker double latitude = 28.6322269; double longitude = 77.3671697; // create marker markeroptions marker = new markeroptions().position( new latlng(latitude, longitude)).title("shipra mall"); // changing marker icon marker.icon(bitmapdescriptorfactory .defaultmarker(bitmapdescriptorfactory.hue_green)); googlemap.addmarker(marker); // end - adding marker location // marker district center in janakpuri double latitude2 = 28.6292303; double longitude2 = 77.0805496; markeroptions marker2 = new markeroptions().position( new latlng(latitude2, longitude2)).title("district center"); marker2.icon(bitmapdescriptorfactory .defaultmarker(bitmapdescriptorfactory.hue_blue)); googlemap.addmarker(marker2); // end - marker district center in janakpuri } private void addingcircles() { // latitude , longitude of pacific mall subash nagar double latitude = 28.6438947; double longitude = 77.1128296; // adding circle around pacific mall of 1000 meters radius. googlemap.addcircle(new circleoptions() .center(new latlng(latitude, longitude)) // setting center point .radius(1000) // in meters .strokecolor(color.red) // color of border of circle .fillcolor(r.color.semi_blue) // colour of fill, i've set color semi transparent blue i've defined in colors.xml. fill colour use semi transparent colour. .strokewidth(10)); // width of red border. } private void setonlongclicklistenerforaddingnewmarkers() { googlemap.setonmaplongclicklistener(new googlemap.onmaplongclicklistener() { @override public void onmaplongclick(latlng latlng) { // method add new marker wherever user long clicks on map. // title of marker set based on located @ location. // geocoder.getfromlocation() method gets address , details of location. // find details of location geocoder geocoder; list<address> addresses = null; geocoder = new geocoder(getactivity().getapplicationcontext(), locale.getdefault()); try { addresses = geocoder.getfromlocation(latlng.latitude, latlng.longitude, 1); } catch (ioexception e) { e.printstacktrace(); } assert addresses != null; string address = addresses.get(0).getaddressline(0); string city = addresses.get(0).getlocality(); string state = addresses.get(0).getadminarea(); string country = addresses.get(0).getcountryname(); string postalcode = addresses.get(0).getpostalcode(); string knownname = addresses.get(0).getfeaturename(); // end - find details of location // add new marker map markeroptions mo = new markeroptions(); mo.position(latlng); mo.title(knownname + ", " + address + ", " + city + ", " + state + ", " + country); mo.icon(bitmapdescriptorfactory .defaultmarker(new random().nextfloat() * 360)); googlemap.addmarker(mo); } }); } @override public void onresume() { super.onresume(); mmapview.onresume(); } @override public void onpause() { super.onpause(); mmapview.onpause(); } @override public void ondestroy() { super.ondestroy(); mmapview.ondestroy(); } @override public void onlowmemory() { super.onlowmemory(); mmapview.onlowmemory(); } }
i've added fragment mainactivity.
you need add permissions manifest , specify google maps api key. manifest file is:
<?xml version="1.0" encoding="utf-8"?> <manifest package="your_package" xmlns:android="http://schemas.android.com/apk/res/android"> <uses-permission android:name="android.permission.internet"/> <uses-permission android:name="android.permission.access_network_state"/> <uses-permission android:name="android.permission.write_external_storage"/> <uses-permission android:name="com.google.android.providers.gsf.permission.read_gservices"/> <!-- access_coarse/fine_location permissions not required use google maps android api v2, recommended. --> <uses-permission android:name="android.permission.access_coarse_location"/> <uses-permission android:name="android.permission.access_fine_location"/> <permission android:name="com.arshad.map.permission.maps_receive" android:protectionlevel="signature"/> <!-- permission receive google maps --> <uses-permission android:name="com.arshad.map.permission.maps_receive"/> <!-- maps api needs opengl es 2.0. --> <uses-feature android:glesversion="0x00020000" android:required="true"/> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme"> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/> <meta-data android:name="com.google.android.maps.v2.api_key" android:value="@string/google_maps_key"/> <activity android:name=".mainactivity" android:label="@string/title_activity_main"> <intent-filter> <action android:name="android.intent.action.main"/> <category android:name="android.intent.category.launcher"/> </intent-filter> </activity> </application> </manifest>
and you're go.
Comments
Post a Comment