【问题标题】:Setting marker Label in google maps api在谷歌地图 api 中设置标记标签
【发布时间】:2018-03-12 14:47:46
【问题描述】:

我在为 google maps api 找出标签时遇到了一些麻烦。我正在调用我的 geojson 并像这样加载我的地​​图:

    var map;
function init() {
  map = new google.maps.Map(document.getElementById('map'), {
    zoom: 11,
    center: {lat: 43.2, lng: -84.65},
mapTypeId: google.maps.MapTypeId.ROADMAP
  });

  map.data.loadGeoJson(
      'Maps/Newark.geojson');         
  map.data.setStyle({
      icon: {
          path: google.maps.SymbolPath.CIRCLE,
          scale: 4,
          fillColor: 'blue',
          fillOpacity: .5,
          strokeColor: 'blue',
          strokeWeight: 1
        }
   })  
}

这是示例 geojson 数据:

    {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
   "type": "Point",
   "coordinates":  [ -84.69729,43.24808 ]
},
"properties": {
"Name":"Name 1",
"Address":"My Address",
"City":"town",
"State":"ST",
"Zip":12345,
"Group":"Newark"
}
},
{
"type": "Feature",
"geometry": {
   "type": "Point",
   "coordinates":  [ -84.58872,43.23395 ]
},
"properties": {
"Name":"Name 2",
"Address":"another address",
"City":"town",
"State":"ST",
"Zip": 12345,
"Group":"Newark"
}
}
]
}

我想要做的是将属性命名为标记的标签。

我尝试了this,但无法成功。

【问题讨论】:

  • 你能弄清楚如何为 geoJson 标记显示标签吗?
  • 是的,我使用了下面的答案。

标签: google-maps-api-3 label geojson


【解决方案1】:

修改自Google Maps API draw a text from GeoJSON point geometry

变化:

  1. 删除阅读不相关的属性
  2. 删除map.data.setMap(null);(因为我假设您仍然希望显示标记

proof of concept fiddle

代码 sn-p:

function initialize() {
  var mapOptions = {
    zoom: 14,
    center: new google.maps.LatLng(43.23395, -84.58872),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };

  var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
  var bounds = new google.maps.LatLngBounds();
  map.data.addListener('addfeature', function(evt) {
    if (evt.feature.getGeometry().getType() == "Point") {


      var coord = evt.feature.getGeometry().get();
      bounds.extend(coord);
      map.fitBounds(bounds);
      var labelText = evt.feature.getProperty("Name");
      var labelDiv = document.createElement("div");
      labelDiv.innerHTML = labelText;
      labelDiv.setAttribute("id", "label");

      var myOptions = {
        content: labelDiv,
        boxStyle: {
          border: "none",
          textAlign: "center",
          width: "50px"
        },
        disableAutoPan: true,
        pixelOffset: new google.maps.Size(-25, 0),
        position: coord,
        closeBoxURL: "",
        isHidden: false,
        pane: "mapPane",
        enableEventPropagation: true
      };

      var ibLabel = new InfoBox(myOptions);
      ibLabel.open(map);
    }
  });

  map.data.addGeoJson(geoJson);
}
google.maps.event.addDomListener(window, "load", initialize);

var geoJson = {
  "type": "FeatureCollection",
  "features": [{
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [-84.69729, 43.24808]
      },
      "properties": {
        "Name": "Name 1",
        "Address": "My Address",
        "City": "town",
        "State": "ST",
        "Zip": 12345,
        "Group": "Newark"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [-84.58872, 43.23395]
      },
      "properties": {
        "Name": "Name 2",
        "Address": "another address",
        "City": "town",
        "State": "ST",
        "Zip": 12345,
        "Group": "Newark"
      }
    }
  ]
};
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px;
  background-color: white;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script src="https://cdn.rawgit.com/googlemaps/v3-utility-library/master/infobox/src/infobox.js"></script>
<div id="map_canvas"></div>

【讨论】:

  • 我收到几个错误:未捕获的 ReferenceError:google 未在(索引):74(匿名)@(索引):74 infobox.js:128 未捕获的 ReferenceError:google 未定义在 infobox.js:128,我还需要知道如何使用路径 (Maps/myfile.geojson) 加载外部 goejson 文件
  • 这些都是不同的问题。 1. 对loadGeoJson 的调用将加载您的外部文件(如果它原本可以工作),addGeoJson 让我加载您提供的数据并创建一个工作示例。 2.google is not defined是由于异步加载API,而InfoBox库在加载之前是同步的。
猜你喜欢
  • 2016-04-08
  • 2015-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
相关资源
最近更新 更多