blechreiz-website/location_field/media/form.js

140 lines
4.5 KiB
JavaScript

($ || django.jQuery)(function($){
function location_field_load(map, location_based, zoom)
{
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
};
location_map = new google.maps.Map(map[0], options);
var initial_position;
if (location_coordinate.val())
{
var l = location_coordinate.val().split(/,/);
if (l.length > 1)
{
initial_position = new google.maps.LatLng(l[0], l[1]);
location_map.setZoom( parseInt(l[2]) );
}
}
else
{
location_map.setZoom(zoom);
}
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
});
google.maps.event.addListener(marker, 'dragend', function(mouseEvent) {
savePosition(mouseEvent.latLng);
});
google.maps.event.addListener(location_map, 'click', function(mouseEvent){
marker.setPosition(mouseEvent.latLng);
savePosition(mouseEvent.latLng);
});
google.maps.event.addListener(location_map, 'zoom_changed', function(mouseEvent){
savePosition(point);
});
location_based.bindWithDelay("keypress", function() {
var lstr = [];
location_based.each(function(){
var b = $(this);
lstr.push(b.val())
});
geocode(lstr.join(','), function(l){
location_coordinate.val( l.lat()+','+l.lng() );
});
onLocationCoordinateChange();
}, 2000 );
function onLocationCoordinateChange()
{
var latlng = jQuery(this).val().split(/,/);
if (latlng.length < 2) return;
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());
//});
}
function placeMarker(location) {
marker.setPosition(location);
location_map.setCenter(location);
savePosition(location);
location_map.panTo(location);
}
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);
}
});
}
}*/
placeMarker(initial_position);
}
load();
}
$('input[data-location-widget]').each(function(){
var $el = $(this);
var $map = $($el.attr('data-map')),
$based_fields = $($el.attr('data-based-fields')),
zoom = parseInt($el.attr('data-zoom'));
location_field_load($map, $based_fields, zoom);
});
});