简体   繁体   中英

Google Map - Load KML Overlay

I am trying to display a map, zoom into a zipcode, and then place Congressional District overlay onto map. The overlay is here: https://www.google.com/fusiontables/DataSource?snapid=S506424n-DY

I downloaded the KML link file, and saved it onto my local IIS server. However, the overlay never is drawn.

I also added the following mime types to my IIS Server:

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

The kml file looks like this:

<?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:

<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.

This is form google doc.

Overview

The Google Maps API supports the KML and GeoRSS data formats for displaying geographic information. 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.

the constructor take the URL of a publicly accessible KML.

Then for yoour need you must place your kml in a publicly accessible server

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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