2012-05-23 28 views
8

ここで言及している距離検索オプションでランクを使用する方法を知っている人はいますか? https://developers.google.com/maps/documentation/javascript/places#place_search_requestsgoogle maps 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

同じ問題がありました。このソースを1として :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 
}; 

Varのキーは必要なく、後で使用するために追加されていないAPIキーです。 GPSは:

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

最後に、使用しない地図に境界線を追加した以外は、あなたが行ったことすべてを実行しました。そのためには、

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

radius + rankByプロパティを一緒に使用することはできません。

最も近い場所が必要な場合は、いくつかのタイプを選択し、rankByのプロパティを設定します。あなたはこの

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' 

]のようなplaceTypesを設定することができます

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

13

radiusプロパティとRankBy.DISTANCEプロパティを一緒に使用することはできません。したがって、2つの選択肢があります:

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検索次に半径を使用して結果をキャップ。再び、距離を計算するためには、haversineの式が必要です。

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

でPlacesServiceクラス上の「検索」方法は利用できない現在のAPIドキュメント上のことに注意してください。

あなたが半径

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が離れすぎているかどうかを確認するために、この機能を使用することができ、距離半径に基づいてデータを制限したい場合はを設定傾けるRankBy.DISTANCEすることを選択した場合。

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

これは、指定したときに一定の半径 beacuse内部の場所を得るもへの唯一の道であることに注意してください「rankBy:google.maps.places.RankBy.PROMINENCE」と "半径= xx "placesServiceはも定義された領域の外に結果を返します

関連する問題