Created
July 2, 2014 09:53
-
-
Save josetapadas/e5ce791bb5c87d7039f7 to your computer and use it in GitHub Desktop.
Simple Google Maps API JavaScript implementation
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 google_map = {} | |
window.googleMapInitialize = function() { | |
var $map_address = $('#map-address'); | |
var map_options = { | |
zoom: 17, | |
}; | |
google_map = { | |
map_element: new google.maps.Map(document.getElementById('map-canvas'), map_options), | |
geocoder: new google.maps.Geocoder(), | |
marker: new google.maps.Marker(), | |
default_location: new google.maps.LatLng(39.57983841844286, -7.9527174624999875) | |
} | |
google.maps.event.addListener(google_map.marker, 'position_changed', function() { | |
var dragged_location = google_map.marker.getPosition(); | |
fieldValue('map_lat', dragged_location.lat()); | |
fieldValue('map_lng', dragged_location.lng()); | |
}); | |
google.maps.event.addListener(google_map.map_element, 'zoom_changed', function() { | |
var new_zoom_value = google_map.map_element.getZoom(); | |
fieldValue('map_zoom', new_zoom_value); | |
}); | |
if ($map_address.length && $map_address.val()) { | |
googleGeocodeAddress($map_address.val()); | |
} else { | |
googleMapSetMarker(); | |
} | |
createMapHiddenInputs(); | |
fieldValue('map_zoom', map_options.zoom); | |
} | |
function createMapHiddenInputs() { | |
['map_lat', 'map_lng', 'map_zoom'].forEach(function(current_input) { | |
addHiddenInput(current_input, ''); | |
}); | |
} | |
function googleMapSetMarker(options) { | |
if (googleMapIsNotLoaded()) { | |
return; | |
} | |
function googleMapBounce() { | |
if (google_map.marker.getAnimation() != null) { | |
google_map.marker.setAnimation(null); | |
} else { | |
google_map.marker.setAnimation(google.maps.Animation.BOUNCE); | |
} | |
} | |
// clear the marker because we only want one | |
if (google_map.marker.getMap()) { | |
google_map.marker.setMap(null); | |
} | |
var default_options = { | |
//icon: 'http://i.snag.gy/T9oqh.jpg', | |
location: google_map.default_location | |
} | |
var actual_options = $.extend({}, default_options, options); | |
//google_map.marker.setIcon(actual_options.icon); | |
google_map.marker.setPosition(actual_options.location); | |
google_map.marker.setDraggable(true); | |
google_map.map_element.setCenter(actual_options.location); | |
google_map.marker.setMap(google_map.map_element); | |
// for the lulz | |
// googleMapBounce(); | |
} | |
function googleMapIsNotLoaded() { | |
return (google_map.map_element === undefined && google_map.geocoder === undefined && google_map.marker === undefined) | |
} | |
function googleMapGetMap() { | |
if (googleMapIsNotLoaded()) { | |
$.getScript('https://maps.googleapis.com/maps/api/js?v=3.exp&key=LOLOL&sensor=false&callback=googleMapInitialize'); | |
} | |
} | |
function googleGeocodeAddress(address_string) { | |
if (!address_string) { | |
return; | |
} | |
if (googleMapIsNotLoaded()) { | |
googleMapGetMap(); | |
toggleSuchMapControls(); | |
return; | |
} | |
google_map.geocoder.geocode({ | |
'address': address_string | |
}, function(results, status) { | |
if (status == google.maps.GeocoderStatus.OK) { | |
googleMapSetMarker({ | |
location: results[0].geometry.location | |
}); | |
} else { | |
console.log("googleGeocodeAddress: unable to retrieve location."); | |
googleMapSetMarker(); | |
} | |
}); | |
} | |
function toggleSuchMapControls() { | |
var $map_canvas = $('#map-canvas:visible'); | |
if ($map_canvas.length) { | |
$('.map-togglable').addClass('hidden'); | |
} else { | |
$('.map-togglable').removeClass('hidden'); | |
} | |
} | |
$("#check_address").live('click', function() { | |
if (googleMapIsNotLoaded()) { | |
googleMapGetMap(); | |
toggleSuchMapControls(); | |
return false; | |
} | |
var $map_address = $('#map-address'); | |
if ($map_address.length && $map_address.val()) { | |
googleGeocodeAddress($map_address.val()); | |
if ($('#map-canvas:visible').length == 0) { | |
toggleSuchMapControls(); | |
} | |
} | |
return false; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment