如何移除Google Maps JavaScript API中KML标记的图标并显示标签?

huangapple go评论51阅读模式
英文:

How do I remove the icon and show the label of a KML placemark in Google Maps JavaScript API?

问题

以下是您要翻译的内容:

"I'm using Google Maps JavaScript API and loading a KML layer to display points.

I want it to display the labels but not the blue point markers.

Here is what it looks like now:

如何移除Google Maps JavaScript API中KML标记的图标并显示标签?

The blue markers are where I want the labels to be.

Here is the relevant section from the KML:

<Style id="normalPlacemark">
<IconStyle>
<Icon><href>https://upload.wikimedia.org/wikipedia/commons/8/8c/Transparent.png</href></Icon>
</IconStyle>
<LineStyle>
<gx:labelVisibility>1</gx:labelVisibility>
</LineStyle>
</Style>
<Folder>
<name>Labels</name>
<Placemark>
<name>3H</name>
<Point><coordinates>-74.784880,41.752639,0</coordinates></Point>
</Placemark>
<Placemark>
<name>4F</name>
<Point><coordinates>-74.985419,42.619435,0</coordinates></Point>
</Placemark>
<Placemark>
<name>4G</name>
<Point><coordinates>-74.444085,42.514970,0</coordinates></Point>
</Placemark>
<Placemark>
<name>4H</name>
<Point><coordinates>-74.126351,42.508052,0</coordinates></Point>
</Placemark>
<Placemark>
<name>4O</name>
<Point><coordinates>-75.214074,42.230072,0</coordinates></Point>
</Placemark>
<Placemark>
<name>4P</name>
<Point><coordinates>-74.795944,42.201073,0</coordinates></Point>
</Placemark>
<Placemark>
<name>4R</name>
<Point><coordinates>-74.418647,42.266143,0</coordinates></Point>
</Placemark>
<Placemark>
<name>4W</name>
<Point><coordinates>-75.115810,41.965973,0</coordinates></Point>
</Placemark>
<Placemark>
<name>6G</name>
<Point><coordinates>-76.005867,43.998634,0</coordinates></Point>
</Placemark>
</Folder>

I've tried messing with this in every way I can think of but I think I just don't understand KML well enough. How do I get these blue markers to stop appearing and the labels to start appearing?"

如果您需要进一步的帮助,请告诉我。

英文:

I'm using Google Maps JavaScript API and loading a KML layer to display points.

I want it to display the labels but not the blue point markers.

Here is what it looks like now:

如何移除Google Maps JavaScript API中KML标记的图标并显示标签?

The blue markers are where I want the labels to be.

Here is the relevant section from the KML:

&lt;Style id=&quot;normalPlacemark&quot;&gt;
	&lt;IconStyle&gt;
		&lt;Icon&gt;&lt;href&gt;https://upload.wikimedia.org/wikipedia/commons/8/8c/Transparent.png&lt;/href&gt;&lt;/Icon&gt;
	&lt;/IconStyle&gt;
	&lt;LineStyle&gt;
		&lt;gx:labelVisibility&gt;1&lt;/gx:labelVisibility&gt;
	&lt;/LineStyle&gt;
&lt;/Style&gt;
&lt;Folder&gt;
  &lt;name&gt;Labels&lt;/name&gt;
  &lt;Placemark&gt;
    &lt;name&gt;3H&lt;/name&gt;
    &lt;Point&gt;&lt;coordinates&gt;-74.784880,41.752639,0&lt;/coordinates&gt;&lt;/Point&gt;
  &lt;/Placemark&gt;
  &lt;Placemark&gt;
    &lt;name&gt;4F&lt;/name&gt;
    &lt;Point&gt;&lt;coordinates&gt;-74.985419,42.619435,0&lt;/coordinates&gt;&lt;/Point&gt;
  &lt;/Placemark&gt;
  &lt;Placemark&gt;
    &lt;name&gt;4G&lt;/name&gt;
    &lt;Point&gt;&lt;coordinates&gt;-74.444085,42.514970,0&lt;/coordinates&gt;&lt;/Point&gt;
  &lt;/Placemark&gt;
  &lt;Placemark&gt;
    &lt;name&gt;4H&lt;/name&gt;
    &lt;Point&gt;&lt;coordinates&gt;-74.126351,42.508052,0&lt;/coordinates&gt;&lt;/Point&gt;
  &lt;/Placemark&gt;
  &lt;Placemark&gt;
    &lt;name&gt;4O&lt;/name&gt;
    &lt;Point&gt;&lt;coordinates&gt;-75.214074,42.230072,0&lt;/coordinates&gt;&lt;/Point&gt;
  &lt;/Placemark&gt;
  &lt;Placemark&gt;
    &lt;name&gt;4P&lt;/name&gt;
    &lt;Point&gt;&lt;coordinates&gt;-74.795944,42.201073,0&lt;/coordinates&gt;&lt;/Point&gt;
  &lt;/Placemark&gt;
  &lt;Placemark&gt;
    &lt;name&gt;4R&lt;/name&gt;
    &lt;Point&gt;&lt;coordinates&gt;-74.418647,42.266143,0&lt;/coordinates&gt;&lt;/Point&gt;
  &lt;/Placemark&gt;
  &lt;Placemark&gt;
    &lt;name&gt;4W&lt;/name&gt;
    &lt;Point&gt;&lt;coordinates&gt;-75.115810,41.965973,0&lt;/coordinates&gt;&lt;/Point&gt;
  &lt;/Placemark&gt;
  &lt;Placemark&gt;
    &lt;name&gt;6G&lt;/name&gt;
    &lt;Point&gt;&lt;coordinates&gt;-76.005867,43.998634,0&lt;/coordinates&gt;&lt;/Point&gt;
  &lt;/Placemark&gt;
&lt;/Folder&gt;

I've tried messing with this in every way I can think of but I think I just don't understand KML well enough. How do I get these blue markers to stop appearing and the labels to start appearing?

答案1

得分: 1

以下是您要翻译的内容:

function createMarker(placemark, doc) {
  var markerOptions = {
    optimized: true,
    clickable: false,
    position: placemark.latlng,
    map: map,
    label: {
      text: placemark.name,
      fontSize: "18px"
    },
    icon: "https://upload.wikimedia.org/wikipedia/commons/8/8c/Transparent.png",
    visible: true,
  };
  var marker = new google.maps.Marker(markerOptions);
  doc.markers.push(marker);
  return marker;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Import KML</title>
</head>

<body>
  <div id="map_canvas"></div>
  <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"></script>
  <script src="https://cdn.rawgit.com/geocodezip/geoxml3/master/polys/geoxml3.js"></script>
  <script type="text/javascript" src="https://cdn.rawgit.com/geocodezip/geoxml3/master/ProjectedOverlay.js"></script>
</body>

</html>

希望这可以帮助您。如果您需要更多信息,请随时提出。

英文:

One option to display the Placemark names as labels using geoxml3 (based of the related question: Change font size of placemark's name when zooming) would be to create a custom createMarker function for geoxml3 that uses your transparent icon and sets the Marker's label to the name text of the placemark:

function createMarker(placemark, doc) {
  var markerOptions = {
    optimized: true,
    clickable: false,
    position: placemark.latlng,
    map: map,
    label: {
      text: placemark.name,
      fontSize: &quot;18px&quot;
    },
    icon: &quot;https://upload.wikimedia.org/wikipedia/commons/8/8c/Transparent.png&quot;,
    visible: true,
  };
  var marker = new google.maps.Marker(markerOptions);
  doc.markers.push(marker);
  return marker;
}

You could continue to use KmlLayer to display the polygons, or use geoxml3 to display those as well.

如何移除Google Maps JavaScript API中KML标记的图标并显示标签?

code snippet:

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->

var geoXml;
var map;

function createMarker(placemark, doc) {
  var markerOptions = {
    optimized: true,
    clickable: false,
    position: placemark.latlng,
    map: map,
    label: {
      text: placemark.name,
      fontSize: &quot;18px&quot;
    },
    icon: &quot;https://upload.wikimedia.org/wikipedia/commons/8/8c/Transparent.png&quot;,
    visible: true,
  };

  var marker = new google.maps.Marker(markerOptions);
  doc.markers.push(marker);

  return marker;
}

function initialize() {
  var myOptions = {
    center: new google.maps.LatLng(105.4009049697155, 45.9307395294156),
    zoom: 15,
  };
  map = new google.maps.Map(
    document.getElementById(&quot;map_canvas&quot;),
    myOptions
  );
  geoXml = new geoXML3.parser({
    map: map,
    singleInfoWindow: false,
    processStyles: false,
    createMarker: createMarker,
    afterParse: useTheData,
  });
  geoXml.parseKmlString(kmlString);

  google.maps.event.addListener(map, &quot;zoom_changed&quot;, function() {
    console.log(&quot;zoom=&quot; + map.getZoom());
    if (map.getZoom() &lt; 7) {
      geoXml.docs[0].markers.forEach(function(placemark) {
        if (placemark.getMap() != null) {
          var label = placemark.getLabel();
          console.log(label);
          if (typeof label === &#39;string&#39;) {
            var labelObj = {};
            labelObj.text = label;
            labelObj.fontSize = &quot;0px&quot;;
            placemark.setLabel(labelObj);
          } else {
            label.fontSize = &quot;0px&quot;;
            placemark.setLabel(label);
          }

        }
      });
    } else {
      geoXml.docs[0].markers.forEach(function(placemark) {
        if (placemark.getMap() != null) {
          var label = placemark.getLabel();
          // console.log(label);
          if (typeof label === &#39;string&#39;) {
            var labelObj = {};
            labelObj.text = label;
            label.fontSize = &quot;18px&quot;;
            placemark.setLabel(labelObj);
          } else {
            label.fontSize = &quot;18px&quot;;
            placemark.setLabel(label);
          }
        }
      });
    }
  });

}

function useTheData(doc) {
  for (var i = 0; i &lt; doc[0].placemarks.length; i++) {
    var placemark = doc[0].placemarks[i];
    if (placemark.polygon) {
      placemark.polygon.normalStyle = {
        strokeColor: &quot;#ffff00&quot;,
        strokeWeight: 1,
        strokeOpacity: 0,
        fillColor: &quot;#ffff00&quot;,
        fillOpacity: 0,
      };
      placemark.polygon.setOptions(placemark.polygon.normalStyle);
    }
  }
}
var kmlString = &#39;&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&lt;kml xmlns=&quot;http://www.opengis.net/kml/2.2&quot; xmlns:gx=&quot;http://www.google.com/kml/ext/2.2&quot;&gt;&lt;Document&gt;&lt;Style id=&quot;normalPlacemark&quot;&gt;&lt;IconStyle&gt;&lt;Icon&gt;&lt;href&gt;https://upload.wikimedia.org/wikipedia/commons/8/8c/Transparent.png&lt;/href&gt;&lt;/Icon&gt;&lt;/IconStyle&gt;&lt;LineStyle&gt;&lt;gx:labelVisibility&gt;1&lt;/gx:labelVisibility&gt;&lt;/LineStyle&gt;&lt;LabelStyle&gt;&lt;color&gt;ff0000cc&lt;/color&gt;&lt;colorMode&gt;random&lt;/colorMode&gt;&lt;scale&gt;1.5&lt;/scale&gt;&lt;/LabelStyle&gt;&lt;/Style&gt;&lt;Folder&gt;&lt;name&gt;Labels&lt;/name&gt;&lt;Placemark&gt;&lt;name&gt;3H&lt;/name&gt;&lt;styleUrl&gt;#normalPlacemark&lt;/styleUrl&gt;&lt;Point&gt;&lt;coordinates&gt;-74.784880,41.752639,0&lt;/coordinates&gt;&lt;/Point&gt;&lt;/Placemark&gt;&lt;Placemark&gt;&lt;name&gt;4F&lt;/name&gt;&lt;styleUrl&gt;#normalPlacemark&lt;/styleUrl&gt;&lt;Point&gt;&lt;coordinates&gt;-74.985419,42.619435,0&lt;/coordinates&gt;&lt;/Point&gt;&lt;/Placemark&gt;&lt;Placemark&gt;&lt;name&gt;4G&lt;/name&gt;&lt;styleUrl&gt;#normalPlacemark&lt;/styleUrl&gt;&lt;Point&gt;&lt;coordinates&gt;-74.444085,42.514970,0&lt;/coordinates&gt;&lt;/Point&gt;&lt;/Placemark&gt;&lt;Placemark&gt;&lt;name&gt;4H&lt;/name&gt;&lt;styleUrl&gt;#normalPlacemark&lt;/styleUrl&gt;&lt;Point&gt;&lt;coordinates&gt;-74.126351,42.508052,0&lt;/coordinates&gt;&lt;/Point&gt;&lt;/Placemark&gt;&lt;Placemark&gt;&lt;name&gt;4O&lt;/name&gt;&lt;styleUrl&gt;#normalPlacemark&lt;/styleUrl&gt;&lt;Point&gt;&lt;coordinates&gt;-75.214074,42.230072,0&lt;/coordinates&gt;&lt;/Point&gt;&lt;/Placemark&gt;&lt;Placemark&gt;&lt;name&gt;4P&lt;/name&gt;&lt;styleUrl&gt;#normalPlacemark&lt;/styleUrl&gt;&lt;Point&gt;&lt;coordinates&gt;-74.795944,42.201073,0&lt;/coordinates&gt;&lt;/Point&gt;&lt;/Placemark&gt;&lt;Placemark&gt;&lt;name&gt;4R&lt;/name&gt;&lt;styleUrl&gt;#normalPlacemark&lt;/styleUrl&gt;&lt;Point&gt;&lt;coordinates&gt;-74.418647,42.266143,0&lt;/coordinates&gt;&lt;/Point&gt;&lt;/Placemark&gt;&lt;Placemark&gt;&lt;name&gt;4W&lt;/name&gt;&lt;styleUrl&gt;#normalPlacemark&lt;/styleUrl&gt;&lt;Point&gt;&lt;coordinates&gt;-75.115810,41.965973,0&lt;/coordinates&gt;&lt;/Point&gt;&lt;/Placemark&gt;&lt;Placemark&gt;&lt;name&gt;6G&lt;/name&gt;&lt;styleUrl&gt;#normalPlacemark&lt;/styleUrl&gt;&lt;Point&gt;&lt;coordinates&gt;-76.005867,43.998634,0&lt;/coordinates&gt;&lt;/Point&gt;&lt;/Placemark&gt;&lt;/Folder&gt;&lt;/Document&gt;&lt;/kml&gt;&#39;;

google.maps.event.addDomListener(window, &#39;load&#39;, initialize);

<!-- language: lang-css -->

html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  padding: 0px;
  margin: 0px;
}

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;

&lt;head&gt;
  &lt;meta charset=&quot;UTF-8&quot;&gt;
  &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  &lt;title&gt;Import KML&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
  &lt;div id=&quot;map_canvas&quot;&gt;&lt;/div&gt;
  &lt;script src=&quot;https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;https://cdn.rawgit.com/geocodezip/geoxml3/master/polys/geoxml3.js&quot;&gt;&lt;/script&gt;
  &lt;script type=&quot;text/javascript&quot; src=&quot;https://cdn.rawgit.com/geocodezip/geoxml3/master/ProjectedOverlay.js&quot;&gt;&lt;/script&gt;
&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

modified KML:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;kml xmlns=&quot;http://www.opengis.net/kml/2.2&quot; xmlns:gx=&quot;http://www.google.com/kml/ext/2.2&quot;&gt;
	&lt;Document&gt;
		&lt;Style id=&quot;normalPlacemark&quot;&gt;
			&lt;IconStyle&gt;
				&lt;Icon&gt;
					&lt;href&gt;https://upload.wikimedia.org/wikipedia/commons/8/8c/Transparent.png&lt;/href&gt;
				&lt;/Icon&gt;
			&lt;/IconStyle&gt;
			&lt;LineStyle&gt;
				&lt;gx:labelVisibility&gt;1&lt;/gx:labelVisibility&gt;
			&lt;/LineStyle&gt;
			&lt;LabelStyle&gt;
				&lt;color&gt;ff0000cc&lt;/color&gt;
				&lt;colorMode&gt;random&lt;/colorMode&gt;
				&lt;scale&gt;1.5&lt;/scale&gt;
			&lt;/LabelStyle&gt;
		&lt;/Style&gt;
		&lt;Folder&gt;
			&lt;name&gt;Labels&lt;/name&gt;
			&lt;Placemark&gt;
				&lt;name&gt;3H&lt;/name&gt;
				&lt;styleUrl&gt;#normalPlacemark&lt;/styleUrl&gt; &lt;!-- missing --&gt;
				&lt;Point&gt;
					&lt;coordinates&gt;-74.784880,41.752639,0&lt;/coordinates&gt;
				&lt;/Point&gt;
			&lt;/Placemark&gt;
			&lt;Placemark&gt;
				&lt;name&gt;4F&lt;/name&gt;
				&lt;styleUrl&gt;#normalPlacemark&lt;/styleUrl&gt;
				&lt;Point&gt;
					&lt;coordinates&gt;-74.985419,42.619435,0&lt;/coordinates&gt;
				&lt;/Point&gt;
			&lt;/Placemark&gt;
			&lt;Placemark&gt;
				&lt;name&gt;4G&lt;/name&gt;
				&lt;styleUrl&gt;#normalPlacemark&lt;/styleUrl&gt;
				&lt;Point&gt;
					&lt;coordinates&gt;-74.444085,42.514970,0&lt;/coordinates&gt;
				&lt;/Point&gt;
			&lt;/Placemark&gt;
			&lt;Placemark&gt;
				&lt;name&gt;4H&lt;/name&gt;
				&lt;styleUrl&gt;#normalPlacemark&lt;/styleUrl&gt;
				&lt;Point&gt;
					&lt;coordinates&gt;-74.126351,42.508052,0&lt;/coordinates&gt;
				&lt;/Point&gt;
			&lt;/Placemark&gt;
			&lt;Placemark&gt;
				&lt;name&gt;4O&lt;/name&gt;
				&lt;styleUrl&gt;#normalPlacemark&lt;/styleUrl&gt;
				&lt;Point&gt;
					&lt;coordinates&gt;-75.214074,42.230072,0&lt;/coordinates&gt;
				&lt;/Point&gt;
			&lt;/Placemark&gt;
			&lt;Placemark&gt;
				&lt;name&gt;4P&lt;/name&gt;
				&lt;styleUrl&gt;#normalPlacemark&lt;/styleUrl&gt;
				&lt;Point&gt;
					&lt;coordinates&gt;-74.795944,42.201073,0&lt;/coordinates&gt;
				&lt;/Point&gt;
			&lt;/Placemark&gt;
			&lt;Placemark&gt;
				&lt;name&gt;4R&lt;/name&gt;
				&lt;styleUrl&gt;#normalPlacemark&lt;/styleUrl&gt;
				&lt;Point&gt;
					&lt;coordinates&gt;-74.418647,42.266143,0&lt;/coordinates&gt;
				&lt;/Point&gt;
			&lt;/Placemark&gt;
			&lt;Placemark&gt;
				&lt;name&gt;4W&lt;/name&gt;
				&lt;styleUrl&gt;#normalPlacemark&lt;/styleUrl&gt;
				&lt;Point&gt;
					&lt;coordinates&gt;-75.115810,41.965973,0&lt;/coordinates&gt;
				&lt;/Point&gt;
			&lt;/Placemark&gt;
			&lt;Placemark&gt;
	
			&lt;name&gt;6G&lt;/name&gt;
				&lt;styleUrl&gt;#normalPlacemark&lt;/styleUrl&gt;
				&lt;Point&gt;
					&lt;coordinates&gt;-76.005867,43.998634,0&lt;/coordinates&gt;
				&lt;/Point&gt;
			&lt;/Placemark&gt;
		&lt;/Folder&gt;
	&lt;/Document&gt;
&lt;/kml&gt;

live version

huangapple
  • 本文由 发表于 2023年5月25日 21:02:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332574.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定