在google地图上划线

通过 google.maps.Polyline 实现在Google地图上画线

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">    
    <title>google地图</title>    
    <script src="http://maps.google.com/maps/api/js?v=3.1&sensor=true"type="text/javascript"></script>  
    <script type="text/javascript">    
        function initialize() {    
            var myOptions = {    
                zoom: 16,    
                center: new google.maps.LatLng(29.82579, 121.56413),    
                mapTypeId: google.maps.MapTypeId.ROADMAP    
            };    
            var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);    
            // 这里填上轨迹的经纬坐标,或者ajax从后台读取    
        // 坐标做好是在路口的,不然画线不在路上。    
            var trackPoints = [    
				new google.maps.LatLng(29.83092, 121.55923),
                new google.maps.LatLng(29.83020, 121.55923),    
                new google.maps.LatLng(29.83015, 121.56059),    
                new google.maps.LatLng(29.82929, 121.56423),    
                new google.maps.LatLng(29.82276, 121.56407),    
                new google.maps.LatLng(29.82273, 121.56652),    
				new google.maps.LatLng(29.82234, 121.56652), 
				new google.maps.LatLng(29.82209, 121.56695),
            ];    
                
            var trackPath = new google.maps.Polyline({    
                path: trackPoints,    
                strokeColor: "#FF0000", // 线条颜色    
                strokeOpacity: 1.0, // 线条透明度    
                strokeWeight: 2 // 线条粗细    
            });    
    
            trackPath.setMap(map);    //画线
        }    
</script>    
</head>    
<body οnlοad="initialize()">    
    <form id="form1" runat="server">    
    <div id="map_canvas" style="width:1000px; height:700px;"></div>    
    </form>    
</body>    
</html>

谷歌地图被中国防火墙封杀,所以不用直接引用http://maps.googleapis.com/maps/api/js?sensor=false&language=en这域名下的谷歌地图api,而是改为http://maps.google.cn/maps/api/js?sensor=false这个地址,google.cn在国内的域名没有被封杀,可以使用。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>网站引用谷歌地图打不开解决办法:使用google.cn</title>  
</head>  
<body>  
<div id="map_canvas" class="map" style="height: 350px;width: 500px;"></div>  

<script type="text/javascript">  
  function renderGoogleMap() { //回调的方法  
            var myOptions = {        
                zoom: 15,        
                center: new google.maps.LatLng(29.82579, 121.56413),        
                mapTypeId: google.maps.MapTypeId.ROADMAP        
            };        
            var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);        
            // 这里填上轨迹的经纬坐标,或者ajax从后台读取        
        // 坐标做好是在路口的,不然画线不在路上。        
            var trackPoints = [        
                new google.maps.LatLng(29.83092, 121.55923),    
                new google.maps.LatLng(29.83020, 121.55923),        
                new google.maps.LatLng(29.83015, 121.56059),        
                new google.maps.LatLng(29.82929, 121.56423),        
                new google.maps.LatLng(29.82276, 121.56407),        
                new google.maps.LatLng(29.82273, 121.56652),        
                new google.maps.LatLng(29.82234, 121.56652),     
                new google.maps.LatLng(29.82209, 121.56695),    
            ];        
                    
            var trackPath = new google.maps.Polyline({        
                path: trackPoints,        
                strokeColor: "#FF0000", // 线条颜色        
                strokeOpacity: 1.0, // 线条透明度        
                strokeWeight: 2 // 线条粗细        
            });        
        
            trackPath.setMap(map);    //画线    
  }   
</script>  
<script type="text/javascript" src="http://maps.google.cn/maps/api/js?sensor=false&callback=renderGoogleMap"></script>  
</body>  
</html>  


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值