PageRenderTime 14ms CodeModel.GetById 2ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/mod_admin/lib/js/admin_edit_location.js

https://code.google.com/p/zotonic/
JavaScript | 84 lines | 67 code | 17 blank | 0 comment | 5 complexity | 59026bd51c91142f5c87c4a5eeedf90e MD5 | raw file
 1$(document).ready(function() {
 2	if($('#map_canvas').length) {
 3		googleMapsControl.buildMap({lat: 70.0, lng: -50.0, mapId: 'map_canvas', zoom: 2});
 4
 5		window.marker = false;
 6		var map = googleMapsControl.getMap();
 7
 8        if ($("#location_lat").val() && $("#location_lon").val())
 9        {
10			addMarker(new google.maps.LatLng($("#location_lat").val(), $("#location_lon").val()));
11			setTimeout(function(){map.setCenter(window.marker.getPosition());},100);
12        }
13
14		$('#fill-geo').click(function() {
15			googleMapsControl.getGeoForAddress($('.field-title:first').val(), function(data) {
16				if(window.marker) {
17					removeMarker(window.marker);
18				}
19
20				addMarker(data.geometry.location);
21				fillLatLng(data.geometry.location);
22
23				google.maps.event.addListener(window.marker, 'dragend', function(e) {
24					fillLatLng(window.marker.getPosition());
25					map.setCenter(window.marker.getPosition());
26				});
27
28				google.maps.event.addListener(window.marker, 'drag', function(e) {
29					fillLatLng(window.marker.getPosition());
30				});
31			});
32		});
33
34		google.maps.event.addListener(map, 'click', function(e)	{
35			if(window.marker) {
36				removeMarker(window.marker);
37			}
38
39			addMarker(e.latLng);
40
41			google.maps.event.addListener(window.marker, 'click', function(e) {
42				fillLatLng(window.marker.getPosition());
43				map.setCenter(window.marker.getPosition());
44			});
45
46			clickMarker(window.marker);
47
48			google.maps.event.addListener(window.marker, 'dragend', function(e) {
49				fillLatLng(window.marker.getPosition())
50				map.setCenter(window.marker.getPosition());
51			});
52
53			google.maps.event.addListener(window.marker, 'drag', function(e) {
54				fillLatLng(window.marker.getPosition());
55			});
56		});
57	}
58
59	function fillLatLng(pos) {
60		$('#location_lat').val(pos.lat());
61		$('#location_lng').val(pos.lng());
62	}
63
64	function addMarker(position) {
65		window.marker = new google.maps.Marker({
66			position:	position,
67			map: 		map,
68			icon:		'/lib/images/map_icon.png',
69			flat: 		false,
70			draggable:  true
71		});
72
73		google.maps.event.trigger(window.marker, "click");
74		map.setCenter(window.marker.getPosition());
75	}
76
77	function removeMarker(marker) {
78		marker.setMap(null);
79	}
80
81	function clickMarker(marker) {
82		google.maps.event.trigger(marker, "click");
83	}
84});