2012-05-23 13 views
8

Czy ktoś wie, jak korzystać z opcji rankingu według odległości, o której tu wspomniano? https://developers.google.com/maps/documentation/javascript/places#place_search_requestsgoogle maps api v3 jak pozycjonować najbliższy dystans

Umieszczenie tego w opcjach żądań nie działa. Oto moja część kodu w stosunku do tego:

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

Kod zlokalizuje i wyniki Lista gdybym to promień, ale wyniki nie są wymienione w kolejności rosnącej według odległości. Dokumenty Google mówią, że promień nie jest konieczny, jeśli używasz opcji rankBy. Czy czegoś brakuje?

+0

Jestem w tej samej łodzi ... szukając rozwiązania. – GuiDoody

Odpowiedz

7

Wystąpił ten sam problem. Według tego źródła: http://www.geocodezip.com/v3_GoogleEx_place-search.html udało mi się sformułować zapytanie, takie jak:

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

klucz Var jest klucz API, które nie jest wymagane, ale dodaje do późniejszego wykorzystania. GPS:

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

Wreszcie zrobiłem wszystko, co zrobiłeś z wyjątkiem dodałem granic na mapie, że nie zamierza korzystać. W tym celu:

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

Nie można używać właściwości radius + rankBy razem.

Jeśli potrzebujesz najbliższych lokalizacji, wybierz niektóre typy i ustaw właściwość rankBy.

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

gdzie można ustawić placeTypes tak

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

Nie można jednocześnie używać właściwości promienia i RankBy.DISTANCE. Dlatego masz dwie opcje:

1) Wyszukaj według promienia, a następnie posortuj wyniki według odległości we własnym kodzie.

przykład:

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

Opcja 2: wyszukiwanie przez RankBy.Distance czym nasadka wynik za pomocą promienia. Ponownie potrzebna byłaby formuła haversine do obliczania odległości.

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

To była zdecydowanie poprawna odpowiedź dla mnie, doświadczyłem tego samego problemu co @JFlo – David

1

Należy pamiętać, że na obecnym Api Doc „Szukaj” metoda na klasie PlacesService nie jest dostępny.

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

Musisz wybrać betwen:

  • nearbySearch (odzyskać 20 wynik każdej strony, max 3 strony)
  • radarSearch (odzyskać 200 wynik, ale z mniejszą ilością szczegółów)
  • textSearch (podobne do pobliskiegoSearch)

Jeśli zdecydujesz się RankBy.DISTANCE you cant ustawić promień

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

Jeśli chcesz ograniczyć dane w oparciu o promieniu odległości można wykorzystać tę funkcję, aby sprawdzić, czy resut jest zbyt daleko.

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

Należy pamiętać, że jest to również jedyny sposób, aby uzyskać miejsca wewnątrz określonego promienia beacuse po określeniu „rankBy: google.maps.places.RankBy.PROMINENCE” i "promień = xx "the placeService daje także wyniki poza zdefiniowanym obszarem.

Powiązane problemy