2012-04-25 63 views
4

有沒有方法可以確定標記是否進入了KmlLayer覆蓋的區域?我的.kml大部分由一個<Polygon>組成,並帶有一組定義邊界的座標。使用Google Maps API V3,確定標記是否位於KML層邊界內

有文章我發現,描述了一些類似於我在尋找,創建使用覆蓋:http://www.paulmcilwaine.com/api/google-maps-detecting-markers-in-a-particular-boundary

該方法具有使用的getBounds()的優勢,但我在尋找一種方式用KmlLayer邊界來做到這一點。 KmlLayer似乎沒有像getBounds()這樣的方便函數,但是我確實擁有KML文件中所有可用的座標,所以我想有一種方法可以創建我自己的getBounds()函數。我只需要一種方法來確定標記(或拉特朗)是否在KML文件中的座標內。

感謝您的任何想法!

回答

8

KmlLayer上沒有這樣的功能。不過,如果你可以提取面邊界,並創建了一個多邊形,您可以使用Google地圖API的幾何形狀庫,以確定該點位於多邊形內部: https://developers.google.com/maps/documentation/javascript/reference#poly

+0

那偉大工程,我可以從kml中取出座標,從它們中創建一個多邊形,然後使用google.maps.geometry.poly.containsLocation()來確定標記的位置是在邊界之內還是之外。謝謝! – 2012-04-25 22:06:51

6

我有一個請求以提供對如何更詳細我實現了這一點。你可以在這裏看到一個例子:http://www.usinternet.com/fiber-info/map.php

我在地圖上有幾個標記,我想測試它們是否在邊界內部或外部。我現在就寫了這樣的:

 var userLocation = new google.maps.LatLng(44.928633,-93.298919); //Lyndale Park 

     var wendysLocation = new google.maps.LatLng(44.948056,-93.282222); //Wendy's on Lake St 

     var userMarker = new google.maps.Marker({ 
      position: userLocation, 
      map: map, 
      title:"Your Location", 
      draggable: true 
     }); 

     var wendysMarker = new google.maps.Marker({ 
      position: wendysLocation, 
      map: map, 
      title:"Wendy's on Lake St", 
      draggable: true 
     }); 

要創建你需要的東西讀KML文件並生成一組座標,然後可以添加到多邊形的路徑,多邊形:財產。不幸的是,似乎您只能使用Google Maps API從KML轉到Polygon,但如果我錯了,有人會糾正我!

var sector3PillburyPleasant = new google.maps.KmlLayer('http://usinternet.com/fiber-info/kml/Sector3Pillbury-Pleasant.kml', kmlOptions); 
      sector3PillburyPleasant.setMap(map); 

      var coordsSector3PillburyPleasant = [ 
       new google.maps.LatLng(44.91615269014508, -93.28382953316716), 
       new google.maps.LatLng(44.91619137463104, -93.28120338511586), 
       new google.maps.LatLng(44.93046751403393, -93.28114057929436), 
       new google.maps.LatLng(44.93046991974436, -93.28055243604703), 
       new google.maps.LatLng(44.94815274527994, -93.28053962401711), 
       new google.maps.LatLng(44.94815856171297, -93.28364017122617), 
      ]; 

      var polySector3PillburyPleasant = new google.maps.Polygon({ 
       paths: coordsSector3PillburyPleasant, 
       strokeColor: "#FFFFFF", 
       strokeOpacity: 0.0, 
       fillColor: "#FFFFFF", 
       fillOpacity: 0.0 
      }); 

我解析多邊形的小路從KML,構成道路的座標被嵌套這樣的:(如果你關閉路徑是不需要的最後一個座標對。)

<kml> 
<Document> 
    <Placemark> 
     <Polygon> 
      <tessellate>1</tessellate> 
      <outerBoundaryIs> 
       <LinearRing> 
        <coordinates> 
         -93.28382953316716,44.91615269014508,0 -93.28120338511586,44.91619137463104,0 -93.28114057929436,44.93046751403393,0 -93.28055243604703,44.93046991974436,0 -93.28053962401711,44.94815274527994,0 -93.28364017122617,44.94815856171297,0 -93.28382953316716,44.91615269014508,0 
        </coordinates> 
       </LinearRing> 
      </outerBoundaryIs> 
     </Polygon> 
    </Placemark> 
</Document> 
</kml> 

然後我測試我有標記的是否是內部或多邊形之外:

 var markerIn = google.maps.geometry.poly.containsLocation(wendysMarker.getPosition(), polySector3PillburyPleasant); 

     var markerOut = google.maps.geometry.poly.containsLocation(userMarker.getPosition(), polySector3PillburyPleasant); 

     console.log(markerIn); 

     console.log(markerOut); 

那麼你已經有了自己的真或假的測試,如果你是內或KML層外,你gh有一段彎路將KML圖層轉化爲多邊形。

4

這裏是如何KML轉換爲谷歌地圖V3有很大答案:

How to Check if a Point is in KML Polygon (GIS Shapefile)

使用第三方解析器像geoxml3或geoxml-V3,以使您的KML作爲本地谷歌地圖API V3的多邊形,然後用你知道的方式。例如,使用geoxml3

給出的示例將KML爲多邊形,需要一個地址輸入,geolocates它來獲得緯度/經度,然後確定哪些多邊形點是:

http://www.geocodezip.com/geoxml3_test/v3_collection-map2e.html