Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. <script>
  2. // Initialize/create variables
  3. var startLatLong = { lat: 53.483959, lng: -2.244644 };
  4. var latField = document.getElementById('latitude');
  5. var lngField = document.getElementById('longitude');
  6. var searchField = document.getElementById('ApproximateLocation');
  7. var searchSubmitButton = document.getElementById('submitSearch');
  8. var service;
  9. var searchRequest;
  10. var map;
  11. var marker;
  12. var mapService;
  13.  
  14. // Map Initialization (Callback from Google Map API)
  15. function initMap() {
  16. map = new google.maps.Map(document.getElementById('map'), {
  17. center: startLatLong,
  18. zoom: 6
  19. });
  20.  
  21. marker = new google.maps.Marker({
  22. draggable: true,
  23. animation: google.maps.Animation.Po,
  24. position: startLatLong
  25. });
  26.  
  27. addListeners();
  28. initiatePlaces();
  29. }
  30.  
  31. function addListeners(){
  32. // Updates the marker when the map is clicked
  33. map.addListener('click', function (event) {
  34. marker.setMap(map);
  35. marker.setPosition(event.latLng);
  36. marker.setAnimation(google.maps.Animation.Po);
  37.  
  38. updateLatLng();
  39. });
  40.  
  41. // Updates the latlong when the marker is dragged
  42. marker.addListener('dragend', function (event) {
  43. updateLatLng();
  44. });
  45.  
  46. // Update map from text search
  47. searchSubmitButton.onclick = function () {
  48. doSearch();
  49. };
  50. }
  51.  
  52. // Updates the map using search terms
  53. function doSearch() {
  54. request = {
  55. query: searchField.value
  56. }
  57. service.textSearch(request, callback);
  58. }
  59.  
  60. // Updates the latlong with the current marker
  61. function updateLatLng() {
  62. latField.value = marker.getPosition().lat();
  63. lngField.value = marker.getPosition().lng();
  64. }
  65.  
  66. // Intializes the 'Places' API
  67. function initiatePlaces() {
  68. service = new google.maps.places.PlacesService(map);
  69. }
  70.  
  71. // A callback function to process the search results
  72. function callback(results, status) {
  73. if (status == google.maps.places.PlacesServiceStatus.OK) {
  74. // Here, the first search result is used, and a zoom level is set
  75.  
  76. var place = results[0];
  77. map.panTo(place.geometry.location);
  78. map.setZoom(8);
  79. }
  80. }
  81. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement