android - My onLocationChanged is not working properly. What should I do? -
my onlocationchanged function being called first time . change location not getting in google map , not getting marker movement depending upon current location .
package com.example.swap.gps; import android.manifest; import android.content.pm.packagemanager; import android.graphics.color; import android.location.location; import android.os.build; import android.os.bundle; import android.support.annotation.nonnull; import android.support.v4.app.activitycompat; import android.support.v4.app.fragmentactivity; import android.support.v4.content.contextcompat; import android.widget.toast; import com.google.android.gms.common.connectionresult; import com.google.android.gms.common.googleplayservicesutil; import com.google.android.gms.common.api.googleapiclient; import com.google.android.gms.location.locationlistener; import com.google.android.gms.location.locationrequest; import com.google.android.gms.location.locationservices; import com.google.android.gms.maps.cameraupdatefactory; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.onmapreadycallback; import com.google.android.gms.maps.supportmapfragment; import com.google.android.gms.maps.model.bitmapdescriptorfactory; import com.google.android.gms.maps.model.cameraposition; import com.google.android.gms.maps.model.circleoptions; import com.google.android.gms.maps.model.latlng; import com.google.android.gms.maps.model.marker; import com.google.android.gms.maps.model.markeroptions; public class mapsactivity extends fragmentactivity implements onmapreadycallback, locationlistener, googleapiclient.connectioncallbacks, googleapiclient.onconnectionfailedlistener{ private googlemap mmap; googleapiclient mgoogleapiclient; location mlastlocation; locationrequest mlocationrequest; marker marker; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //show error dialog if goolgleplayservices not available if (!isgoogleplayservicesavailable()) { finish(); } setcontentview(r.layout.activity_maps); if (android.os.build.version.sdk_int >= build.version_codes.m) { checklocationpermission(); } // obtain supportmapfragment , notified when map ready used. supportmapfragment mapfragment = (supportmapfragment) getsupportfragmentmanager() .findfragmentbyid(r.id.map); mapfragment.getmapasync(this); } public static final int my_permissions_request_location = 99; public boolean checklocationpermission(){ if (contextcompat.checkselfpermission(this, manifest.permission.access_fine_location) != packagemanager.permission_granted) { // asking user if explanation needed if (activitycompat.shouldshowrequestpermissionrationale(this, manifest.permission.access_fine_location)) { // show explanation user *asynchronously* -- don't block // thread waiting user's response! after user // sees explanation, try again request permission. //prompt user once explanation has been shown activitycompat.requestpermissions(this, new string[]{manifest.permission.access_fine_location}, my_permissions_request_location); } else { // no explanation needed, can request permission. activitycompat.requestpermissions(this, new string[]{manifest.permission.access_fine_location}, my_permissions_request_location); } return false; } else { return true; } } /** * manipulates map once available. * callback triggered when map ready used. * can add markers or lines, add listeners or move camera. in case, * add marker near sydney, australia. * if google play services not installed on device, user prompted install * inside supportmapfragment. method triggered once user has * installed google play services , returned app. */ @override public void onmapready(googlemap googlemap) { mmap = googlemap; mmap.setmaptype(googlemap.map_type_normal); //initialize google play services if (android.os.build.version.sdk_int >= build.version_codes.m) { if (contextcompat.checkselfpermission(this, manifest.permission.access_fine_location) == packagemanager.permission_granted) { buildgoogleapiclient(); mmap.setmylocationenabled(true); } } else { buildgoogleapiclient(); mmap.setmylocationenabled(true); } latlng sahidkhudiram = new latlng(22.466 , 88.3918); showmap(sahidkhudiram,"sahidkhudiram metro"); latlng kavinazrul = new latlng(22.4641823 , 88.38060670000004); showmap(kavinazrul,"kavinazrul metro"); } protected synchronized void buildgoogleapiclient() { mgoogleapiclient = new googleapiclient.builder(this) .addconnectioncallbacks(this) .addonconnectionfailedlistener(this) .addapi(locationservices.api) .build(); mgoogleapiclient.connect(); } private void showmap(latlng latlng, string place){ cameraposition cameraposition = new cameraposition.builder().target( latlng).zoom(14).tilt(60).bearing(30).build(); marker = mmap.addmarker(new markeroptions().position(latlng).title(place)); marker.showinfowindow(); mmap.animatecamera(cameraupdatefactory.newcameraposition(cameraposition)); mmap.addcircle(new circleoptions().center(latlng). radius(90). strokewidth(3). strokecolor(color.red). fillcolor(color.transparent)); } @override public void onlocationchanged(location location) { toast.maketext(this,"new location found",toast.length_short).show(); mlastlocation = location; //place current location marker latlng latlng = new latlng(location.getlatitude(), location.getlongitude()); showmap(latlng,"me"); //stop location updates if (mgoogleapiclient != null) { locationservices.fusedlocationapi.removelocationupdates(mgoogleapiclient, this); } } private boolean isgoogleplayservicesavailable() { int status = googleplayservicesutil.isgoogleplayservicesavailable(this); if (connectionresult.success == status) { return true; } else { googleplayservicesutil.geterrordialog(status, this, 0).show(); return false; } } @override public void onconnected(bundle bundle) { mlocationrequest = new locationrequest(); mlocationrequest.setinterval(1000); mlocationrequest.setfastestinterval(1000); mlocationrequest.setpriority(locationrequest.priority_balanced_power_accuracy); if (contextcompat.checkselfpermission(this, manifest.permission.access_fine_location) == packagemanager.permission_granted) { locationservices.fusedlocationapi.requestlocationupdates(mgoogleapiclient, mlocationrequest, this); } } @override public void onconnectionsuspended(int i) { } @override public void onconnectionfailed(connectionresult connectionresult) { } @override public void onrequestpermissionsresult(int requestcode, @nonnull string[] permissions, @nonnull int[] grantresults) { switch (requestcode) { case my_permissions_request_location: { // if request cancelled, result arrays empty. if (grantresults.length > 0 && grantresults[0] == packagemanager.permission_granted) { // permission granted. // contacts-related task need do. if (contextcompat.checkselfpermission(this, manifest.permission.access_fine_location) == packagemanager.permission_granted) { if (mgoogleapiclient == null) { buildgoogleapiclient(); } mmap.setmylocationenabled(true); } } else { // permission denied, disable functionality depends on permission. toast.maketext(this, "permission denied", toast.length_long).show(); } return; } // other 'case' lines check other permissions app might request. // can add here other case statements according requirement. } }
}
in onlocationchanged
method, remove location updates. means receive 1 update. code:
//stop location updates if (mgoogleapiclient != null) { locationservices.fusedlocationapi.removelocationupdates(mgoogleapiclient, this); }
Comments
Post a Comment