forked from ryancramerdesign/FieldtypeMapMarker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputfieldMapMarker.js
129 lines (109 loc) · 4.01 KB
/
InputfieldMapMarker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* Display a Google Map and pinpoint a location for InputfieldMapMarker
*
*/
var InputfieldMapMarker = {
options: {
zoom: 12, // mats, previously 5
draggable: true, // +mats
center: null,
mapTypeId: google.maps.MapTypeId.HYBRID,
scrollwheel: false,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
},
scaleControl: false
},
init: function(mapId, lat, lng, zoom, mapType) {
var options = InputfieldMapMarker.options;
if(zoom < 1) zoom = 12;
options.center = new google.maps.LatLng(lat, lng);
options.zoom = parseInt(zoom);
if(mapType == 'SATELLITE') options.mapTypeId = google.maps.MapTypeId.SATELLITE;
else if(mapType == 'ROADMAP') options.mapTypeId = google.maps.MapTypeId.ROADMAP;
var map = new google.maps.Map(document.getElementById(mapId), options);
var marker = new google.maps.Marker({
position: options.center,
map: map,
draggable: options.draggable
});
var $map = $('#' + mapId);
var $lat = $map.siblings(".InputfieldMapMarkerLat").find("input[type=text]");
var $lng = $map.siblings(".InputfieldMapMarkerLng").find("input[type=text]");
var $addr = $map.siblings(".InputfieldMapMarkerAddress").find("input[type=text]");
var $addrJS = $map.siblings(".InputfieldMapMarkerAddress").find("input[type=hidden]");
var $toggle = $map.siblings(".InputfieldMapMarkerToggle").find("input[type=checkbox]");
var $zoom = $map.siblings(".InputfieldMapMarkerZoom").find("input[type=number]");
var $notes = $map.siblings(".notes");
$lat.val(marker.getPosition().lat());
$lng.val(marker.getPosition().lng());
$zoom.val(map.getZoom());
google.maps.event.addListener(marker, 'dragend', function(event) {
var geocoder = new google.maps.Geocoder();
var position = this.getPosition();
$lat.val(position.lat());
$lng.val(position.lng());
if($toggle.is(":checked")) {
geocoder.geocode({ 'latLng': position }, function(results, status) {
if(status == google.maps.GeocoderStatus.OK && results[0]) {
$addr.val(results[0].formatted_address);
$addrJS.val($addr.val());
}
$notes.text(status);
});
}
});
google.maps.event.addListener(map, 'zoom_changed', function() {
$zoom.val(map.getZoom());
});
$addr.blur(function() {
if(!$toggle.is(":checked")) return true;
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': $(this).val()}, function(results, status) {
if(status == google.maps.GeocoderStatus.OK && results[0]) {
var position = results[0].geometry.location;
map.setCenter(position);
marker.setPosition(position);
$lat.val(position.lat());
$lng.val(position.lng());
$addrJS.val($addr.val());
}
$notes.text(status);
});
return true;
});
$zoom.change(function() {
map.setZoom(parseInt($(this).val()));
});
$toggle.click(function() {
if($(this).is(":checked")) {
$notes.text('Geocode ON');
// google.maps.event.trigger(marker, 'dragend');
$addr.trigger('blur');
} else {
$notes.text('Geocode OFF');
}
return true;
});
// added by diogo to solve the problem of maps not rendering correctly in hidden elements
// trigger a resize on the map when either the tab button or the toggle field bar are pressed
// get the tab element where this map is integrated
var $map = $('#' + mapId);
var $tab = $('#_' + $map.closest('.InputfieldFieldsetTabOpen').attr('id'));
// get the inputfield where this map is integrated and add the tab to the stack
var $inputFields = $map.closest('.Inputfield').find('.InputfieldStateToggle').add($tab);
$inputFields.on('click',function(){
// give it time to open
window.setTimeout(function(){
google.maps.event.trigger(map,'resize');
map.setCenter(options.center);
}, 200);
});
}
};
$(document).ready(function() {
$(".InputfieldMapMarkerMap").each(function() {
var $t = $(this);
InputfieldMapMarker.init($t.attr('id'), $t.attr('data-lat'), $t.attr('data-lng'), $t.attr('data-zoom'), $t.attr('data-type'));
});
});