谷歌地图radarSearch返回少于200 [英] google maps radarSearch returning less than 200

查看:140
本文介绍了谷歌地图radarSearch返回少于200的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题陈述



雷达搜索仅在初始加载时返回地图中心附近的位置,仅约140(不是200)返回。我将请求对象的边界成员设置为map.getBounds ...

当我移动地图的中心时,会有更多位置被添加...


问题



为什么我得到更少比广告中的200个地点要多,我如何获得地图区域的详尽搜索结果?
$ b

代码

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%;
保证金: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;

函数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);


函数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& region = au'
script.type ='text / javascript';
script.src = myScript;
document.body.appendChild(script);
}

window.onload = loadScript;

函数performSearch(){
var request = {
bounds:map.getBounds(),
关键字:'caravan parks'
};

if(!infoWindow){infoWindow = new google.maps.InfoWindow()};
if(!service){service = new google.maps.places.PlacesService(map)};
service.radarSearch(request,searchCallback);


函数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('搜索失败')
}
}

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

注意:


  1. 如果我包含一个有效的API密钥,它没有任何区别。

  2. 如果我使用半径参数,则返回148个位置,但边界设置为映射.getBounds我只得到145.我在这个阶段的猜测是,当使用边界参数时,搜索区域是一个包含在50公里半径圆中的矩形。这是不明确的基于文档。


解决方案

我自己最终得出的结论如下......


  1. 当使用位置请求对象的半径参数,搜索区域实际上是搜索半径的包含的平方。最大搜索区域是半径为50公里的圆的包含平方(这是记录的搜索半径的最大值。)

  2. 使用边界时
  3. 请求对象的参数,则搜索的最大区域为100公里正方形,如果边界设置得大于此范围,搜索仍限制在100公里的范围内,但文档不明确,但这是合乎逻辑的。


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... As I move the center of the map, more locations get added...

Question

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&region=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:

  1. It doesn't make any difference if I include a valid API key.
  2. 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.

解决方案

I managed to bottom this out myself eventually, my findings are as follows...

  1. 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.
  2. 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.

这篇关于谷歌地图radarSearch返回少于200的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆