google map 根据坐标点自动缩放

google map v3的命令和v2有些差别。下面把我的代码贴出来。

这个功能主要是如果一个地图上有多个坐标,可以根据坐标点自动计算zoom值,防止有点坐标点跑到地图外面,导致显示不全。

<script type="text/javascript"  src="http://ditu.google.cn/maps/api/js?v=3.exp&key=xxxxxxxx&language=zh-cn&sensor=false"></script>


planService.initMap = function (id, flightPlanCoordinates, areaList) { // by sjw


    var lat = 34.0204989;    //默认坐标点
    var lng = -118.4117325;   //默认坐标点



    var map = new google.maps.Map(document.getElementById('map' + id), {
        zoom: 6,                   //默认zoom=6
        center: {lat: lat, lng: lng},
        mapTypeId: google.maps.MapTypeId.TERRAIN
    });


    var bounds = new google.maps.LatLngBounds();      //声明一个bounds
    $.each(areaList, function (i, area) {    //遍历坐标列表

        var image = '../../resources/images/icon-' + area.imgName + '.png';   //声明图标
        bounds.extend(new google.maps.LatLng(area.lat, area.lng));
        var beachMarker = new google.maps.Marker({
            position: {lat: area.lat, lng: area.lng},
            map: map,
            icon: image
        });
    })

    if (areaList.length > 1) {    //如果坐标点多余1个,则使用  google map 的fitBounds,自动计算zoom值

        map.fitBounds(bounds);
    }



    var lineSymbol = {              //把各点用虚线连起来
        path: 'M 0,-1 0,1',
        strokeOpacity: 1,
        strokeWeight: 2,  //线粗细
        scale: 2    //虚线段长度
    };


    var line = new google.maps.Polyline({
        path: flightPlanCoordinates,
        strokeOpacity: 0,
        strokeColor: "#ed5565",   //颜色
        strokeWeight: 1,
        icons: [{
            icon: lineSymbol,
            offset: '0',
            repeat: '15px'    //间距
        }],
        map: map
    });


};

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值