android - LocationServices.FusedLocationApi.getLastLocation(googleApiClient) is always null on emulator -
i don't know whats wrong emulator. can't take locatioan command
locationservices.fusedlocationapi.getlastlocation(googleapiclient)
in emulator supported gps , google play service nothing works taking current locaion. location = null. need help. here code :
private void configuregoogleapiclient() { locationrequest = locationrequest.create(); locationrequest.setpriority(locationrequest.priority_high_accuracy); locationrequest.setinterval(constant.interval); locationrequest.setfastestinterval(constant.fastinterval); googleapiclient = new googleapiclient.builder(this) .addapi(locationservices.api) .addconnectioncallbacks(this) .addonconnectionfailedlistener(this) .build(); if (googleapiclient != null) { googleapiclient.connect(); } } @override protected void onstart() { super.onstart(); if (googleapiclient != null) { googleapiclient.connect(); } } protected void onstop() { // disconnecting client invalidates it. if (googleapiclient != null) { googleapiclient.disconnect(); } super.onstop(); } @override protected void onpause() { super.onpause(); if (googleapiclient != null) { googleapiclient.disconnect(); } } @override protected void onresume() { super.onresume(); if (manager.isproviderenabled(locationmanager.gps_provider)) { if (googleapiclient != null) googleapiclient.connect(); } } @override public void onconnected(@nullable bundle bundle) { if (googleapiclient.isconnected()) { log.e(mainactivity.class.getsimplename(), "is connected"); } else log.e(mainactivity.class.getsimplename(), "is not connected"); if (manager.isproviderenabled(locationmanager.gps_provider)) { log.e(mainactivity.class.getsimplename(), "is connected on gps"); } else { log.e(mainactivity.class.getsimplename(), "is not connected on gps"); } utility.checkpermission(this); location = locationservices.fusedlocationapi.getlastlocation(googleapiclient); if (location == null) { utility.checkpermission(this); locationservices.fusedlocationapi.requestlocationupdates(googleapiclient, locationrequest, this); } if (location != null) { lattext.settext("lat: " + string.valueof(location.getlatitude())); longtext.settext("long: " + string.valueof(location.getlongitude())); } else { lattext.settext(getstring(r.string.error_find_location)); longtext.settext(getstring(r.string.error_find_location)); } } @override public void onconnectionsuspended(int i) { } @override public void onconnectionfailed(@nonnull connectionresult connectionresult) { log.e(mainactivity.class.getsimplename(), connectionresult.tostring()); } @override public void onlocationchanged(location location) { currentlocation = location; if (googleapiclient != null) if (googleapiclient.isconnected() || googleapiclient.isconnecting()) { googleapiclient.disconnect(); googleapiclient.connect(); } else if (!googleapiclient.isconnected()) { googleapiclient.connect(); } string msg = "updated location: " + double.tostring(location.getlatitude()) + "," + double.tostring(location.getlongitude()); toast.maketext(this, msg, toast.length_short).show(); configureview(); }
my guess last location shows last location. doesn't try figure out location. on phone app requested location. so, there last location.
i've added code request location if there none.
location userlocation = locationservices.fusedlocationapi.getlastlocation(googleapiclient); if (userlocation == null) { locationrequest locationrequest = new locationrequest(); locationrequest.setnumupdates(1); locationrequest.setinterval(0); locationrequest.setpriority(priority_high_accuracy); locationservices.fusedlocationapi.requestlocationupdates( googleapiclient, locationrequest, new locationlistener() { @override public void onlocationchanged(location location) { locationservices.fusedlocationapi.removelocationupdates( googleapiclient, this); } });
Comments
Post a Comment