问题
Problem statement
radarSearch is only returning locations close to the center of the map on initial load and only about 140 (not 200) are returned. I am setting the bounds member of the request object to map.getBounds...


Why do I get less than the advertised 200 locations and how do I get an exhaustive search result for the map area?
Code
jsFiddle
HTML
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<style type="text/css">
html, body, #map-canvas {
height: 100%;
margin: 0;
padding: 0;
}
</style>
<script src="JavaScriptMin.js"></script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>
Javascript
var map;
var infoWindow;
var service;
function initialize() {
var mapOptions = {
zoom: 5,
center: new google.maps.LatLng(-27.984113, 153.411076)
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
google.maps.event.addListener(map, 'bounds_changed', performSearch);
}
function loadScript() {
var script = document.createElement('script'),
myScript = 'https://maps.googleapis.com/maps/api/js?signed_in=true&v=2.exp' +
'&libraries=weather,places&sensor;=true_or_false&signed_in=true&callback=initialize®ion=au'
script.type = 'text/javascript';
script.src = myScript;
document.body.appendChild(script);
}
window.onload = loadScript;
function performSearch() {
var request = {
bounds: map.getBounds(),
keyword: 'caravan parks'
};
if (!infoWindow) { infoWindow = new google.maps.InfoWindow() };
if (!service) { service = new google.maps.places.PlacesService(map) };
service.radarSearch(request, searchCallback);
}
function searchCallback(results, status, pagination) {
if (results) { console.log('searchCallback, results count is ' + results.length) };
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
var place = results[i];
createMarker(results[i]);
}
} else {
console.log('search failed')
}
}
function createMarker(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
icon: {
path: google.maps.SymbolPath.CIRCLE,
scale: 2,
opacity: 1,
fillColor: 'red',
fillOpacity: 1,
strokeColor: 'red',
strokeWeight: 1
},
position: place.geometry.location
});
}
Notes:
- It doesn't make any difference if I include a valid API key.
- If I use a radius parameter it returns 148 locations but with bounds set to map.getBounds I only get 145. My guess at this stage is that the search area, when using the bounds parameter, is a rectangle contained in a 50 km radius circle. This is not clear to me based on the documentation.
回答1:
I managed to bottom this out myself eventually, my findings are as follows...
- When using the location and radius parameters of the request object, the search area is actually the containing square of the search radius. The maximum search area is the containing square of a circle with 50 km radius (which is the documented maximum value for the search radius.
- When using the bounds parameter of the request object, the maximum area searched is a 100 km square. If the bounds are set bigger than that, the search is still limited to the 100 km square. The documentation is not clear on this but it's kind of logical.
来源:https://stackoverflow.com/questions/28513810/google-maps-radarsearch-returning-less-than-200