Google地图API V3方法fitBounds() [英] Google maps API V3 method fitBounds()

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

问题描述

 函数初始化(){
var myOptions = {
center:new google.maps.LatLng(45.4555729,9.169236),
zoom:13,
mapTypeId:google.maps.MapTypeId.ROADMAP,
$ b panControl:true ,
mapTypeControl:false,
panControlOptions:{
位置:google.maps.ControlPosition.RIGHT_CENTER
},
zoomControl:true,
zoomControlOptions:{
style:google.maps.ZoomControlStyle.LARGE,
position:google.maps.ControlPosition.RIGHT_CENTER
},
scaleControl:false,
streetViewControl:false,
streetViewControlOptions:{
position:google.maps.ControlPosition.RIGHT_CENTER
}
};
var map = new google.maps.Map(document.getElementById(mapCanvas),
myOptions);



var Item_1 = new google.maps.LatLng(45.5983128,8.9172776);

var myPlace = new google.maps.LatLng(45.4555729,9.169236);

var marker = new google.maps.Marker({
position:Item_1,
map:map});

var marker = new google.maps.Marker({
position:myPlace,
map:map});

var bounds = new google.maps.LatLngBounds(myPlace,Item_1);
map.fitBounds(bounds);

}

即使两点距离25公里,这个结果:





虽然我想呈现更高级别的缩放。



像这样



我使用方法 fitBounds()

  var bounds = new google.maps.LatLngBounds(myPlace ,Item_1); 
map.fitBounds(bounds);

感谢您的支持

解决方案

这是因为 LatLngBounds( ) 不需要两个任意点作为参数,但SW和NE点使用。对空边界对象执行extend()方法

  var bounds = new google.maps.LatLngBounds( ); 
bounds.extend(myPlace);
bounds.extend(Item_1);
map.fitBounds(bounds);

演示 http://jsfiddle.net/gaby/22qte/

I have this code that renders a map.

function initialize() {
    var myOptions = {
      center: new google.maps.LatLng(45.4555729, 9.169236),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP,

panControl: true,
mapTypeControl: false,
panControlOptions: {
            position: google.maps.ControlPosition.RIGHT_CENTER
        },
zoomControl: true,
zoomControlOptions: {
    style: google.maps.ZoomControlStyle.LARGE,
    position: google.maps.ControlPosition.RIGHT_CENTER
},
scaleControl: false,
streetViewControl: false,
streetViewControlOptions: {
    position: google.maps.ControlPosition.RIGHT_CENTER
            }
        };
    var map = new google.maps.Map(document.getElementById("mapCanvas"),
        myOptions);



var Item_1 = new google.maps.LatLng(45.5983128 ,8.9172776);

var myPlace = new google.maps.LatLng(45.4555729, 9.169236);

var marker = new google.maps.Marker({
    position: Item_1, 
    map: map});

var marker = new google.maps.Marker({
    position: myPlace, 
    map: map});

var bounds = new google.maps.LatLngBounds(myPlace, Item_1);
map.fitBounds(bounds);

    }

Even if the two points are separated from 25 km I get this result:

While I would like to render a higher level zoom.

Like this

I use the method fitBounds()

    var bounds = new google.maps.LatLngBounds(myPlace, Item_1);
    map.fitBounds(bounds);

Thanks for your support

解决方案

This happens because LatLngBounds() does not take two arbitrary points as parameters, but SW and NE points

use the .extend() method on an empty bounds object

var bounds = new google.maps.LatLngBounds();
bounds.extend(myPlace);
bounds.extend(Item_1);
map.fitBounds(bounds);

Demo at http://jsfiddle.net/gaby/22qte/

这篇关于Google地图API V3方法fitBounds()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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