0
点赞
收藏
分享

微信扫一扫

google地图常用方法


google map api v3

1.计算两点间距离

加载geometry库
复制代码 代码如下:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=geometry"></script>

计算距离
复制代码 代码如下:

var meters=google.maps.geometry.spherical.computeDistanceBetween(latLngA, latLngB);
document.getElementById("distance").innerText = meters+"米";


说明:单位是米

 

2.地图的缩放事件监听

google.maps.event.addListener(map, 'zoom_changed',function() {
            if (map.getZoom() < MinZoomLevel) map.setZoom(MinZoomLevel);
 });

3.google地图禁止鼠标滚轮缩放

scrollwheel: false

初始化:

var map = new google.maps.Map(document.getElementById("map"), {
                zoom: 15, //放大比例
                center: new google.maps.LatLng(31.3, 120.6), //经纬度
                mapTypeId: google.maps.MapTypeId.ROADMAP, //地图类型ROADMAP、SATELLITE、

HYBRID
                panControl: false, //方向盘
                scaleControl: false, //比例尺
                mapTypeControl: false, //可以选的地图类型,下面是配置
                //mapTypeControlOptions: {style: 

google.maps.MapTypeControlStyle.DROPDOWN_MENU},
                streetViewControl:false,//街头小人
                zoomControl: true, //放大按钮,下面是配置
                zoomControlOptions: {
                    style: google.maps.ZoomControlStyle.SMALL//LARGE
                    //position: google.maps.ControlPosition.LEFT_CENTER
                }
            });

 

4.Google Map的比例尺与Zoom级别的关系:

简单罗列一下, 做个记录:
其中Zoom=5,6以及Zoom=15,16时, 比例尺是相同的, 分别是1:200km, 1:200m;
但是,在Map窗口里面的表现还是稍有不同的, 那就是刻度的长度上有变化。

Zoom    Scale
0    10000km
1    5000
2    2000
3    1000
4    500
5    200
6    200
7    100
8    50
9    20
10  10
11   5
12   2
13   1km
14   500m
15   200
16   200
17   100
18   50
19   20
20   10
21   5
22   2
23   1
24   1
25   1
26   1

5.反向地址解析:

//实例化地址解析器 
 var   geoCoder = new google.maps.Geocoder();function GetGeoLocation() { 
         if (navigator.geolocation) { 
             navigator.geolocation.getCurrentPosition(function(position) { 
                 var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 
                 console.log(position); 
                 geoCoder.geocode({ 
                     'latLng': pos 
                 }, 
                 function(results, state) { 
                     if (state = google.maps.GeocoderStatus.OK) { 
                         if (results[0]) { 
                             var point = results[0].geometry.location; 
formatted_address; 
                             if (marker) { 
                                 marker.setMap(null); 
                             } 
                             marker = new google.maps.Marker({ 
                                 map: map, 
                                 position: point 
                             }); 
                             var infowindow = new google.maps.InfoWindow({ 
                                 content: '<h3>我在这里</h3>' + myDirection 
                             }); 
                             google.maps.event.addListener(marker, 'click', 
                             function() { 
                                 infowindow.open(map, marker); 
                             }); 
                             map.setCenter(point); 
                             $("#txt_address").val(myDirection.split(' ')[0]); 
  
                         } 
                     } 
                 }) 
             }, 
             function() { 
                 handleNoGeolocation(true); 
             }, 
             { 
                 'enableHighAccuracy': true, 
                 'timeout': 10000, 
                 'maximumAge': 0 
             }); 
         } else { 
             // 浏览器不支持Geolocation 
             handleNoGeolocation(false); 
         } 
     }

举报

相关推荐

0 条评论