-
Notifications
You must be signed in to change notification settings - Fork 17
/
mapmarker.jquery.js
79 lines (67 loc) · 2.05 KB
/
mapmarker.jquery.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
(function($){
$.fn.mapmarker = function(options){
var opts = $.extend({}, $.fn.mapmarker.defaults, options);
return this.each(function() {
// Apply plugin functionality to each element
var map_element = this;
addMapMarker(map_element, opts.zoom, opts.center, opts.markers);
});
};
// Set up default values
var defaultMarkers = {
"markers": []
};
$.fn.mapmarker.defaults = {
zoom : 8,
center : 'United States',
markers : defaultMarkers
}
// Main function code here (ref:google map api v3)
function addMapMarker(map_element, zoom, center, markers){
//console.log($.fn.mapmarker.defaults['center']);
//Set center of the Map
var myOptions = {
zoom: zoom,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(map_element, myOptions);
var geocoder = new google.maps.Geocoder();
var infowindow = null;
var baloon_text = "";
geocoder.geocode( { 'address': center}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
//In this case it creates a marker, but you can get the lat and lng from the location.LatLng
map.setCenter(results[0].geometry.location);
}
else{
console.log("Geocode was not successful for the following reason: " + status);
}
});
//run the marker JSON loop here
$.each(markers.markers, function(i, the_marker){
latitude=the_marker.latitude;
longitude=the_marker.longitude;
icon=the_marker.icon;
var baloon_text=the_marker.baloon_text;
if(latitude!="" && longitude!=""){
var marker = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(latitude,longitude),
animation: google.maps.Animation.DROP,
icon: icon
});
// Set up markers with info windows
google.maps.event.addListener(marker, 'click', function() {
// Close all open infowindows
if (infowindow) {
infowindow.close();
}
infowindow = new google.maps.InfoWindow({
content: baloon_text
});
infowindow.open(map,marker);
});
}
});
}
})(jQuery);