求助!!!!怎么实现地图上任意两定位点间直线连接???

发布于 2021-11-14 06:35:26 字数 5216 浏览 791 评论 3

<!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 name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <title>搜索经纬度</title>
    <style type="text/css">
        html { height: 100%; }
        body { height: 100%; margin: 0px; padding: 0px; }
        .search { margin:50px auto; width:50%; }
        #map_canvas { width:50%; height: 50%; margin: 50px auto; }
    </style>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript">
        var geocoder;
        var map;
        var infowindow = new google.maps.InfoWindow();
        var marker;
		var x1;
		var y1;
		var x2;
		var y2;
        function initialize()
        {
            geocoder = new google.maps.Geocoder();
            var latlng = new google.maps.LatLng(22.95, 113.4);
            var myOptions = {
                zoom: 4,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP 
            }
            map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
			}
	
   function codeLatLngX()
        {
            var inputX = document.getElementById("latlngX").value;
            var latlngStr = inputX.split(",", 2);
            var latx1 = parseFloat(latlngStr[0]);
			x1=latx1;
            var lngy1 = parseFloat(latlngStr[1]);
			y1=lngy1;
            var latlng = new google.maps.LatLng(latx1, lngy1);
            geocoder.geocode({ 'latLng': latlng }, function(results, status)
            {
                if (status == google.maps.GeocoderStatus.OK)
                {
                    if (results[1])
                    {
                        map.setZoom(4);
                        marker = new google.maps.Marker({
                            position: latlng,
                            map: map
                        });
                        infowindow.setContent(results[1].formatted_address);
                        infowindow.open(map, marker);
                    } 
                    else
                    {
                        alert("没有记录");
                    }
                } else
                {
                    alert("Geocoder failed due to: " + status);
                }
            });	
        }
		function codeLatLngY()
        {
            var inputY = document.getElementById("latlngY").value;
            var latlngStr = inputY.split(",", 2);
            var latx2 = parseFloat(latlngStr[0]);
			x2=latx2;
            var lngy2 = parseFloat(latlngStr[1]);
			y2=lngy2;
            var latlng = new google.maps.LatLng(latx2, lngy2);
            geocoder.geocode({ 'latLng': latlng }, function(results, status)
            {
                if (status == google.maps.GeocoderStatus.OK)
                {
                    if (results[1])
                    {
                        map.setZoom(4);
                        marker = new google.maps.Marker({
                            position: latlng,
                            map: map
                        });
                        infowindow.setContent(results[1].formatted_address);
                        infowindow.open(map, marker);
                    } 
                    else
                    {
                        alert("没有记录");
                    }
                } else
                {
                    alert("Geocoder failed due to: " + status);
                }
            });	
        }
				function creatline()
			{
			var flightPlanCoordinates = [  
         new google.maps.LatLng(x1, y1),  
         new google.maps.LatLng(x2, y2)  
     ];  
     var flightPath = new google.maps.Polyline({  
       path: flightPlanCoordinates,  
       strokeColor: "#FF0000",  
       strokeOpacity: 1.0,  
       strokeWeight: 2  
     });  
    flightPath.setMap(map);  
   }
    </script>
</head>
<body onload="initialize()">
<div class="search">
        <p align="center">请输入经纬度1:
          <input id="latlngX" type="text" value=""/>
          <input type="button" value="搜索" onclick="codeLatLngX()"/>
	</p>
        <p align="center">请输入经纬度2:
		  <input id="latlngY" type="text" value=""/>
		  <input type="button" value="搜索" onclick="codeLatLngY()"/>
    </p>
        <p align="center"> 
          <input type="button" value="连线" onclick="creatline()"/>
                </p>
</div>
    <div id="map_canvas">
    </div>
</body>
</html>  
 </script>  
 </head>  
 <body onLoad="initialize()">  
   <div id="map_canvas"></div>  
 </body>  
 </html> 

急求各位大侠帮忙怎么将上图中红线去除,需要在上面代码中插入什么代码???

 

 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

私藏温柔 2021-11-14 13:14:28

引用来自#2楼“BoMen”的帖子

  1.    function Removeline()   
  2.             {   
  3.             var flightPlanCoordinates = [     
  4.          new google.maps.LatLng(x1, y1),     
  5.          new google.maps.LatLng(x1, y1)     
  6.      ];     
  7.      var flightPath = new google.maps.Polyline({     
  8.        path: flightPlanCoordinates,     
  9.        strokeColor: "#FF0000",     
  10.        strokeOpacity: 1.0,     
  11.        strokeWeight: 2     
  12.      });     
  13.     flightPath.setMap(map);     
  14.    }   
  15. <p align="center">    
  16.           <input type="button" value="移除连线" onclick="Removeline()"/>   
  17.               </p>   

其实只需要将createLine函数的参数改成两个一样的点,这样它就不会划线,而是划一个红点,或者你可以选择让它不画红点,把改动后的createLine命名为RemoveLine,或者你再调用GoogleAPI里真正移除线的函数也可以,不用像上面那样改动createLine。之后定义一个button叫“移除连线”,点移除连线就可以了!

北笙凉宸 2021-11-14 09:51:44

引用来自#2楼“BoMen”的帖子

  1.    function Removeline()   
  2.             {   
  3.             var flightPlanCoordinates = [     
  4.          new google.maps.LatLng(x1, y1),     
  5.          new google.maps.LatLng(x1, y1)     
  6.      ];     
  7.      var flightPath = new google.maps.Polyline({     
  8.        path: flightPlanCoordinates,     
  9.        strokeColor: "#FF0000",     
  10.        strokeOpacity: 1.0,     
  11.        strokeWeight: 2     
  12.      });     
  13.     flightPath.setMap(map);     
  14.    }   
  15. <p align="center">    
  16.           <input type="button" value="移除连线" onclick="Removeline()"/>   
  17.               </p>   

其实只需要将createLine函数的参数改成两个一样的点,这样它就不会划线,而是划一个红点,或者你可以选择让它不画红点,把改动后的createLine命名为RemoveLine,或者你再调用GoogleAPI里真正移除线的函数也可以,不用像上面那样改动createLine。之后定义一个button叫“移除连线”,点移除连线就可以了!

因为看清所以看轻 2021-11-14 09:40:24
  1.    function Removeline()   
  2.             {   
  3.             var flightPlanCoordinates = [     
  4.          new google.maps.LatLng(x1, y1),     
  5.          new google.maps.LatLng(x1, y1)     
  6.      ];     
  7.      var flightPath = new google.maps.Polyline({     
  8.        path: flightPlanCoordinates,     
  9.        strokeColor: "#FF0000",     
  10.        strokeOpacity: 1.0,     
  11.        strokeWeight: 2     
  12.      });     
  13.     flightPath.setMap(map);     
  14.    }   
  15. <p align="center">    
  16.           <input type="button" value="移除连线" onclick="Removeline()"/>   
  17.               </p>   

其实只需要将createLine函数的参数改成两个一样的点,这样它就不会划线,而是划一个红点,或者你可以选择让它不画红点,把改动后的createLine命名为RemoveLine,或者你再调用GoogleAPI里真正移除线的函数也可以,不用像上面那样改动createLine。之后定义一个button叫“移除连线”,点移除连线就可以了!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文