2016-02-07 73 views
0

我在我的地圖中有一個自定義的Google地圖,該地圖有多個多邊形,可以反映路線拾取的各個區域(例如星期一拾取區域,星期二拾取區域等),實質上我想要做的是找出一個特定地址落入哪個多邊形。這樣,我就可以通過單擊一次按鈕來確定新用戶將會屬於哪一天,而不必複製地址,轉到地圖,粘貼,搜索並查看它登錄的多邊形。找到哪個多邊形地址在

我對Google地圖完全陌生,對API或KML/KMZ文件不太熟悉。我發現我可以導出一個自我更新的KML或KMZ文件,所以也許我可以將它存儲在服務器上,然後讓程序使用它來找出多邊形地址在哪裏,但我不知道如何使用c#或我需要使用JavaScript,如果是的話,我會怎麼做?或者有沒有一種方法可以通過GET或其他方式直接查詢Google?

UPDATE 使用下面的代碼我已經能夠加載谷歌提供的網絡鏈接的kml文件。現在我想知道如何才能做到,點擊按鈕,找到從加載的kml中輸入的地址所在的多邊形圖層。我將繼續嘗試使用我發現的內容進行更新。

<!DOCTYPE html> 
<html> 
<head> 
<script 
src="http://maps.googleapis.com/maps/api/js"> 
</script> 

<script> 
function initMap() { 
    var map = new google.maps.Map(document.getElementById('googleMap'), { 
    zoom: 11, 
    center: {lat: 39.10342, lng: -76.87271} 
    }); 

    var kmlLayer = new google.maps.KmlLayer(); 
    var kmlUrl =  'http://xxxxxxxxxxx.com/crew/map/test.kml'; 
var kmlOptions = { 
    suppressInfoWindows: true, 
    preserveViewport: false, 
    map: map 
}; 
var kmlLayer = new google.maps.KmlLayer(kmlUrl, kmlOptions); 
} 

google.maps.event.addDomListener(window, 'load', initMap); 
</script> 
</head> 

<body> 
<div id="googleMap" style="width:600px;height:450px;"></div> 

</body> 
</html> 

回答

0

發現原來這就是我結束了混合後及配套各條從不同的站點信息。它加載存儲在服務器上的kml,獲取用戶輸入的地址,以kml的多邊形搜索它,如果發現它,然後計算出它是哪一天,然後適當設置拾取下拉菜單。

請注意,我隱藏了我的地圖,並且未設置任何標記或任何其他標記;我純粹用它來找出哪個多邊形地址屬於哪個。

<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false"></script> 
<script type="text/javascript" src="https://geoxml3.googlecode.com/svn/branches/polys/geoxml3.js"></script> 
<script src="../scripts/v3_epoly.js" type="text/javascript"> </script> 
<script type="text/javascript"> 
    var geoXml = null; 
    var map = null; 
    var geocoder = null; 
    var toggleState = 1; 
    var infowindow = null; 
    var marker = null; 

function initialize() { 
    geocoder = new window.google.maps.Geocoder(); 
    infowindow = new window.google.maps.InfoWindow({size: new window.google.maps.Size(150,50) }); 
    // create the map 
    var myOptions = { 
    zoom: 12, 
    center: new window.google.maps.LatLng(43.502745, -116.240845), 
    mapTypeControl: true, 
    mapTypeControlOptions: {style: window.google.maps.MapTypeControlStyle.DROPDOWN_MENU}, 
    navigationControl: true, 
    mapTypeId: window.google.maps.MapTypeId.ROADMAP 
    } 

    map = new window.google.maps.Map(document.getElementById("map_canvas"), 
           myOptions); 
    geoXml = new window.geoXML3.parser({map: map, singleInfoWindow: true, infoWindow: infowindow}); 

    geoXml.parse('map/routemap-v2.kml'); 
} 
function showAddress() { 
    var address = $("#<%=txtCustomerAddress.ClientID%>").val() + ', ' + $("#<%=txtCustomerCity.ClientID%>").val() + ', ' + $("#<%=txtCustomerState.ClientID%>").val() + ' ' + $("#<%=txtCustomerZipcode.ClientID%>").val(); 
    geocoder.geocode({ 'address': address}, function(results, status) { 
     if (status === window.google.maps.GeocoderStatus.OK) { 
      var point = results[0].geometry.location; 

      for (var i=0; i<geoXml.docs[0].gpolygons.length; i++) { 
       if (geoXml.docs[0].gpolygons[i].Contains(point)) { 
        var layerName = geoXml.docs[0].placemarks[i].name; 
        var day = ""; 
        if (layerName.indexOf("Monday") >= 0) { 
         day = "Monday"; 
        } 
        else if (layerName.indexOf("Tuesday") >= 0) { 
         day = "Tuesday"; 
        } 
        else if (layerName.indexOf("Wednesday") >= 0) { 
         day = "Wednesday"; 
        } 
        else if (layerName.indexOf("Thursday") >= 0) { 
         day = "Thursday"; 
        } 
        else if (layerName.indexOf("Friday") >= 0) { 
         day = "Friday"; 
        } 
        else if (layerName.indexOf("Saturday") >= 0) { 
         day = "Saturday"; 
        } 
        else if (layerName.indexOf("Sunday") >= 0) { 
         day = "Sunday"; 
        } else { 
         day = layerName; 
        } 
        $("#<%=PickUpDay.ClientID%>").val(day); 
        i = 999; // exit loop 
      } 
     } 
     } else { 
      alert("Address, as inputed, is not within the limits of any of the current routes. Please manually consult pick up day or edit address and try again."); 
     } 
    }); 
    } 
</script> 

<body onload="initialize()"> 
    <div id="map_canvas" style="width: 0; height: 0; margin-bottom: 10px; border:1px solid #999; display: none"></div> 
</body> 
0

我得到的是你想知道在哪個多邊形地址將落入。如果是這種情況,那麼containsLocation可以爲你帶來好處。將地址'latlng的值傳遞給每個多邊形,看看這些座標是否在一個內部。

有點像這個

var polygons = []; 

function checkIfPartOfPolygon(addressLatLng, polygon){ 
    return google.maps.geometry.poly.containsLocation(e.latLng, bermudaTriangle); 
} 

function foo(){ 
    var address = {}; 
    for (var i=0; i<polygons.length; i++){ 
     var addressPartOfPolygon = checkIfPartOfPolygon(address.latlng, polygons[i]); 
     if (addressPartOfPolygon){ 
      //address is part of current polygon, do something here 
      break; 
     } 
    } 
} 

爲containsLocation的官方樣片可在documentation's sample section

相關問題