Android Places Autocomplete set lat long bounds -


i using google places autocomplete api. application has autocomplete text view. working fine followed example here .

only issue being setting latlng bounds of mountain view.

  private static final latlngbounds bounds_mountain_view = new latlngbounds(         new latlng(37.398160, -122.180831), new latlng(37.430610, -121.972090)); 

my googleapiclient

 mgoogleapiclient = new googleapiclient.builder(this)              .addapi(places.geo_data_api)              .build(); 

when try enter new orleans address, have type lot not experience user out of ca. there better way set latlng bounds depending upon current location without asking location permission or precise location position. assuming not limitation of places api limited knowledge.

thanks in advance.

unfortunately no, need location permission since permission choose in manifest determines accuracy of location returned google api client. suggest using access_coarse_location accurate within 1 city block. here how dynamically got user's location , made bound of approx. 5 mile radius around location.

public class placessearchactivity extends appcompatactivity implements googleapiclient.onconnectionfailedlistener,googleapiclient.connectioncallbacks, locationlistener {      private string tag = this.tostring();     protected googleapiclient mgoogleapiclient;     private autocompletetextview mautocompleteview;     private placeautocompleteadapter madapter;     private locationrequest mlocationrequest;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_places_search);          // construct googleapiclient {@link places#geo_data_api} using automanage         // functionality, automatically sets api client handle activity lifecycle         // events. if activity not extend fragmentactivity, make sure call connect()         // , disconnect() explicitly.         mgoogleapiclient = new googleapiclient.builder(this)                 .addconnectioncallbacks(this)                 .addonconnectionfailedlistener(this)                 .enableautomanage(this, 0 /* clientid */, this)                 .addapi(places.geo_data_api)                 .addapi(locationservices.api)                 .build();         // create locationrequest object         mlocationrequest = locationrequest.create()                 .setpriority(locationrequest.priority_low_power)                 .setinterval(10 * 1000)        // 10 seconds, in milliseconds                 .setfastestinterval(1 * 1000); // 1 second, in milliseconds         // retrieve autocompletetextview display place suggestions.         mautocompleteview = (autocompletetextview)findviewbyid(r.id.autocomplete_places);         // register listener receives callbacks when suggestion has been selected         mautocompleteview.setonitemclicklistener(mautocompleteclicklistener);     }      private void setbounds(location location, int mdistanceinmeters ){         double latradian = math.toradians(location.getlatitude());          double deglatkm = 110.574235;         double deglongkm = 110.572833 * math.cos(latradian);         double deltalat = mdistanceinmeters / 1000.0 / deglatkm;         double deltalong = mdistanceinmeters / 1000.0 / deglongkm;          double minlat = location.getlatitude() - deltalat;         double minlong = location.getlongitude() - deltalong;         double maxlat = location.getlatitude() + deltalat;         double maxlong = location.getlongitude() + deltalong;          log.d(tag,"min: "+double.tostring(minlat)+","+double.tostring(minlong));         log.d(tag,"max: "+double.tostring(maxlat)+","+double.tostring(maxlong));          // set adapter retrieve suggestions places geo data api cover         // entire world.         madapter = new placeautocompleteadapter(this, android.r.layout.simple_list_item_1,                 mgoogleapiclient, new latlngbounds(new latlng(minlat, minlong), new latlng(maxlat, maxlong)), null);         mautocompleteview.setadapter(madapter);     }      /**      * listener handles selections suggestions autocompletetextview      * displays place suggestions.      * gets place id of selected item , issues request places geo data api      * retrieve more details place.      *      * @see com.google.android.gms.location.places.geodataapi#getplacebyid(com.google.android.gms.common.api.googleapiclient,      * string...)      */     private adapterview.onitemclicklistener mautocompleteclicklistener = new adapterview.onitemclicklistener() {         @override         public void onitemclick(adapterview<?> parent, view view, int position, long id) {             /*              retrieve place id of selected item adapter.              adapter stores each place suggestion in placeautocomplete object              read place id.               */             final placeautocompleteadapter.placeautocomplete item = madapter.getitem(position);             final string placeid = string.valueof(item.placeid);             log.i(tag, "autocomplete item selected: " + item.description);              /*              issue request places geo data api retrieve place object additional               details place.               */             pendingresult<placebuffer> placeresult = places.geodataapi                     .getplacebyid(mgoogleapiclient, placeid);             placeresult.setresultcallback(mupdateplacedetailscallback);              toast.maketext(getapplicationcontext(), "clicked: " + item.description,                     toast.length_short).show();             log.i(tag, "called getplacebyid place details " + item.placeid);         }     };      /**      * callback results places geo data api query shows first place result in      * details view on screen.      */     private resultcallback<placebuffer> mupdateplacedetailscallback             = new resultcallback<placebuffer>() {         @override         public void onresult(placebuffer places) {             if (!places.getstatus().issuccess()) {                 // request did not complete                 log.e(tag, "place query did not complete. error: " + places.getstatus().tostring());                 places.release();                 return;             }             // place object buffer.             final place place = places.get(0);              log.i(tag, "place details received: " + place.getname());              places.release();         }     };      @override     protected void onstart() {         super.onstart();         mgoogleapiclient.connect();     }      @override     protected void onstop() {         super.onstop();         if (mgoogleapiclient.isconnected()) {             locationservices.fusedlocationapi.removelocationupdates(mgoogleapiclient, this);             mgoogleapiclient.disconnect();         }     }      @override     public void onconnectionfailed(connectionresult connectionresult) {         log.e(tag, "onconnectionfailed: connectionresult.geterrorcode() = " + connectionresult.geterrorcode());          // todo(developer): check error code , notify user of error state , resolution.         toast.maketext(this,"could not connect google api client: error " + connectionresult.geterrorcode(),toast.length_short).show();     }      @override     public void onlocationchanged(location location) {         setbounds(location,5500);     }      @override     public void onconnected(bundle bundle) {         location location = locationservices.fusedlocationapi.getlastlocation(mgoogleapiclient);          if (location == null) {             locationservices.fusedlocationapi.requestlocationupdates(mgoogleapiclient, mlocationrequest, this);         } else {             setbounds(location,5500);         }     }      @override     public void onconnectionsuspended(int i) {      } } 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -