2012-05-23 18 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 के दस्तावेज़ों का कहना है कि रैंकबी विकल्प का उपयोग करते हुए त्रिज्या आवश्यक नहीं है। क्या मैं कुछ भूल रहा हूँ?

+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 कुंजी जो की आवश्यकता नहीं है, लेकिन बाद में उपयोग के लिए जोड़ा है। जीपीएस है:

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

अन्त में, मैं सब कुछ है कि तुमने किया था सिवाय मैं एक नक्शा है कि मैं का उपयोग नहीं करने जा रहा था करने के लिए सीमा जोड़ा था। इसके लिए मैंने किया:

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

आप त्रिज्या + रैंक गुणों का एक साथ उपयोग नहीं कर सकते हैं।

यदि आपको निकटतम स्थानों की आवश्यकता है, तो कुछ प्रकार चुनें, और रैंक द्वारा सेट प्रोटीटी सेट करें।

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

आप त्रिज्या और 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

कृपया ध्यान दें कि वर्तमान एपीआई डॉक्टर पर PlacesService वर्ग पर "खोज" विधि उपलब्ध नहीं है।

  • nearbySearch
  • radarSearch (प्रति पृष्ठ 20 परिणाम, अधिकतम 3 पृष्ठों को पुनः प्राप्त) (200 परिणाम पुनः प्राप्त लेकिन कम विवरण के साथ)
  • textSearch:

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

    आप betwen चुनना होगा (पास के खोज के समान)

क्या आप त्रिज्या

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

कृपया को नोट करें कि यह भी एक ही रास्ता है एक निश्चित त्रिज्या के अंदर स्थानों प्राप्त क्योंकि जब आप "rankBy: google.maps.places.RankBy.PROMINENCE" निर्दिष्ट करते हैं और एक "त्रिज्या = xx "स्थान सेवा आपको परिभाषित क्षेत्र के बाहर भी परिणाम देती है।

संबंधित मुद्दे