Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. <script>
  2. //Map
  3. $(function(){
  4. var map;
  5. var markers = [];
  6. function initialize() {
  7. var mapOptions = {
  8. zoom: 10,
  9. center: {lat: 55.741744, lng: 37.640715}
  10. }
  11. map = new google.maps.Map(document.getElementById('form-map'), mapOptions);
  12. var infowindow = new google.maps.InfoWindow;
  13. var geocoder = new google.maps.Geocoder;
  14. google.maps.event.addListener(map, 'click', function(event) {
  15. geocodeLatLng(event.latLng, geocoder, map, infowindow);
  16. $('input[name="coords"]').val(event.latLng.lat() + ',' + event.latLng.lng());
  17. });
  18. }
  19.  
  20. google.maps.event.addDomListener(window, 'load', initialize);
  21. function setMapOnAll(map) {
  22. for (var i = 0; i < markers.length; i++) {
  23. markers[i].setMap(map);
  24. }
  25. }
  26. function geocodeLatLng(latlng, geocoder, map, infowindow) {
  27. geocoder.geocode({'location': latlng}, function(results, status) {
  28. setMapOnAll(null);
  29. if (status === google.maps.GeocoderStatus.OK) {
  30. if (results[1]) {
  31. var marker = new google.maps.Marker({
  32. position: latlng,
  33. map: map
  34. });
  35. markers.push(marker);
  36. infowindow.setContent(results[1].formatted_address);
  37. $('input[name="address"]').val(results[1].formatted_address);
  38. infowindow.open(map, marker);
  39. } else {
  40. window.alert('No results found');
  41. }
  42. } else {
  43. window.alert('Geocoder failed due to: ' + status);
  44. }
  45. });
  46. }
  47.  
  48. function codeAddress(address) {
  49. var geocoder = new google.maps.Geocoder;
  50. setMapOnAll(null);
  51. geocoder.geocode( { 'address': address}, function(results, status) {
  52. if (status == google.maps.GeocoderStatus.OK) {
  53. map.setCenter(results[0].geometry.location);
  54. var marker = new google.maps.Marker({
  55. map: map,
  56. position: results[0].geometry.location
  57. });
  58. $('input[name="coords"]').val(results[0].geometry.location.lat() + ',' + results[0].geometry.location.lng());
  59. markers.push(marker);
  60. }
  61. });
  62. }
  63.  
  64. $('.address-search').click(function(){
  65. var val = $('input[name="address"]').val();
  66. codeAddress(val);
  67. return false;
  68. });
  69. })
  70. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement