Improved location widget

This commit is contained in:
Martin Bauer
2013-09-27 11:39:28 +02:00
parent 11135b5592
commit 05b7a2b969
8 changed files with 74 additions and 94 deletions

View File

@@ -1,76 +1,66 @@
($ || django.jQuery)(function($){
function location_field_load(map, location_based, zoom)
function location_field_load( map, base_field, initZoom )
{
var parent = map.parent().parent();
var location_map;
var location_coordinate = parent.find('input[type=text]');
function load() {
var point = new google.maps.LatLng(49.340174,10.890595);
var options = {
mapTypeId: google.maps.MapTypeId.HYBRID
};
var parent = map.parent().parent();
var coordinate_field = parent.find('input[type=text]');
var map;
var current_position;
//coordinate_field.hide();
coordinate_field.attr("readonly","readonly")
function load()
{
var options = { mapTypeId: google.maps.MapTypeId.HYBRID };
map = new google.maps.Map(map[0], options);
location_map = new google.maps.Map(map[0], options);
var initial_position;
if (location_coordinate.val())
if ( coordinate_field.val() ) // Already values in the coordinate field
{
var l = location_coordinate.val().split(/,/);
var l = coordinate_field.val().split(/,/);
if (l.length > 1)
{
initial_position = new google.maps.LatLng(l[0], l[1]);
location_map.setZoom( parseInt(l[2]) );
current_position = new google.maps.LatLng(l[0], l[1]);
map.setZoom( parseInt( l[2] ) );
}
}
else
if ( ! current_position )
{
location_map.setZoom(zoom);
var locationRohr = new google.maps.LatLng( 49.340174,10.890595 );
current_position = locationRohr;
map.setZoom( initZoom );
}
function savePosition( p )
{
point = p;
location_coordinate.val(point.lat().toFixed(6) + "," + point.lng().toFixed(6) + "," + location_map.getZoom() );
}
var marker = new google.maps.Marker({
map: location_map,
position: initial_position,
draggable: true
});
function savePosition( p ) {
current_position = p;
coordinate_field.val( current_position.lat().toFixed(6) + "," + current_position.lng().toFixed(6) + "," + map.getZoom() );
}
var marker = new google.maps.Marker({
map: map,
position: current_position,
draggable: true
});
google.maps.event.addListener(marker, 'dragend', function(mouseEvent) {
savePosition(mouseEvent.latLng);
});
google.maps.event.addListener(location_map, 'click', function(mouseEvent){
google.maps.event.addListener(map, 'click', function(mouseEvent){
marker.setPosition(mouseEvent.latLng);
savePosition(mouseEvent.latLng);
savePosition( mouseEvent.latLng );
});
google.maps.event.addListener(location_map, 'zoom_changed', function(mouseEvent){
savePosition(point);
google.maps.event.addListener(map, 'zoom_changed', function(mouseEvent){
savePosition( current_position );
});
location_based.bindWithDelay("keypress", function() {
var lstr = [];
location_based.each(function(){
var b = $(this);
lstr.push(b.val())
});
base_field.bindWithDelay( "keypress", function() {
geocode(lstr.join(','), function(l){
location_coordinate.val( l.lat()+','+l.lng() );
geocode( base_field.val() , function(l){
coordinate_field.val( l.lat()+','+l.lng() );
});
onLocationCoordinateChange();
}, 2000 );
@@ -79,19 +69,16 @@
{
var latlng = jQuery(this).val().split(/,/);
if (latlng.length < 2) return;
var latlng = new google.maps.LatLng(latlng[0], latlng[1]);
var latlng = new google.maps.LatLng( latlng[0], latlng[1] );
placeMarker( latlng );
location_map.setZoom( latlng[2] );
//geocode_reverse(latlng, function(l){
// location_coordinate.val(l.lat()+','+l.lng());
//});
map.setZoom( latlng[2] );
}
function placeMarker(location) {
marker.setPosition(location);
location_map.setCenter(location);
map.setCenter(location);
savePosition(location);
location_map.panTo(location);
map.panTo(location);
}
function geocode(address, cb) {
@@ -119,21 +106,22 @@
}
}*/
placeMarker(initial_position);
placeMarker( current_position );
}
load();
}
$('input[data-location-widget]').each(function(){
var $el = $(this);
var $map = $($el.attr('data-map')),
$based_fields = $($el.attr('data-based-fields')),
$based_field = $(($el.attr('data-based-field'))),
zoom = parseInt($el.attr('data-zoom'));
location_field_load($map, $based_fields, zoom);
location_field_load( $map, $based_field, zoom );
});
});