Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. <script>
  2. // Note: This example requires that you consent to location sharing when
  3. // prompted by your browser. If you see the error "The Geolocation service
  4. // failed.", it means you probably did not give permission for the browser to
  5. // locate you.
  6. var customLabel = {
  7. restaurant: {
  8. label: 'R'
  9. },
  10. bar: {
  11. label: 'B'
  12. }
  13. };
  14. function initMap() {
  15. var map = new google.maps.Map(document.getElementById('map'), {
  16. center: new google.maps.LatLng(-23.568130, -46.649166),
  17. zoom: 13, scrollwheel: !1
  18.  
  19. });
  20.  
  21. var infoWindow = new google.maps.InfoWindow;
  22.  
  23. downloadUrl('resultado.php', function (data) {
  24. var xml = data.responseXML;
  25. var markers = xml.documentElement.getElementsByTagName('marker');
  26. Array.prototype.forEach.call(markers, function (markerElem) {
  27. var name = markerElem.getAttribute('name');
  28. var address = markerElem.getAttribute('address');
  29. var type = markerElem.getAttribute('type');
  30. var point = new google.maps.LatLng(
  31. parseFloat(markerElem.getAttribute('lat')),
  32. parseFloat(markerElem.getAttribute('lng')));
  33. var infowincontent = document.createElement('div');
  34. var strong = document.createElement('strong');
  35. strong.textContent = name;
  36. infowincontent.appendChild(strong);
  37. infowincontent.appendChild(document.createElement('br'));
  38. var text = document.createElement('text');
  39. text.textContent = address;
  40. infowincontent.appendChild(text);
  41. var icon = customLabel[type] || {
  42. };
  43. var marker = new google.maps.Marker({
  44. position: point,
  45. label: icon.label,
  46. icon: 'pointer.png',
  47. map: map
  48. });
  49. marker.addListener('click', function () {
  50. infoWindow.setContent(infowincontent);
  51. infoWindow.open(map, marker);
  52. });
  53. });
  54. });
  55. function downloadUrl(url, callback) {
  56. var request = window.ActiveXObject ?
  57. new ActiveXObject('Microsoft.XMLHTTP') :
  58. new XMLHttpRequest;
  59. request.onreadystatechange = function () {
  60. if (request.readyState == 4) {
  61. request.onreadystatechange = doNothing;
  62. callback(request, request.status);
  63. }
  64. };
  65. request.open('GET', url, true);
  66. request.send(null);
  67. }
  68. function doNothing() {}
  69. var infoWindow = new google.maps.InfoWindow({map: map});
  70. if (navigator.geolocation) {
  71. navigator.geolocation.getCurrentPosition(function (position) {
  72. var pos = {
  73. lat: position.coords.latitude,
  74. lng: position.coords.longitude
  75. };
  76.  
  77. infoWindow.setPosition(pos);
  78. infoWindow.setContent('Você está aqui');
  79. map.setCenter(pos);
  80. }, function () {
  81. handleLocationError(true, infoWindow, map.getCenter());
  82. });
  83. } else {
  84. handleLocationError(false, infoWindow, map.getCenter());
  85. }
  86. }
  87. function handleLocationError(browserHasGeolocation, infoWindow, pos) {
  88. infoWindow.setPosition(pos);
  89. infoWindow.setContent(browserHasGeolocation ?
  90. 'Error: The Geolocation service failed.' :
  91. 'Error: Your browser doesn\'t support geolocation.');
  92. }
  93. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement