目前正在开发一个
Android应用程序,它将最近的20位置返回给用户当前位置.
Google Places API返回约20个位置靠近用户位置,但不是最近的20个按距离排序.
看Google Places API Documentation并没有显示任何我认为不正确的内容.
GetPlaces.java
String types = "accounting|airport|amusement_park|aquarium|art_gallery|atm|bakery|bank|bar|beauty_salon|bicycle_store|book_store|bowling_alley|bus_station|cafe|campground|car_dealer|car_rental|car_repair|car_wash|casino|cemetery|church|city_hall|clothing_store|convenience_store|courthouse|dentist|department_store|doctor|electrician|electronics_store|embassy|establishment|finance|fire_station|florist|food|funeral_home|furniture_store|gas_station|general_contractor|grocery_or_supermarket|gym|hair_care|hardware_store|health|hindu_temple|home_goods_store|hospital|insurance_agency|jewelry_store|laundry|lawyer|library|liquor_store|local_government_office|locksmith|lodging|meal_delivery|meal_takeaway|mosque|movie_rental|movie_theater|moving_company|museum|night_club|painter|park|parking|pet_store|pharmacy|physiotherapist|place_of_worship|plumber|police|post_office|real_estate_agency|restaurant|roofing_contractor|rv_park|school|shoe_store|shopping_mall|spa|stadium|storage|store|subway_station|synagogue|taxi_stand|train_station|travel_agency|university|veterinary_care|zoo"; resourceURI = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location="+myLocation.latitude+","+myLocation.longitude+"&radius=500&rankBy=distance&types="+ URLEncoder.encode(types,"UTF-8")+"&sensor=true&key=GOOGLE_MAPS_KEY"; try { String url =resourceURI; //getURL(myLocation.latitude,myLocation.longitude); HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams,30000); HttpConnectionParams.setSoTimeout(httpParams,30000); DefaultHttpClient httpClient = new DefaultHttpClient(httpParams); HttpGet httpGet = new HttpGet(url); httpGet.setHeader("Content-type","application/json"); ResponseHandler responseHandler = new BasicResponseHandler(); String response = (String) httpClient.execute(httpGet,responseHandler); if (response != null) { mResult = new JSONObject(response); results = mResult.getJSONArray("results"); } } catch (ClientProtocolException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; } catch (JSONException e) { e.printStackTrace(); return null; } return results; }
这将返回有效的JSON,但不是返回传入距离的最近位置.我知道有一个事实,那里的地方比请求返回的地方更近.
例如,我在一个已知的谷歌地方提出请求,但它没有显示我目前所在的地方 – 但其他更远的地方.
解决方法
也许你已经解决了你的问题,但我希望这可以帮助(专注于粗体):
radius – 定义返回Place结果的距离(以米为单位).允许的最大宽度为50 000米.注意如果指定了rankby = distance(在可选参数下面描述),则不得包括半径.
rankby – 指定列出结果的顺序.可能的值是:
突出(默认).此选项根据结果的重要性对结果进行排序.排名将有利于指定区域内的显着位置.地方在Google索引中的排名,应用程序的签到数量,全球受欢迎程度以及其他因素都会影响显着性.
距离.此选项按距离指定位置的距离按升序对结果进行排序.指定距离时,需要一个或多个关键字,名称或类型.
根据:https://developers.google.com/places/documentation/search?hl=en
我已经根据谷歌文档了解到你不能同时发送参数“rankby”和“radius”,你必须同时只使用其中一个,这样你就可以得到按距离排序的结果.
测试请求执行此操作:
resourceURI = "https://maps.googleapis.com/maps/api/place/nearbysearch/json? location="+myLocation.latitude+","+myLocation.longitude+"&rankBy=distance &types="+ URLEncoder.encode (types,"UTF-8") + "&sensor = true& key = GOOGLE_MAPS_KEY";
祝你好运,祝你好运!