how can I determine when Google Maps loadGeoJson completes?

后端 未结 3 950
粉色の甜心
粉色の甜心 2020-12-18 10:09

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.

3条回答
  •  一个人的身影
    2020-12-18 10:48

    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
    });
    

提交回复
热议问题