简体   繁体   English

通过geojson属性更改Google Maps标记图标

[英]Changing Google Maps marker icons through geojson properties

I have to plot markers in Google Maps, using geojson files like this: 我必须使用geojson文件在Google Maps中绘制标记:

{"type": "FeatureCollection", "features": [{"geometry": {"type": "Point", 
"coordinates": [-77.76242552657358, -10.749107039737899]}, "type": 
"Feature", "properties": {"icon": "chart_red.png"}}, {"geometry": {"type": 
"Point", "coordinates": [-77.76242552657358, -10.749107039737899]}, "type": 
"Feature", "properties": {"icon": "chart_red.png"}}, {"geometry": {"type": 
"Point", "coordinates": [-77.7623641788643, -10.7500605616405]}, "type": 
"Feature", "properties": {"icon": "chart_green.png"}}, {"geometry": {"type": 
"Point", "coordinates": [-77.77211472346339, -10.728081474512]}, "type": 
"Feature", "properties": {"icon": "chart_green.png"}}, {"geometry": {"type": 
"Point", "coordinates": [-77.7614956126977, -10.750647917225699]}, "type": 
"Feature", "properties": {"icon": "chart_green.png"}}, {"geometry": {"type": 
"Point", "coordinates": [-77.76185649510131, -10.7516817240557]}, "type": 
"Feature", "properties": {"icon": "chart_red.png"}}, {"geometry": {"type": 
"Point", "coordinates": [-77.76185649510131, -10.7516817240557]}, "type": 
"Feature", "properties": {"icon": "chart_red.png"}}, {"geometry": {"type": 
"Point", "coordinates": [-77.76242552657358, -10.749107039737899]}, "type": 
"Feature", "properties": {"icon": "chart_red.png"}}, {"geometry": {"type": 
"Point", "coordinates": [-77.7469388021172, -10.7729040573081]}, "type": 
"Feature", "properties": {"icon": "chart_green.png"}}]}

where every marker is represented by a Feature with Point geometry and have a property named "icon" that points to a marker image in the same domain. 其中每个标记都由具有点几何的要素表示,并具有一个名为“ icon”的属性,该属性指向同一域中的标记图像。 Using the following javascript code, I'm able to plot the markers, but all of them have the default red marker image. 使用以下javascript代码,我可以绘制标记,但是所有标记都有默认的红色标记图像。

function initMap() {

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 3,
      center: {lat: -12, lng: -77},
      mapTypeId: google.maps.MapTypeId.TERRAIN
    });

    var histPlacesCoordinates = map.data.loadGeoJson('hist1.geojson');
    for (var i = 0, length = histPlacesCoordinates.features.length; i < length; i++) {
      var data = histPlacesCoordinates.features[i],
        latLng = new google.maps.LatLng(data.geometry.coordinates[1], data.geometry.coordinates[0]);

      var histPlaces = new google.maps.Marker();
      histPlaces.setStyle({
          position: latLng, 
          map: map,
          icon: data.feature.getProperty("icon")
        });
    }
  }

What have I to correct to get each marker drawn with the icon specified in the geojson icon property? 我需要纠正什么以使每个标记都用geojson icon属性中指定的图标绘制?

I made it working here..Please see the fiddle 我在这里工作了。请看小提琴

 function initMap() {

  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 7,
    center: {
      lat: -12,
      lng: -77
    },
    mapTypeId: google.maps.MapTypeId.TERRAIN
  });

  var markerCollection = map.data.addGeoJson(geoJson);
  for (var i = 0, length = markerCollection.length; i < length; i++) {
    var feature = markerCollection[i];
    debugger;
    if (feature.getProperty('icon')) {
      map.data.setStyle(function(feature) {
        return {
          icon: feature.getProperty('icon')
        };
      });
    }
  }
}

Since I am loading geoJson from a variable I have used addGeoJson method. 由于我是从变量加载geoJson,因此我使用了addGeoJson方法。 That method will return the feature collection. 该方法将返回要素集合。 So we will iterate that and add style to each feature. 因此,我们将对其进行迭代并为每个功能添加样式。

  map.data.setStyle(function(feature) {
    return {
      icon: feature.getProperty('icon')
    };
  });

Also note that, loadGeoJson return value is null. 另请注意, loadGeoJson返回值为null。 see this answer. 看到这个答案。 so your variable histPlacesCoordinates will be undefined or null. 因此您的变量histPlacesCoordinates将为undefined或为null。

If you are planning to use loadGeoJson then you will get the featureCollection inside the callback. 如果您打算使用loadGeoJson那么您将在回调内获取featureCollection。 You can set the icon in that callBack by iterating that array also. 您还可以通过迭代该数组在该CallBack中设置图标。

loadGeoJson(url:string, options?:Data.GeoJsonOptions, callback?:function(Array)) loadGeoJson(URL:字符串,选项?:Data.GeoJsonOptions,回调?:函数(数组))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM