【发布时间】:2014-05-08 17:14:42
【问题描述】:
目前正在开发一个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,但不会返回与传入的 distance 最接近的位置。我知道有一个比请求返回的地方更近的地方。
例如,我在一个已知的 google 地点提出请求,但它没有显示我当前所在的地点,而是显示更远的其他地点。
【问题讨论】:
-
我看到这是一个旧线程,但是 API 选择问题和按距离顺序检索地点仍然是相关的。 @Raul 注意到对结果进行排序所需的唯一更改,但我很好奇您在 Android 应用程序中用于 Web 服务的 API 密钥类型。受 IP 地址保护的服务器密钥不适用于移动应用程序,因为每部手机都有唯一的 IP。 Android 密钥不起作用,因为该应用正在模拟浏览器请求。你最终做了什么?
标签: android google-api google-places-api