Created
March 24, 2014 11:48
-
-
Save roden0/9738749 to your computer and use it in GitHub Desktop.
GMaps Singleton object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var oldMap = { | |
| map : null, | |
| center : {lat:50, lng:20, zoom:3}, | |
| drawn : false, | |
| data : {}, | |
| divId : "oldMap", | |
| url : "php/getMapData.php", | |
| infowindow : new google.maps.InfoWindow({ | |
| size: new google.maps.Size(150,50) | |
| }), | |
| init : function () { | |
| if (!this.drawn){ | |
| $.getJSON( | |
| this.url, | |
| function(d){ | |
| if(d){ | |
| $.extend(oldMap.data,d); | |
| var latlng = new google.maps.LatLng(oldMap.center.lat, oldMap.center.lng); | |
| var myOptions = { | |
| zoom: oldMap.center.zoom, | |
| center: latlng, | |
| mapTypeId: google.maps.MapTypeId.TERRAIN | |
| }; | |
| // create the map | |
| map = new google.maps.Map(document.getElementById("oldMap"),myOptions); | |
| // create the legend | |
| var legendDiv = document.createElement('DIV'); | |
| legendDiv.innerHTML = '<div id="legend"><img src="images/markers/legend-blur.png"></div>'; | |
| map.controls[google.maps.ControlPosition.TOP_LEFT].push(legendDiv); | |
| google.maps.event.addListener(map, 'click', function() { | |
| infowindow.close(); | |
| }); | |
| // Set the info window html template | |
| var infoWindowTemplate = "<div id='balloon'>{{#url2}}<img src='{{url2}}' />{{/url2}}<h2>{{project_name}}</h2><p><b>Amount</b> € {{cost}}</p><p><b>Country</b> {{country}}</p><p><b>Year</b> {{year}}</p><p><b>Project Type</b> {{project_type}}</p><p>{{description}}</p>{{#url}}<p><a target='_blank' href='{{url}}'>More info</a></p>{{/url}}</div>" | |
| // loop through the projects | |
| for(var m in oldMap.data) { | |
| // if the project has a marker type defined | |
| if (oldMap.data[m].marker) { | |
| // point | |
| var point = new google.maps.LatLng(oldMap.data[m].lat, oldMap.data[m].longtd); | |
| // create HTML for info window | |
| var infoHtml = Mustache.to_html(infoWindowTemplate, oldMap.data[m]); | |
| // icon | |
| var icon = new google.maps.MarkerImage( | |
| "images/markers/33px/" + oldMap.data[m].marker + ".png", | |
| new google.maps.Size(33,33) | |
| ); | |
| // create a marker for this project | |
| var marker = oldMap.createMarker(point,infoHtml,icon); | |
| } | |
| } | |
| oldMap.drawn = true; | |
| } | |
| } | |
| ) | |
| } | |
| }, | |
| createMarker : function (latlng, html, icon) { | |
| // create the marker | |
| var marker = new google.maps.Marker({ | |
| position: latlng, | |
| icon: icon, | |
| map: map, | |
| zIndex: Math.round(latlng.lat()*-100000)<<5 | |
| }); | |
| // open info window when marker clicked | |
| google.maps.event.addListener(marker, 'click', function() { | |
| oldMap.infowindow.setContent(html); | |
| oldMap.infowindow.open(map,marker); | |
| }); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment