google maps api v3如何按最近距离排名

JFl*_*Flo 8 search google-maps rank

有谁知道如何使用这里提到的距离搜索选项的排名? https://developers.google.com/maps/documentation/javascript/places#place_search_requests

在请求选项中列出这个似乎不起作用.这是我相对于此的代码部分:

var request = {
  location: coords,
  //radius: 30000,
  keyword: ['puma, retail'],
  types: ['store'],
  rankBy: google.maps.places.RankBy.DISTANCE
};

service = new google.maps.places.PlacesService(map);
service.search(request, callback);

function callback(results, status) {
    if (status == google.maps.places.PlacesServiceStatus.OK) {
        for (var i = 0; i < results.length; i++) {
            createMarker(results[i]);
                            listResults(results[i]);
        }
    } 
}
Run Code Online (Sandbox Code Playgroud)

如果我包含半径,代码将定位并列出结果,但结果不按距离的升序列出.谷歌的文档说,如果使用rankBy选项,则不需要半径.我错过了什么吗?

小智 13

您不能同时使用radius和RankBy.DISTANCE属性.因此,您有两种选择:

1)按半径搜索,然后在您自己的代码中按距离对结果进行排序.

例:

var request = {
               location: coords,
               radius: 30000,
               keyword: ['puma, retail'],
               types: ['store']
               };
service = new google.maps.places.PlacesService(map);
service.search(request, callback);

function callback(results, status) {
         if (status == google.maps.places.PlacesServiceStatus.OK) {
          for (var i = 0; i < results.length; i++) {
           sortresults(results[i]);//sortresult uses haversine to calcuate distance and then arranges the result in the order of distance
           createMarker(results[i]);
           listResults(results[i]);
       }
   } 
}
Run Code Online (Sandbox Code Playgroud)

选项2:按RankBy.Distance搜索,然后使用半径限制结果.您需要再次使用半正式公式来计算距离.

var request = {
               location: coords,
               rankBy: google.maps.places.RankBy.DISTANCE,
               keyword: ['puma, retail'],
               types: ['store']
               };
service = new google.maps.places.PlacesService(map);
service.search(request, callback);

function callback(results, status) {
         if (status == google.maps.places.PlacesServiceStatus.OK) {

           for (var i = 0; i < results.length; i++) {
           d= distance(coords,results[i].latlng)
           if(d<rd)
            {createMarker(results[i]);
             listResults(results[i]);
            }
           }
   } 
}

//Returns Distance between two latlng objects using haversine formula
distance(p1, p2) {
 if (!p1 || !p2) 
  return 0;
 var R = 6371000; // Radius of the Earth in m
 var dLat = (p2.lat() - p1.lat()) * Math.PI / 180;
 var dLon = (p2.lng() - p1.lng()) * Math.PI / 180;
 var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
 Math.cos(p1.lat() * Math.PI / 180) * Math.cos(p2.lat() * Math.PI / 180) *
 Math.sin(dLon / 2) * Math.sin(dLon / 2);
 var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
 var d = R * c;
 return d;
 }
Run Code Online (Sandbox Code Playgroud)


bru*_*sty 8

有同样的问题.根据此来源:http: //www.geocodezip.com/v3_GoogleEx_place-search.html 我能够如此制定查询:

var request = {
location: gps,
types: ['food'], //You can substitute "keyword: 'food'," (without double-quotes) here as well.
rankBy: google.maps.places.RankBy.DISTANCE, //Note there is no quotes here, I made that mistake.
key: key 
};
Run Code Online (Sandbox Code Playgroud)

Var键是API密钥,它不是必需的,但添加后供使用.GPS是:

var gps = new google.maps.LatLng(location.lat,location.lon);
Run Code Online (Sandbox Code Playgroud)

最后,我做了你所做的一切,除了我添加了一个我不打算使用的地图边界.为此我做了:

var bounds = new google.maps.LatLngBounds();
Run Code Online (Sandbox Code Playgroud)