2012-05-23 119 views
8

有沒有人知道如何使用這裏提到的距離搜索選項的排名? https://developers.google.com/maps/documentation/javascript/places#place_search_requests谷歌地圖api v3如何按最近距離排名

在請求選項中列出此項似乎不起作用。這裏是我的親戚代碼的一部分這樣:

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的文檔說如果使用rankBy選項,則不需要半徑。我錯過了什麼嗎?

+0

我在同一條船上......尋找解決方案。 – GuiDoody

回答

7

出現同樣的問題。 按本來源: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 
}; 

瓦爾關鍵是不需要倒是供以後使用API​​密鑰。 GPS是:

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

最後,我這樣做,你做我除了加邊界的地圖,我是不打算使用的一切。爲此我做了:

var bounds = new google.maps.LatLngBounds(); 
3

您不能一起使用radius + rankBy屬性。

如果你需要最近的地點,選擇一些類型,並設置rankBy proprety。

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

在這裏你可以設置placeTypes這樣

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' 

]。

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

選項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; 
} 
+0

這對我來說絕對是正確的答案,我經歷了與@JFlo相同的問題 – David

1

請注意,當前API文檔的地方信息類「搜索」的方法不可用

https://developers.google.com/maps/documentation/javascript/reference?hl=it#PlacesService

你必須選擇愨:

  • nearbySearch(檢索每頁20結果,最多3頁)
  • radarSearch(檢索200結果,但用更少的細節)
  • TEXTSEARCH (類似於附近的搜索)

如果選擇RankBy.DISTANCE你不能設置半徑

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

如果你想限制基於您可以使用此功能來檢查resut太遠的距離半徑的數據。

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

請注意,這也是唯一的辦法獲得地方一定半徑內怎麼一回事,因爲當你指定「rankBy:google.maps.places.RankBy.PROMINENCE」和「半徑= xx「的地方服務給你也定義區域以外的結果