简体   繁体   English

Google Map-载入KML叠加层

[英]Google Map - Load KML Overlay

I am trying to display a map, zoom into a zipcode, and then place Congressional District overlay onto map. 我正在尝试显示地图,放大邮政编码,然后将Congressional District叠加层放置在地图上。 The overlay is here: https://www.google.com/fusiontables/DataSource?snapid=S506424n-DY 叠加层位于此处: https : //www.google.com/fusiontables/DataSource?snapid=S506424n-DY

I downloaded the KML link file, and saved it onto my local IIS server. 我下载了KML链接文件,并将其保存到本地IIS服务器上。 However, the overlay never is drawn. 但是,永远不会绘制叠加层。

I also added the following mime types to my IIS Server: 我还将以下MIME类型添加到了IIS服务器:

.kml application/vnd.google-earth.kml+xml 
.kmz application/vnd.google-earth.kmz

The kml file looks like this: kml文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<NetworkLink>
<name><![CDATA[2012 US Congressional Districts]]></name>
<Link>
<href>
https://www.google.com/fusiontables/exporttable?query=select+col5+from+1QlQxBF17RR-89NCYeBmw4kFzOT3mLENp60xXAJM&amp;o=kmllink&amp;g=col5</href>
</Link>

My HTML/javascript looks like: 我的HTML / javascript看起来像:

<script src="http://maps.googleapis.com/maps/api/js" type="text/javascript"></script>
<script language="javascript">
var map;
var marker;
function showAddress() {
    $("#divGoogleMap").css('display', '');
    var mapProp = {
        center: new google.maps.LatLng(51.508742, -0.120850),
        zoom: 11,
        mapTypeId: google.maps.MapTypeId.TERRAIN
    };
    map = new google.maps.Map(document.getElementById("divGoogleMap"), mapProp);

    myListener = google.maps.event.addListener(map, 'click', function (event) {
        placeMarker(event.latLng);
    });
    google.maps.event.addListener(map, 'drag', function (event) {
        placeMarker(event.latLng);
    });

    var zipCode = $("#txtZip").val();
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({ 'address': zipCode }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            //Got result, center the map and put it out there
            var pos = results[0].geometry.location;
            saveLocation(pos);
            map.setCenter(pos);
            marker = new google.maps.Marker({
                map: map,
                draggable: true,
                position: pos
            });
        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
    var kmlUrl = '/DesktopModules/HelloWorld/2012_US_Congressional_Districts.kml';
    var kmlOptions = {
        suppressInfoWindows: true,
        preserveViewport: false,
        map: map
    };
    var kmlLayer = new google.maps.KmlLayer(kmlUrl, kmlOptions);

}
function placeMarker(location) {
    if (marker) {
        marker.setPosition(location);
    } else {
        marker = new google.maps.Marker({
            position: location,
            map: map,
            draggable: true
        });
        google.maps.event.addListener(marker, "drag", function (mEvent) {
            populateInputs(mEvent.latLng);
        });
    }
    saveLocation(location);
}
function saveLocation(pos) {
    $("#hfLatitude").val(pos.lat());
    $("#hfLogitude").val(pos.lng());
}
</script>

<table cellpadding="2" cellspacing="2" border="0">
    <tr>
        <td>
            <asp:TextBox ID="txtZip" ClientIDMode="Static" runat="server"></asp:TextBox>
        </td>
        <td>
            <input type="button" id="btnSearch" value="Search" onclick="showAddress()" />
        </td>
    </tr>
</table>
<div id="divGoogleMap" style="width: 800px;height: 600px;display: none;"></div>
<asp:HiddenField ID="hfLatitude" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="hfLongitude" ClientIDMode="Static" runat="server" />

What am I doing wrong here? 我在这里做错了什么?

For use in google maps th kml must be located in an accessible server e not in local. 要在Google地图中使用,kml必须位于可访问的服务器中,而不位于本地。

This is form google doc. 这是Google文档的表格。

Overview 总览

The Google Maps API supports the KML and GeoRSS data formats for displaying geographic information. Google Maps API支持用于显示地理信息的KML和GeoRSS数据格式。 These data formats are displayed on a map using a KmlLayer object, whose constructor takes the URL of a publicly accessible KML or GeoRSS file. 这些数据格式使用KmlLayer对象显示在地图上,该对象的构造函数使用可公开访问的KML或GeoRSS文件的URL。

the constructor take the URL of a publicly accessible KML. 构造函数采用可公开访问的KML的URL。

Then for yoour need you must place your kml in a publicly accessible server 然后,根据需要,您必须将kml放置在可公开访问的服务器中

I had forgotten that i had already had a similar experience some years ago and the solution was precisely to place the files kml on a server accessible from the Internet. 我忘了几年前我也有过类似的经历,而解决方案就是将kml文件放在可从Internet访问的服务器上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM