Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script>
- //Map
- $(function(){
- var map;
- var markers = [];
- function initialize() {
- var mapOptions = {
- zoom: 10,
- center: {lat: 55.741744, lng: 37.640715}
- }
- map = new google.maps.Map(document.getElementById('form-map'), mapOptions);
- var infowindow = new google.maps.InfoWindow;
- var geocoder = new google.maps.Geocoder;
- google.maps.event.addListener(map, 'click', function(event) {
- geocodeLatLng(event.latLng, geocoder, map, infowindow);
- $('input[name="coords"]').val(event.latLng.lat() + ',' + event.latLng.lng());
- });
- }
- google.maps.event.addDomListener(window, 'load', initialize);
- function setMapOnAll(map) {
- for (var i = 0; i < markers.length; i++) {
- markers[i].setMap(map);
- }
- }
- function geocodeLatLng(latlng, geocoder, map, infowindow) {
- geocoder.geocode({'location': latlng}, function(results, status) {
- setMapOnAll(null);
- if (status === google.maps.GeocoderStatus.OK) {
- if (results[1]) {
- var marker = new google.maps.Marker({
- position: latlng,
- map: map
- });
- markers.push(marker);
- infowindow.setContent(results[1].formatted_address);
- $('input[name="address"]').val(results[1].formatted_address);
- infowindow.open(map, marker);
- } else {
- window.alert('No results found');
- }
- } else {
- window.alert('Geocoder failed due to: ' + status);
- }
- });
- }
- function codeAddress(address) {
- var geocoder = new google.maps.Geocoder;
- setMapOnAll(null);
- geocoder.geocode( { 'address': address}, function(results, status) {
- if (status == google.maps.GeocoderStatus.OK) {
- map.setCenter(results[0].geometry.location);
- var marker = new google.maps.Marker({
- map: map,
- position: results[0].geometry.location
- });
- $('input[name="coords"]').val(results[0].geometry.location.lat() + ',' + results[0].geometry.location.lng());
- markers.push(marker);
- }
- });
- }
- $('.address-search').click(function(){
- var val = $('input[name="address"]').val();
- codeAddress(val);
- return false;
- });
- })
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement