Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
145 views
in Technique[技术] by (71.8m points)

javascript - Google Maps KML Layer won't read kml

I'm trying to read a simple KML with Google Maps API 3, I created KML file in Google Maps Editor, codes are google sample, i can't find where is problem.

gm-sample.html

BTM.kml

function initMap() {
        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 13,
          center: {lat: 40.40926, lng: 49.86709}
        });

        var ctaLayer = new google.maps.KmlLayer({
          url: 'itdc.byethost7.com/BTM.kml',
          map: map
        });
      }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The link provided for the KML on github.com is being served with a MIME type of "text/plain", it should be "application/vnd.google-earth.kml+xml"

3 KML MIME Types
When responding to a request from Google Earth (or any Earth browser), a KML server must follow a certain set of rules so that Google Earth can correctly interpret its responses.

Upon success, the server must return a response code of HTTP 200 and set the response's content-type to a suitable MIME type, as described here.

Google Earth reads KML and KMZ files. The MIME type for KML files is application/vnd.google-earth.kml+xml

The MIME type for KMZ files is
application/vnd.google-earth.kmz

validator results:

Recommendations

This feed is valid, but interoperability with the widest range of feed readers could be improved by implementing the following recommendations.

Feeds should not be served with the "text/plain" media type

If I copy your KML to my server (which serves KML with the correct MIME type), it works

BTW - If I point the validator to the link in the code in your question, it doesn't work at all, it isn't a KML file, but if I point the KmlLayer URL to that, it also works


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...