谷歌地图 —如何巧妙处理地理编码地址的视口
我正在将 Google 地图实施到一个新项目中,一个页面应返回一张大的 (100% * 100%) 加拿大地图。我还使用地理编码来确定要传递给地图的纬度。
这是代码:
var myLatlng = new google.maps.LatLng(37.775, -122.4183333);
var myOptions ={
zoom: 5,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.TERRAIN
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var address = "Canada";
var geocoder = new google.maps.Geocoder(address);
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
//map.setCenter(results[0].geometry.location);
//The above was too far north
map.fitBounds(results[0].geometry.viewport);
map.setZoom(5);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
它工作得很好,只是“有点太靠左了”。从本质上讲,它切断了大部分沿海省份的联系。示例如下:
http://50.28.69.176/~sequoia/locations.php
有人知道如何巧妙地处理这个问题,使国家/地区更好地位于视口的中心吗?
感谢您的任何帮助。
I'm implementing Google Maps into a new project and one page should return a large (100% * 100%) map of Canada. I'm also using Geocoding to determine the latlng to be passed to the map.
Here's the code:
var myLatlng = new google.maps.LatLng(37.775, -122.4183333);
var myOptions ={
zoom: 5,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.TERRAIN
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var address = "Canada";
var geocoder = new google.maps.Geocoder(address);
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
//map.setCenter(results[0].geometry.location);
//The above was too far north
map.fitBounds(results[0].geometry.viewport);
map.setZoom(5);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
It's working nicely except it's "a little too far to the left". Essentially, it's cut off most of the Maritime provinces. An example can be seen here:
http://50.28.69.176/~sequoia/locations.php
Anyone know how to finesse this so that the country is better centered in the viewport?
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议不要每次加载页面时都进行地理编码,而是获取所需的正确
LatLng
并将其存储。如果您仍想坚持使用此方法,可以在
LatLngBounds
上使用getCenter()
方法来获取其中心。这样你就可以调用
Instead of geocoding every time the page is loaded, I'd suggest just getting the correct
LatLng
that you want, and storing it.If you still want to stick with this approach, you can use the
getCenter()
method on aLatLngBounds
to get its center.That way, you can call