I have a scenario where users can store their mapping styling. When a user navigates back to that page with the map it should display the map the way it was previously.
addfeature event is definitely invoked by loadGeoJson, if isn't for you is some setup error.
loadGeoJson has an optional callback parameter what is invoked after all features were loaded and has the features as parameter.
https://developers.google.com/maps/documentation/javascript/3.exp/reference
loadGeoJson(url:string, options?:Data.GeoJsonOptions, callback?:function(Array))
You can signal your code from this callback that it can continue processing.
map.data.loadGeoJson('google.json', null, function (features) {
map.fitBounds(bounds); // or do other stuff what requires all features loaded
});
You could also wrap loadGeoJson into a promise and resolve the promise in loadGeoJson's callback.
function loadGeoJson(url, options) {
var promise = new Promise(function (resolve, reject) {
try {
map.data.loadGeoJson(url, options, function (features) {
resolve(features);
});
} catch (e) {
reject(e);
}
});
return promise;
}
// Somewhere else in your code:
var promise = loadGeoJson('studs.json');
promise.then(function (features) {
// Do stuff
});
promise.catch(function (error) {
// Handle error
});