Skip to content

Instantly share code, notes, and snippets.

@carmichaelize
Created July 10, 2014 14:26
Show Gist options
  • Save carmichaelize/c3a0542f159bcc963ce3 to your computer and use it in GitHub Desktop.
Save carmichaelize/c3a0542f159bcc963ce3 to your computer and use it in GitHub Desktop.
Google Maps API Framework
var ui = {
buildMarkers: function( data, first ){
var results = [];
$.each( data, function(i, marker){
var html = ui.buildHTML( marker.title, marker.address, marker.telephone );
if(i == 0){
results.push({
lat: marker.lat,
lng: marker.lng ,
html: html,
popup:true
});
} else {
results.push({
lat: marker.lat,
lng: marker.lng,
html: html
});
}
});
//Switch item to first
if( first ){
var temp = results[first];
results.splice(first, 1);[first];
results.unshift(temp);
}
return results;
},
buildHTML: function( title, address, telephone ){
var string = "";
string += '<strong>' + title + '</strong><br />';
string += '<em style="font-size:0.9em;">';
string += address;
string += '<br />Tel: ' + telephone;
string += '</em>';
return string;
},
buildMap: function( markers, zoom, locationSet ){
$map = null;
if(!markers.length) return false;
//Set center and zoom
options.center = new google.maps.LatLng( markers[0].lat, markers[0].lng);
options.zoom = zoom;
//Build map
$map = new google.maps.Map( document.getElementById('sc-location-map'), options )
$.each( markers, function(i, marker){
//Add Markers
markers[i] = new google.maps.Marker({
position: new google.maps.LatLng(marker.lat, marker.lng),
map: $map
});
//Add Info Windows
infoWindows[i] = new google.maps.InfoWindow({
content: marker.html
});
//Add Mrker Events
markers[i].addListener('click', function(){
infoWindows[i].open( $map, markers[i] );
});
if( i == 0 && locationSet ){
infoWindows[i].open( $map, markers[i] );
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment