Google Maps Javascript

// Prevents form submission on Enter var preventSubmitOnEnter = function(inputId) { $(inputId).keydown(function(event){ if(event.which == 13) { event.preventDefault(); return false; } }); }; // Resets the input on click var resetInputOnClick = function(inputId) { $(inputId).on("click", function() { $(this).val(""); }); }; // Adds a marker to the map var addMarker = function(latLng, map) { return new google.maps.Marker({ position: latLng, map: map }); }; // Remove markers in an array var removeMarkers = function(markersArray) { for (var i = 0; i < markersArray.length; i++) { google.maps.event.clearInstanceListeners(markersArray[i]); markersArray[i].setMap(null); } markersArray.length = 0; }; // Gets the latitude and longitude for a place var findLatLng = function(place) { return new google.maps.LatLng(place.geometry.location.lat(), place.geometry.location.lng()); }; // Move map to a place var centerMap = function(latLng, map) { map.setCenter(latLng); map.setZoom(14); }; var placeDefined = false; var search_field = function() { var autocomplete, input; input = document.getElementById('q'); autocomplete = new google.maps.places.Autocomplete(input); google.maps.event.addListener(autocomplete, 'place_changed', function(event) { var place; place = autocomplete.getPlace(); if (place.address_components !== null) { $("#lat").val(place.geometry.location.lat()); $("#lng").val(place.geometry.location.lng()); return placeDefined = true; } else { $("#lat").removeAttr("value"); $("#lng").removeAttr("value"); return placeDefined = false; } }); return $(input).keydown(function(event) { if (event.which === 13) { if (placeDefined) { return true; } else { return false; } } else { placeDefined = false; return true; } }); };
Useful functions for implementing maps and autocomplete fields.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.