给定坐标点,判断是否在某区域范围内 地理围栏算法

地图平台:leaflet

语言:js

 

index.html

 

[html] view plain copy

 print?

  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4.     <head>  
  5.         <title>Leaflet 快速开始指南示例</title>  
  6.         <meta charset="utf-8" />  
  7.   
  8.         <link rel="stylesheet" href="css/leaflet.css" />  
  9.     </head>  
  10.   
  11.     <body>  
  12.         <div id="map" style="width: 1024px; height: 768px"></div>  
  13.   
  14.         <script src="js/leaflet.js"></script>  
  15.         <script type="text/javascript" src="js/jquery-2.0.3.min.js"></script>  
  16.         <script type="text/javascript" src="js/isPointInPolygon.js"></script>  
  17.         <script>  
  18.             var map = L.map('map').setView([24, 110], 5);  
  19.             L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6IjZjNmRjNzk3ZmE2MTcwOTEwMGY0MzU3YjUzOWFmNWZhIn0.Y8bhBaUMqFiPrDRW9hieoQ', {  
  20.                 maxZoom: 18,  
  21.                 attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +  
  22.                     '<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +  
  23.                     'Imagery © <a href="http://mapbox.com">Mapbox</a>',  
  24.                 id: 'mapbox.streets'  
  25.             }).addTo(map);  
  26.             L.marker([24, 100]).addTo(map)  
  27.                 .bindPopup("<b>Hello world!</b><br />我是一个冒泡窗").openPopup();  
  28.             L.circle([24, 100], 500, {  
  29.                 color: 'red',  
  30.                 fillColor: '#f03',  
  31.                 fillOpacity: 0.5  
  32.             }).addTo(map).bindPopup("我是一个圆");  
  33.             L.polygon([{  
  34.                 lat: 26,  
  35.                 lng: 101  
  36.             }, {  
  37.                 lat: 24,  
  38.                 lng: 120  
  39.             }, {  
  40.                 lat: 35,  
  41.                 lng: 110              
  42.             }]).addTo(map);  
  43.             var popup = L.popup();  
  44.   
  45.             function points() {  
  46.                 var points = [{  
  47.                 lat: 26,  
  48.                 lng: 101  
  49.             }, {  
  50.                 lat: 24,  
  51.                 lng: 120  
  52.             }, {  
  53.                 lat: 35,  
  54.                 lng: 110  
  55.             }]  
  56.                 return points;  
  57.             }  
  58.   
  59.             function onMapClick(e) {  
  60.                 popup  
  61.                     .setLatLng(e.latlng)  
  62.                     .setContent("你刚点击的坐标是 " + e.latlng.toString())  
  63.                     .openOn(map);  
  64.                 /*var hh = IsPtInPoly(119.44336,25.20494, points());*/  
  65.                 var hh = IsPtInPoly(e.latlng.lng,e.latlng.lat, points());  
  66.                 alert(hh);  
  67.             }  
  68.             map.on('click', onMapClick);  
  69.         </script>  
  70.     </body>  
  71.   
  72. </html>  


 

 

isPointInPolygon.js

 

[javascript] view plain copy

 print?

  1. function IsPtInPoly(ALon, ALat, APoints) {  
  2.     var iSum = 0,  
  3.         iCount;  
  4.     var dLon1, dLon2, dLat1, dLat2, dLon;  
  5.     if (APoints.length < 3) return false;  
  6.     iCount = APoints.length;  
  7.     for (var i = 0; i < iCount; i++) {  
  8.         if (i == iCount - 1) {  
  9.             dLon1 = APoints[i].lng;  
  10.             dLat1 = APoints[i].lat;  
  11.             dLon2 = APoints[0].lng;  
  12.             dLat2 = APoints[0].lat;  
  13.         } else {  
  14.             dLon1 = APoints[i].lng;  
  15.             dLat1 = APoints[i].lat;  
  16.             dLon2 = APoints[i + 1].lng;  
  17.             dLat2 = APoints[i + 1].lat;  
  18.         }  
  19.         //以下语句判断A点是否在边的两端点的水平平行线之间,在则可能有交点,开始判断交点是否在左射线上  
  20.         if (((ALat >= dLat1) && (ALat < dLat2)) || ((ALat >= dLat2) && (ALat < dLat1))) {  
  21.             if (Math.abs(dLat1 - dLat2) > 0) {  
  22.                 //得到 A点向左射线与边的交点的x坐标:  
  23.                 dLon = dLon1 - ((dLon1 - dLon2) * (dLat1 - ALat)) / (dLat1 - dLat2);  
  24.                 if (dLon < ALon)  
  25.                     iSum++;  
  26.             }  
  27.         }  
  28.     }  
  29.     if (iSum % 2 != 0)  
  30.         return true;  
  31.     return false;  
  32. }  


 

 

 

 

转载于:https://my.oschina.net/hejunbinlan/blog/1486711

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值