diff --git a/src/view-map.js b/src/view-map.js index f0a75ae8..79b19492 100644 --- a/src/view-map.js +++ b/src/view-map.js @@ -347,13 +347,19 @@ my.Map = Backbone.View.extend({ if (typeof(value) === 'string'){ // We *may* have a GeoJSON string representation try { - return $.parseJSON(value); + value = $.parseJSON(value); } catch(e) { } - } else { - // We assume that the contents of the field are a valid GeoJSON object - return value; } + if (value && value.lat) { + // not yet geojson so convert + value = { + "type": "Point", + "coordinates": [value.lon || value.lng, value.lat] + }; + } + // We now assume that contents of the field are a valid GeoJSON object + return value; } else if (this.state.get('lonField') && this.state.get('latField')){ // We'll create a GeoJSON like point object from the two lat/lon fields var lon = doc.get(this.state.get('lonField')); diff --git a/test/view-map.test.js b/test/view-map.test.js index 3ad5a4d2..61e4d5a3 100644 --- a/test/view-map.test.js +++ b/test/view-map.test.js @@ -90,6 +90,23 @@ test('GeoJSON geom field', function () { view.remove(); }); +test('geom field non-GeoJSON', function () { + var data = [{ + location: { lon: 47, lat: 53}, + title: 'abc' + }]; + var dataset = recline.Backend.Memory.createDataset(data); + var view = new recline.View.Map({ + model: dataset + }); + + //Fire query, otherwise the map won't be initialized + dataset.query(); + + // Check that all features were created + equal(_getFeaturesCount(view.features), 1); +}); + test('Popup', function () { var dataset = GeoJSONFixture.getDataset(); var view = new recline.View.Map({