Skip to content Skip to sidebar Skip to footer

Google Maps API: GetFeatureById For Feature Collection Not Working

I try to change the style of a specific feature of a feature collection data overlay. This is a snippet of my json: and this is the relevant snippet from my map.js And I am alway

Solution 1:

You need to call map.data.getFeatureById(1) inside the callback function (so it doesn't execute before the GeoJson has loaded).

from the documentation:

loadGeoJson(url:string, options?:Data.GeoJsonOptions, callback?:function(Array))

Return Value: None

Loads GeoJS

proof of concept fiddle

code snippet:

var geocoder;
var map;

function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      zoom: 4,
      center: {
        lat: -28,
        lng: 137
      },
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  // map.data.addGeoJson(geoJson);
  map.data.loadGeoJson(
    'https://api.myjson.com/bins/1teyu', {},
    function(features) {
      console.log(map.data.getFeatureById(1));
      console.log(map.data.getFeatureById(1).getProperty("letter"));
    });
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>

Post a Comment for "Google Maps API: GetFeatureById For Feature Collection Not Working"