我正在使用Google API v3中的地理编码器来显示国家/地区的地图.我得到了国家推荐的视口,但是当我想让地图适合这个视口时,它不起作用(请参阅下面代码中调用fitBounds函数之前和之后的边界).
我究竟做错了什么?
如何将地图的视口设置为results[0].geometry.viewport?
var geocoder = new google.maps.Geocoder();
geocoder.geocode(
{'address': '{{countrycode}}'},
function(results, status) {
var bounds = new google.maps.LatLngBounds();
bounds = results[0].geometry.viewport;
console.log(bounds); // ((35.173, -12.524), (45.244, 5.098))
console.log(map.getBounds()); // ((34.628, -14.683), (58.283, 27.503))
map.fitBounds(bounds);
console.log(map.getBounds()); // ((25.740, -24.806), (52.442, 17.380))
}
);
Run Code Online (Sandbox Code Playgroud) 我正在使用多个地图标记.目前我map.fitBounds(bounds);在我的JavaScript中使用来调整地图的大小.bounds包含多个LatLng对象.
我究竟做错了什么?因为它缩小得太远了:-(
JavaScript源码
var geocoder, map;
$(document).ready(function(){
var coll_gmap = $(".gmap");
if ( coll_gmap.length != 0 )
{
//initiate map
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 13,
center: latlng,
mapTypeControl: true,
navigationControl: true,
scaleControl: true,
navigationControlOptions: {style: google.maps.NavigationControlStyle.ZOOM_PAN},
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var bounds = new google.maps.LatLngBounds();
//loop all addressen + insert into map
map = new google.maps.Map(coll_gmap[0], myOptions);
coll_gmap.each(function(index)
{
if (geocoder) {
geocoder.geocode( { 'address': $(this).attr("address")}, …Run Code Online (Sandbox Code Playgroud)