简体   繁体   中英

How to combine HTML and KML with Google-Earth-Plugin?

First, I want to thank you for the efforts in answering.

Second, I created a site that contains a plug-in of Google Earth, and I want to use a local KML file to show two coordinates.

HTML code:

<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"> </script>
<script type="text/javascript">
var ge;
google.load("earth", "1", {"other_params":"sensor=false"});

var fso, kmlString, fh;

function init() {
  google.earth.createInstance('map3d', initCB, failureCB);
}

function initCB(instance) {
  ge = instance;
  ge.getWindow().setVisibility(true);
}

function failureCB(errorCode) {
}

google.setOnLoadCallback(init);

</script>
</head>
<body>
    <div id="map3d" style="height: 300px; width: 400px;"></div>
</body>
</html>

KML code:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Placemark>
<name>Simple placemark</name>
<description>Attached to the ground. Intelligently places itself 
   at the height of the underlying terrain.</description>
<Point>
  <coordinates>-122.0822035425683,37.42228990140251,0</coordinates>
</Point>
</Placemark>
</kml>

I searched Google for some answers, but nothing helped me to understand how to combine between the HTML and KML (Google Earth Plug-in). please help me. Thank you very much, Orian.

function initCallback(pluginInstance) {
  ge = pluginInstance;
  ge.getWindow().setVisibility(true);

  // Earth is ready, we can add features to it
  addKmlFromUrl('http://path/to/your.kml');
}

function addKmlFromUrl(kmlUrl) {
  var link = ge.createLink('');
  link.setHref(kmlUrl);

  var networkLink = ge.createNetworkLink('');
  networkLink.setLink(link);
  networkLink.setFlyToView(true);

  ge.getFeatures().appendChild(networkLink);
}

or

function addKmlFromUrl(kmlUrl) {
  google.earth.fetchKml(ge, kmlUrl, kmlFinishedLoading);
}

function kmlFinishedLoading(kmlObject) {
  if (kmlObject) {
    ge.getFeatures().appendChild(kmlObject);
  }
}

https://developers.google.com/earth/articles/earthapikml

"use a local KML file"

Using the file:// handler in the browser may satisfy your needs, using a web-browser to access local files has it's own gotchas though, from security restrictions onwards.

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