简体   繁体   中英

How to catch exceptions when loading geoJSON into data layer

I am loading a feature collection into the map via maps.data.loadGeoJson(). Unfortunately, my data source occasionally returns a malformed GeoJson object (features is null, rather than an empty array) throwing an InvalidValueError.

An example of that data:

{"type":"FeatureCollection","features":null} 

I'd like to be able to catch this error, but because loadGeoJson() is an asynchronous call, by the time the exception is thrown I have already exited the try block. I have read ( Is it possible to catch exceptions thrown in a JavaScript async callback? ) that the way to handle this is by placing the try/catch block in the callback.

Sounds pretty reasonable but I'm not sure what I should be setting as a condition in the try block.

map.data.loadGeoJson(url ,null,
    function(data){
        try(what_exactly){

        }catch(e){ }
    }
);

Is this the right approach, or am I on the wrong boat? If I'm on the wrong boat, which is the right one?

Thanks!

Instead of using loadGeoJson , use addGeoJson .

  • loadGeoJson
    • Loads GeoJSON from a URL, and adds the features to the collection.
    • Return Value: None
  • addGeoJson
    • Adds GeoJSON features to the collection. Give this method a parsed JSON. The imported features are returned. Throws an exception if the GeoJSON could not be imported .
    • Return Value: Array< Data.Feature >

Here is a related SO question that provides a code implementation about addGeoJson .

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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