1
0
Fork 0
forked from 0x2620/oxjs

make Ox.Map lead geocode results, in case someone wants to 'cache' them

This commit is contained in:
rolux 2011-04-29 16:56:40 +02:00
commit 506a2f2923
9 changed files with 97 additions and 31 deletions

View file

@ -131,7 +131,7 @@ Ox.Map = function(options, self) {
.appendTo(self.$toolbar)
}
self.$map = new Ox.Element({})
self.$map = new Ox.Element()
.css({
left: 0,
top: self.options.toolbar * 24 + 'px',
@ -317,6 +317,7 @@ Ox.Map = function(options, self) {
}
function canContain(outerBounds, innerBounds) {
// checks if outerBounds _can_ contain innerBounds
var outerSpan = outerBounds.toSpan(),
innerSpan = innerBounds.toSpan();
return outerSpan.lat() > innerSpan.lat() &&
@ -336,6 +337,7 @@ Ox.Map = function(options, self) {
Ox.print('Ox.Map clickMap')
if (self.options.clickable/* && !editing()*/) {
getPlaceByLatLng(event.latLng, self.map.getBounds(), function(place) {
Ox.print('>>>>', place)
if (place) {
addPlaceToMap(place);
//selectPlace(place.id);
@ -434,6 +436,7 @@ Ox.Map = function(options, self) {
}
function getPlaceByLatLng(latlng, bounds, callback) {
// gets the largest place at latlng that would fit in bounds
Ox.print('ll b', latlng, bounds)
var callback = arguments.length == 3 ? callback : bounds,
bounds = arguments.length == 3 ? bounds : null;
@ -443,25 +446,30 @@ Ox.Map = function(options, self) {
Ox.print('results', results)
var length = results.length;
if (status == google.maps.GeocoderStatus.OK) {
if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
if (bounds) {
Ox.forEach(results.reverse(), function(result, i) {
if (
i == length - 1 ||
canContain(bounds, result.geometry.bounds || result.geometry.viewport)
) {
callback(new Ox.MapPlace(parseGeodata(results[i])));
return false;
}
});
} else {
callback(new Ox.MapPlace(parseGeodata(results[0])));
}
if (bounds) {
Ox.forEach(results.reverse(), function(result, i) {
if (
i == length - 1 ||
canContain(bounds, result.geometry.bounds || result.geometry.viewport)
) {
callback(new Ox.MapPlace(parseGeodata(results[i])));
return false;
}
});
} else {
callback(null);
callback(new Ox.MapPlace(parseGeodata(results[0])));
}
}
if (
status == google.maps.GeocoderStatus.OK ||
status == google.maps.GeocoderStatus.ZERO_RESULTS
) {
triggerGeocodeEvent({
latLng: latlng,
results: results
});
} else {
//Ox.print('geocode failed:', status);
Ox.print('geocode failed:', status);
callback(null);
}
});
@ -472,12 +480,16 @@ Ox.Map = function(options, self) {
address: name
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
Ox.print('GEOCODER RESULT', results[0])
callback(new Ox.MapPlace(parseGeodata(results[0])));
} else {
callback(null);
}
callback(new Ox.MapPlace(parseGeodata(results[0])));
}
if (
status == google.maps.GeocoderStatus.OK ||
status == google.maps.GeocoderStatus.ZERO_RESULTS
) {
triggerGeocodeEvent({
address: name,
results: results
});
} else {
Ox.print('geocode failed:', status);
callback(null);
@ -804,6 +816,41 @@ Ox.Map = function(options, self) {
});
}
function triggerGeocodeEvent(data) {
// someone may want to cache google geocode data, so we fire an event.
// google puts functions like lat or lng on the objects' prototypes,
// so we create properly named properties, for json encoding
if (data.latLng) {
data.latLng = {
lat: data.latLng.lat(),
lng: data.latLng.lng()
}
}
data.results.forEach(function(result) {
['bounds', 'viewport'].forEach(function(key) {
if (result.geometry[key]) {
result.geometry[key] = {
northEast: {
lat: result.geometry[key].getNorthEast().lat(),
lng: result.geometry[key].getNorthEast().lng()
},
southWest: {
lat: result.geometry[key].getSouthWest().lat(),
lng: result.geometry[key].getSouthWest().lng()
}
}
}
});
if (result.geometry.location) {
result.geometry.location = {
lat: result.geometry.location.lat(),
lng: result.geometry.location.lng()
}
}
});
that.triggerEvent('geocode', data);
}
function undo() {
Ox.print('Map undo')
var place = getSelectedPlace();