English 中文(简体)
Google 地图 api v3 如何排列最近距离
原标题:google maps api v3 how to rank by closest distance

Does anyone know how to use the rank by distance search option that is mentioned here? 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]);
        }
    } 
}

代码会定位并列出结果, 如果我包括一个半径, 但结果没有按距离以升序排列。 Google s docs 说如果使用等级By 选项, 半径也没有必要 。 我是不是漏掉了?

最佳回答

Was having the same problem. As per this source: http://www.geocodezip.com/v3_GoogleEx_place-search.html I was able to formulate the query as such:

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 
};

Var 键是 API 键,不需要它,但添加后再使用。 GPS是:

var gps = new google.maps.LatLng(location.lat,location.lon);

最后,我做了你所做的一切,除了我添加了地图的界限,我不打算使用。

var bounds = new google.maps.LatLngBounds();
问题回答

您不能同时使用半径和 RankBy.Disignance 属性。 因此您有两个选项 :

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]);
       }
   } 
}

选项 2: 由 RankBy 搜索。 偏差然后用半径限制结果。 您再次需要 harnsine 公式来计算距离 。

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;
 }

您不能同时使用半径 + 排名By 属性 。

如果您需要最接近的位置,请选择某些类型,并设置等级By proprety。

places.nearbySearch({
                        location: LatLng,
                        types: placeTypes,
                        rankBy: google.maps.places.RankBy.DISTANCE
                    },
                    function (results) {
                        // process the results, r[0] is the closest place
                    }
                );

在哪里可以设置这样的位置Typepes

var placeTypes = [
 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 

; ; ; ; ;

请注意,在目前的 Api Doc 上, Places Service 类 < strong > 上的“ 搜索” 方法不存在 < /strong > 。

https://developers.google.com/maps/document/javascription/reference?hl=it#Places Servicice

你必须选择赌注:

  • nearbySearch (retrieve 20 result per page, max 3 pages)
  • radarSearch (retrieve 200 result but with less details)
  • textSearch (similar to the nearbySearch)

< 坚固> 如果您选择排序 By 。 您无法设置半径

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

placesService.nearbySearch(request, function (data, status, placeSearchPagination) {
  if (status == google.maps.places.PlacesServiceStatus.OK) {
  //...
  // do your stuffs with data results
  //...
  if (placeSearchPagination && placeSearchPagination.hasNextPage) {
    placeSearchPagination.nextPage();
  } 
});

如果您想要限制基于距离半径的数据, 您可以使用此函数来检查转盘是否太远 。

function checkRadiusDistance(place,centerLatLng,radius) {
  return google.maps.geometry.spherical.computeDistanceBetween(place.geometry.location, centerLatLng) < radius;
});

请注意,当您指定“ irkBy:google.maps.places.RankBy.PROMINEC ”和“em>radiratus=xx ”和“em>regy>tradus=xx ”时,这也是在一定半径 内指定“em>irkBy:google.maps.places.places.RankBy.places.RankBy.PROmineence 中,服务公司给您提供 和定义区域外的结果 之外的结果的唯一方法。





相关问题
How to decide the current point reach on google map?

How to decide the current point reach on google map? I have a list of points (pickup points) of a route that I want to show in my google map with polyline. Now i have to get the current location of ...

Topographical or relief data in Map APIs

I was wondering if anyone knew of any map APIs that offer topographical or relief data? I ve had a quick look at Google and Bing APIs, but could find nothing there. Google allow you to view a map as ...

Using maps on Windows Mobile

I m experimenting with maps on different mobile platforms. Getting Google Maps to work on Android was easy, following this tutorial. Getting the same to work on Windows Mobile is a different matter. ...

Adding a custom icon to a google map

I need a hand adding a custom icon to some Google Maps javascript. Code below for your reference: function populateMap() { var map = new GMap2(document.getElementById("map")); map.setCenter(new ...

RSS to KML Overlay

I m want to display my blog as a Google Map overlay (each post contains geotags). How can I dynamically create a KML overlay from an RSS? Or better, how can I create a loop (PHP) that would display ...

开放街道地图管理员

我需要开放Street的标记管理员。 地图,如山角地图。

热门标签