绑定谷歌地图 [英] bounds google maps

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

问题描述

我的Google地图API地图缩放显示了Google地方搜索后添加的所有标记。在移动地图上,这可能需要一些时间来加载,因为这个应用程序我喜欢只在视口中显示结果。我读到的大多数研究表明,仅在视口内返回结果是不可能的。添加标记之后,我能否保持地图边界不变?如果只显示添加到当前界限的标记,除非只有一个标记返回,否则将会很好,那么它应该放大该标记。因此,搜索比萨饼这样的东西将在当前视口中显示许多比萨饼位置而不进行平移或缩放。搜索一个特定的地址将会放大到该标记,即使它在视口之外。

My Google maps API map zooms put to show all the markers added after a google places search. On a mobile map this can take a bit to load and as this app for cyclists I prefer to only show the results within the viewport. Most research I read indicates it is not possible to only return results within the view port. Is it possible for me to just keep the map bounds from changing after the markers are added? It would be great if it would just show me the markers added to the current bounds unless there is only one marker returned, then it should zoom to that marker. So searching for something general like pizza will show many pizza places in the current viewport without panning or zooming. Searching for a specific address will zoom to that marker even if it is outside the viewport.

 var input = document.getElementById('target');
 var searchBox = new google.maps.places.SearchBox(input);
 var bounds = map.getBounds();
 searchBox.setBounds(bounds);
 var markers = [];
 service = new google.maps.places.PlacesService(map);

 google.maps.event.addListener(searchBox, 'places_changed', function() {
     var places = searchBox.getPlaces();
     // alert("getPlaces returns "+places.length+" places");

     for (var i = 0; i < gmarkers.length; i++) {
       gmarkers[i].setMap(null);
     }

     gmarkers = [];
     var bounds = new google.maps.LatLngBounds();

     for (var i = 0, place; place = places[i]; i++) {
       var place = places[i];
       createMarker(place);
       bounds.extend(place.geometry.location);
     }
     map.fitBounds(bounds);
     // if (markers.length == 1) map.setZoom(17);
   });

   google.maps.event.addListener(map, 'bounds_changed', function() {
     var bounds = map.getBounds();
     searchBox.setBounds(bounds);
   });

   ib = new InfoBox({});


推荐答案

您的问题的答案:


  • 在添加标记之后,我可以保持地图范围不会发生变化。

是肯定的。正如你所说,包括移除map.fitBounds。

is yes. And as you say, involves removing the map.fitBounds.

要让地图居中放大一个地址,请使用地理编码器(分别实施) )或检查结果的数量(如果结果为1),将结果放在结果中心,如果结果中有建议的视口,则使用该视口居中放大地图[使用map.fitBounds(place.geometry.viewport)我认为]。

To get the map to center and zoom on a single address, either use the geocoder for that (implement it separately) or check the number of results, if it is one, center the map on the result and if there is a suggested viewport in the result, use that to center and zoom the map [with map.fitBounds(place.geometry.viewport) I think].

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

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