Blue dot and circle is not shown on MyLocation using android fused location api -
i using locationmanager track user current location, after changing location manager fusedlocation api, blue dot , circle not shown after setting map.setmylocationenabled(true)
. can see current location icon on top right corner in map fragment clicking on nothing. reverted code locationmanager able see blue dot pointing current location. wrong using fused location api.
for targeting api-23 or higher
see this answer....
for targeting api-22 , lower:
this code works me, has mylocation
blue dot/circle, , places marker
on current location using fused location provider.
here entire activity code used:
import android.support.v7.app.appcompatactivity; import android.os.bundle; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.supportmapfragment; import android.location.location; import android.widget.toast; import com.google.android.gms.common.connectionresult; import com.google.android.gms.common.api.googleapiclient; import com.google.android.gms.location.locationrequest; import com.google.android.gms.location.locationservices; import com.google.android.gms.location.locationlistener; import com.google.android.gms.maps.model.bitmapdescriptorfactory; import com.google.android.gms.maps.model.latlng; import com.google.android.gms.maps.model.marker; import com.google.android.gms.maps.model.markeroptions; import com.google.android.gms.maps.onmapreadycallback; public class mainactivity extends appcompatactivity implements googleapiclient.connectioncallbacks, googleapiclient.onconnectionfailedlistener, locationlistener, onmapreadycallback { locationrequest mlocationrequest; googleapiclient mgoogleapiclient; latlng latlng; googlemap mgooglemap; supportmapfragment mfragment; marker mcurrlocation; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mfragment = (supportmapfragment) getsupportfragmentmanager().findfragmentbyid(r.id.map); mfragment.getmapasync(this); } @override public void onmapready(googlemap googlemap) { mgooglemap = googlemap; mgooglemap.setmylocationenabled(true); buildgoogleapiclient(); mgoogleapiclient.connect(); } @override public void onpause() { super.onpause(); //unregister location callbacks: if (mgoogleapiclient != null) { locationservices.fusedlocationapi.removelocationupdates(mgoogleapiclient, this); } } protected synchronized void buildgoogleapiclient() { toast.maketext(this,"buildgoogleapiclient",toast.length_short).show(); mgoogleapiclient = new googleapiclient.builder(this) .addconnectioncallbacks(this) .addonconnectionfailedlistener(this) .addapi(locationservices.api) .build(); } @override public void onconnected(bundle bundle) { toast.maketext(this,"onconnected",toast.length_short).show(); location mlastlocation = locationservices.fusedlocationapi.getlastlocation( mgoogleapiclient); if (mlastlocation != null) { //place marker @ current position mgooglemap.clear(); latlng = new latlng(mlastlocation.getlatitude(), mlastlocation.getlongitude()); markeroptions markeroptions = new markeroptions(); markeroptions.position(latlng); markeroptions.title("current position"); markeroptions.icon(bitmapdescriptorfactory.defaultmarker(bitmapdescriptorfactory.hue_magenta)); mcurrlocation = mgooglemap.addmarker(markeroptions); } mlocationrequest = new locationrequest(); mlocationrequest.setinterval(5000); //5 seconds mlocationrequest.setfastestinterval(3000); //3 seconds mlocationrequest.setpriority(locationrequest.priority_balanced_power_accuracy); //mlocationrequest.setsmallestdisplacement(0.1f); //1/10 meter locationservices.fusedlocationapi.requestlocationupdates(mgoogleapiclient, mlocationrequest, this); } @override public void onconnectionsuspended(int i) { toast.maketext(this,"onconnectionsuspended",toast.length_short).show(); } @override public void onconnectionfailed(connectionresult connectionresult) { toast.maketext(this,"onconnectionfailed",toast.length_short).show(); } @override public void onlocationchanged(location location) { //remove previous current location marker , add new 1 @ current position if (mcurrlocation != null) { mcurrlocation.remove(); } latlng = new latlng(location.getlatitude(), location.getlongitude()); markeroptions markeroptions = new markeroptions(); markeroptions.position(latlng); markeroptions.title("current position"); markeroptions.icon(bitmapdescriptorfactory.defaultmarker(bitmapdescriptorfactory.hue_magenta)); mcurrlocation = mgooglemap.addmarker(markeroptions); toast.maketext(this,"location changed",toast.length_short).show(); //if need 1 location, unregister listener //locationservices.fusedlocationapi.removelocationupdates(mgoogleapiclient, this); } }
activity_main.xml:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".mainactivity"> <fragment class="com.google.android.gms.maps.supportmapfragment" android:id="@+id/map" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </relativelayout>
result:
Comments
Post a Comment