目前正在开发一个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,但不会返回传入距离的最近位置。我知道有一个事实,那就是请求返回的位置更接近。
例如,我在一个已知的谷歌地方提出请求,但它没有显示我目前所处的位置 - 但是其他更远的地方。
答案 0 :(得分:10)
也许你已经解决了问题,但我希望这可以帮助(专注于大胆的内容):
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";
看你怎么做,祝你好运!
答案 1 :(得分:0)
您所考虑的地方很可能比返回的地点更接近 true Google地方。
查看此常见问题解答,了解如何在Google地图上开展业务:https://support.google.com/places/answer/145585?hl=en。
对此进行的测试可能是尝试注册您认为距离Google地点https://www.google.com/local/business/add?hl=en&migratedId=04614545856067425787更近的商家,看看它是否允许您。如果可以的话,这意味着它们不是真正的谷歌地方,并且不会出现在这个结果集中。
答案 2 :(得分:0)
希望此代码有效。.
private ArrayList<CLPlaceDTO> sortLocation(LatLng currentLatLng, ArrayList<?> alLocationDTO)
{
ArrayList<CLPlaceDTO> clPlaceDTOArrayList;
ArrayList<CLPlaceDTO> clPlaceDTOArrayList1 = new ArrayList<>();
double dCurrentLat = currentLatLng.latitude;
double dCurrentLong = currentLatLng.longitude;
Iterator<?> clIterator = alLocationDTO.iterator();
while (clIterator.hasNext())
{
clIterator.next();
clPlaceDTOArrayList = new ArrayList<>();
for (int j = 0; j < alLocationDTO.size(); j++)
{
CLLocationDTO clLocationDTO = (CLLocationDTO) alLocationDTO.get(j);
double dLat = clLocationDTO.getLatitude().doubleValue();
double dLng = clLocationDTO.getLongitude().doubleValue();
LatLng clNewLatLng = new LatLng(dLat, dLng);
double dDistance = getDistance(dCurrentLat, dCurrentLong, dLat, dLng);
CLPlaceDTO clPlaceDTO = new CLPlaceDTO(clLocationDTO.getAccountName(), clNewLatLng, dDistance);
clPlaceDTOArrayList.add(clPlaceDTO);
}
Collections.sort(clPlaceDTOArrayList, new CLSortPlaces(currentLatLng));
dCurrentLat = clPlaceDTOArrayList.get(0).getLatlng().latitude;
dCurrentLong = clPlaceDTOArrayList.get(0).getLatlng().longitude;
clPlaceDTOArrayList1.add(clPlaceDTOArrayList.get(0));
clIterator.remove();
}
return clPlaceDTOArrayList1;
}
public static double getDistance(double dbFromLatitude,double dbFromLongitude,double dbToLatitude,double dbToLongitude)
{
double dbRadiusMeters = EARTH_RADIUS * 1000 ; // Earth’s mean radius in meter
double dbLatitudeDiff = Math.toRadians(dbToLatitude - dbFromLatitude);
double dbLongitudeDiff = Math.toRadians(dbToLongitude - dbFromLongitude);
double a = Math.sin(dbLatitudeDiff / 2) * Math.sin(dbLatitudeDiff / 2) +
Math.cos(Math.toRadians(dbFromLatitude)) * Math.cos(Math.toRadians(dbToLatitude)) *
Math.sin(dbLongitudeDiff / 2) * Math.sin(dbLongitudeDiff / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double d = dbRadiusMeters * c;
return d; // returns the distance in meter
}
public class CLSortPlaces implements Comparator<CLPlaceDTO>
{
private LatLng currentLoc;
CLSortPlaces(LatLng current)
{
currentLoc = current;
}
@Override
public int compare(final CLPlaceDTO place1, final CLPlaceDTO place2)
{
return (int) (place1.dDistance - place2.dDistance);
}
}
public class CLPlaceDTO
{
public LatLng latlng;
public String sNameOfLocation;
public double dDistance;
public CLPlaceDTO(String sNameOfLocation, LatLng latlng,double dDistance)
{
this.sNameOfLocation = sNameOfLocation;
this.latlng = latlng;
this.dDistance=dDistance;
}
public CLPlaceDTO(String sNameOfLocation, LatLng latlng)
{
this.sNameOfLocation = sNameOfLocation;
this.latlng = latlng;
this.dDistance=dDistance;
}}