2013-09-22 11:11:48 +02:00
|
|
|
($ || django.jQuery)(function($){
|
2013-09-27 11:39:28 +02:00
|
|
|
function location_field_load( map, base_field, initZoom )
|
2013-09-22 11:11:48 +02:00
|
|
|
{
|
2013-09-27 11:39:28 +02:00
|
|
|
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);
|
2013-09-22 11:11:48 +02:00
|
|
|
|
2013-09-27 11:39:28 +02:00
|
|
|
if ( coordinate_field.val() ) // Already values in the coordinate field
|
2013-09-22 11:11:48 +02:00
|
|
|
{
|
2013-09-27 11:39:28 +02:00
|
|
|
var l = coordinate_field.val().split(/,/);
|
2013-09-22 11:11:48 +02:00
|
|
|
if (l.length > 1)
|
|
|
|
{
|
2013-09-27 11:39:28 +02:00
|
|
|
current_position = new google.maps.LatLng(l[0], l[1]);
|
|
|
|
map.setZoom( parseInt( l[2] ) );
|
2013-09-22 11:11:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-27 11:39:28 +02:00
|
|
|
if ( ! current_position )
|
2013-09-22 11:11:48 +02:00
|
|
|
{
|
2013-09-27 11:39:28 +02:00
|
|
|
var locationRohr = new google.maps.LatLng( 49.340174,10.890595 );
|
|
|
|
current_position = locationRohr;
|
|
|
|
map.setZoom( initZoom );
|
2013-09-22 11:11:48 +02:00
|
|
|
}
|
2013-09-27 11:39:28 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
});
|
2013-09-22 11:11:48 +02:00
|
|
|
|
|
|
|
google.maps.event.addListener(marker, 'dragend', function(mouseEvent) {
|
|
|
|
savePosition(mouseEvent.latLng);
|
|
|
|
});
|
|
|
|
|
2013-09-27 11:39:28 +02:00
|
|
|
google.maps.event.addListener(map, 'click', function(mouseEvent){
|
2013-09-22 11:11:48 +02:00
|
|
|
marker.setPosition(mouseEvent.latLng);
|
2013-09-27 11:39:28 +02:00
|
|
|
savePosition( mouseEvent.latLng );
|
2013-09-22 11:11:48 +02:00
|
|
|
});
|
|
|
|
|
2013-09-27 11:39:28 +02:00
|
|
|
google.maps.event.addListener(map, 'zoom_changed', function(mouseEvent){
|
|
|
|
savePosition( current_position );
|
2013-09-22 11:11:48 +02:00
|
|
|
});
|
|
|
|
|
2013-09-27 11:39:28 +02:00
|
|
|
base_field.bindWithDelay( "keypress", function() {
|
2013-09-22 11:11:48 +02:00
|
|
|
|
2013-09-27 11:39:28 +02:00
|
|
|
geocode( base_field.val() , function(l){
|
|
|
|
coordinate_field.val( l.lat()+','+l.lng() );
|
2013-09-22 11:11:48 +02:00
|
|
|
});
|
|
|
|
onLocationCoordinateChange();
|
|
|
|
}, 2000 );
|
|
|
|
|
|
|
|
|
|
|
|
function onLocationCoordinateChange()
|
|
|
|
{
|
|
|
|
var latlng = jQuery(this).val().split(/,/);
|
|
|
|
if (latlng.length < 2) return;
|
2013-09-27 11:39:28 +02:00
|
|
|
var latlng = new google.maps.LatLng( latlng[0], latlng[1] );
|
2013-09-22 11:11:48 +02:00
|
|
|
placeMarker( latlng );
|
2013-09-27 11:39:28 +02:00
|
|
|
map.setZoom( latlng[2] );
|
2013-09-22 11:11:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function placeMarker(location) {
|
|
|
|
marker.setPosition(location);
|
2013-09-27 11:39:28 +02:00
|
|
|
map.setCenter(location);
|
2013-09-22 11:11:48 +02:00
|
|
|
savePosition(location);
|
2013-09-27 11:39:28 +02:00
|
|
|
map.panTo(location);
|
2013-09-22 11:11:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function geocode(address, cb) {
|
|
|
|
var result;
|
|
|
|
var geocoder = new google.maps.Geocoder();
|
|
|
|
if (geocoder) {
|
|
|
|
geocoder.geocode({"address": address}, function(results, status) {
|
|
|
|
if (status == google.maps.GeocoderStatus.OK) {
|
|
|
|
cb(results[0].geometry.location);
|
|
|
|
placeMarker(results[0].geometry.location);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
function geocode_reverse(location, cb) {
|
|
|
|
var geocoder = new google.maps.Geocoder();
|
|
|
|
if (geocoder) {
|
|
|
|
geocoder.geocode({"latLng": location}, function(results, status) {
|
|
|
|
if (status == google.maps.GeocoderStatus.OK) {
|
|
|
|
cb(results[0].geometry.location);
|
|
|
|
placeMarker(results[0].geometry.location);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
|
2013-09-27 11:39:28 +02:00
|
|
|
placeMarker( current_position );
|
2013-09-22 11:11:48 +02:00
|
|
|
}
|
2013-09-27 11:39:28 +02:00
|
|
|
|
2013-09-22 11:11:48 +02:00
|
|
|
load();
|
|
|
|
}
|
2013-09-27 11:39:28 +02:00
|
|
|
|
2013-09-22 11:11:48 +02:00
|
|
|
|
|
|
|
$('input[data-location-widget]').each(function(){
|
|
|
|
var $el = $(this);
|
|
|
|
|
|
|
|
var $map = $($el.attr('data-map')),
|
2013-09-27 11:39:28 +02:00
|
|
|
$based_field = $(($el.attr('data-based-field'))),
|
2013-09-22 11:11:48 +02:00
|
|
|
zoom = parseInt($el.attr('data-zoom'));
|
|
|
|
|
2013-09-27 11:39:28 +02:00
|
|
|
location_field_load( $map, $based_field, zoom );
|
2013-09-22 11:11:48 +02:00
|
|
|
});
|
2013-09-27 11:39:28 +02:00
|
|
|
|
|
|
|
|
2013-09-22 11:11:48 +02:00
|
|
|
});
|