[view/map] Geometries can be provided as GeoJSON strings (useful e.g. in CSV files)

This commit is contained in:
amercader
2012-04-23 18:24:53 +01:00
parent 7def51b573
commit 9ed43dfdc6

View File

@@ -344,8 +344,14 @@ my.Map = Backbone.View.extend({
_getGeometryFromDocument: function(doc){ _getGeometryFromDocument: function(doc){
if (this.geomReady){ if (this.geomReady){
if (this.state.get('geomField')){ if (this.state.get('geomField')){
var value = doc.get(this.state.get('geomField'));
if (typeof(value) === 'string'){
// We have a GeoJSON string representation
return $.parseJSON(value);
} else {
// We assume that the contents of the field are a valid GeoJSON object // We assume that the contents of the field are a valid GeoJSON object
return doc.attributes[this.state.get('geomField')]; return value;
}
} else if (this.state.get('lonField') && this.state.get('latField')){ } else if (this.state.get('lonField') && this.state.get('latField')){
// We'll create a GeoJSON like point object from the two lat/lon fields // We'll create a GeoJSON like point object from the two lat/lon fields
var lon = doc.get(this.state.get('lonField')); var lon = doc.get(this.state.get('lonField'));
@@ -353,10 +359,7 @@ my.Map = Backbone.View.extend({
if (lon && lat) { if (lon && lat) {
return { return {
type: 'Point', type: 'Point',
coordinates: [ coordinates: [lon,lat]
doc.attributes[this.state.get('lonField')],
doc.attributes[this.state.get('latField')]
]
}; };
} }
} }
@@ -440,7 +443,12 @@ my.Map = Backbone.View.extend({
this.features.getBounds = function(){ this.features.getBounds = function(){
var bounds = new L.LatLngBounds(); var bounds = new L.LatLngBounds();
this._iterateLayers(function (layer) { this._iterateLayers(function (layer) {
bounds.extend(layer instanceof L.Marker ? layer.getLatLng() : layer.getBounds()); if (layer instanceof L.Marker){
bounds.extend(layer.getLatLng());
} else {
bounds.extend(layer.getBounds().getNorthEast());
bounds.extend(layer.getBounds().getSouthWest());
}
}, this); }, this);
return (typeof bounds.getNorthEast() !== 'undefined') ? bounds : null; return (typeof bounds.getNorthEast() !== 'undefined') ? bounds : null;
} }