gpt4 book ai didi

javascript - 从文本区域在谷歌地图上绘制kml图层

转载 作者:行者123 更新时间:2023-11-28 15:24:15 28 4
gpt4 key购买 nike

我想使用 javascript api 在 google map 上绘制一个 kml 图层,并将 kml 代码复制到文本区域中,就像他们在此页面中所做的那样:

http://display-kml.appspot.com/

我在文档中找到的所有示例都从文件加载 kml 层;

最佳答案

Example 。使用 geoxml3 第三方 KML 解析器和另一个第三方库,添加了编辑 KML 的功能(这听起来不像您需要的)。

working fiddle

代码片段:

var geocoder;
var map;

function initialize() {
map = new google.maps.Map(
document.getElementById("map_canvas"), {
center: new google.maps.LatLng(37.4419, -122.1419),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
document.getElementById("kmlString").value = '<kml><Document><Placemark id="Tester"><styleUrl>#transYellowPolyActive</styleUrl><name>Tester</name><Polygon><outerBoundaryIs><LinearRing><tessellate>0</tessellate><coordinates>6.30889892578125,53.28820543193896 6.29791259765625,53.28410053532493 6.233367919921875,53.21096737507053 6.5093994140625,53.19698389904798 6.50390625,53.27096221595853</coordinates></LinearRing></outerBoundaryIs></Polygon></Placemark><Placemark id="jahoor"><styleUrl>#transYellowPolyActive</styleUrl><name>jahoor</name><Polygon><outerBoundaryIs><LinearRing><tessellate>0</tessellate><coordinates>6.168479919433594,53.30318495818702 6.143760681152344,53.29579845109269 6.138267517089844,53.27281003615208 6.195259094238281,53.2707568976735 6.2010955810546875,53.29867113343524</coordinates></LinearRing></outerBoundaryIs></Polygon></Placemark></Document></kml>';
}
google.maps.event.addDomListener(window, "load", initialize);

var mapOverlays = [];
var geoXml = null;
//create a common infoWindow object
var infWindow = new google.maps.InfoWindow();

function setMapFromKML(kmlString) {
if (kmlString.length == 0) {
return false;
}
if (typeof geoXML3 == "undefined") { // check for include of geoxml3 parser
// http://code.google.com/p/geoxml3/
alert("geoxml3.js not included");
return;
}
if (!geoXml)
geoXml = new geoXML3.parser({
map: map,
zoom: false,
suppressInfoWindows: true
});

geoXml.parseKmlString(kmlString);

var tmpOverlay, ovrOptions;
for (var m = 0; m < geoXml.docs[0].placemarks.length; m++) {
if (geoXml.docs[0].placemarks[m].Polygon) {

tmpOverlay = geoXml.docs[0].placemarks[m].polygon;
tmpOverlay.type = "polygon";
} else if (geoXml.docs[0].placemarks[m].LineString) {

tmpOverlay = geoXml.docs[0].placemarks[m].polyline;
tmpOverlay.type = "polyline";
} else if (geoXml.docs[0].placemarks[m].Point) {

tmpOverlay = geoXml.docs[0].placemarks[m].marker;
tmpOverlay.type = "marker";
}

if (geoXml.docs[0].placemarks[m].name) {
tmpOverlay.title = geoXml.docs[0].placemarks[m].name;
} else {
tmpOverlay.title = "";
}

if (geoXml.docs[0].placemarks[m].description) {
tmpOverlay.content = geoXml.docs[0].placemarks[m].description;
} else {
tmpOverlay.content = "";
}

//attach the click listener to the overlay
AttachClickListener(tmpOverlay);

//save the overlay in the array
mapOverlays.push(tmpOverlay);
}
map.fitBounds(geoXml.docs[0].bounds);
}

function AttachClickListener(overlay) {
google.maps.event.addListener(overlay, "click", function(clkEvent) {
var infContent = GetContent(overlay);
openInfowindow(overlay, clkEvent.latLng, infContent);
});
}

function GetContent(overlay) {
var content = '<div><h3>' + overlay.title + '</h3>' + overlay.content + '<br></div>';
return content;
}

function openInfowindow(overlay, latLng, content) {
var div = document.createElement('div');
div.innerHTML = content;
setStyle(div, {
height: "100%"
});
infWindow.setContent(div);
infWindow.setPosition(latLng);
infWindow.relatedOverlay = overlay;
var t = overlay.get('fillColor');
infWindow.open(mapObj);
}
html,
body,
#map_canvas {
height: 500px;
width: 500px;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=drawing,geometry"></script>
<script src="https://cdn.rawgit.com/geocodezip/geoxml3/master/polys/geoxml3.js"></script>
<script src="https://cdn.rawgit.com/geocodezip/geoxml3/master/ProjectedOverlay.js"></script>

<div id="map_canvas" style="border: 2px solid #3872ac;"></div>
<input type="button" value="parse KML to map" onclick="setMapFromKML(document.getElementById('kmlString').value)" />
<textarea id="kmlString" style="width:100%; height:500px"></textarea>

关于javascript - 从文本区域在谷歌地图上绘制kml图层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29813983/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com