Google Map v3 async map


/ Published in: JavaScript
Save to your folder(s)

Init markup <a id=\"gmap\" rel=\"New York\" ></a>


Copy this code and paste it in your HTML
  1. function showMap() {
  2. var geocoder, map,
  3. gmap = document.getElementById('gmap'),
  4. adr = gmap.getAttribute('rel');
  5.  
  6. geocoder = new google.maps.Geocoder();
  7. geocoder.geocode( { 'address': adr}, function(results, status) {
  8. if (status == google.maps.GeocoderStatus.OK) {
  9. map = new google.maps.Map(document.getElementById("gmap"), {
  10. zoom: 16,
  11. center: results[0].geometry.location,
  12. mapTypeId: google.maps.MapTypeId.ROADMAP
  13. });
  14.  
  15. var marker = new google.maps.Marker({
  16. map: map,
  17. title: adr,
  18. position: results[0].geometry.location
  19. });
  20.  
  21. var infowindow = new google.maps.InfoWindow({
  22. content: adr
  23. });
  24.  
  25. google.maps.event.addListener(marker, 'click', function() {
  26. infowindow.open(map,marker);
  27. });
  28.  
  29. } else {
  30. alert("Nepodarilo sa nájsť lokalitu " + adr + ": " + status);
  31. }
  32. });
  33. }
  34.  
  35. function loadMap() {
  36. var script = document.createElement("script");
  37. script.type = "text/javascript";
  38. script.src = "http://maps.google.com/maps/api/js?sensor=false&language=sk&callback=showMap";
  39. document.body.appendChild(script);
  40. }
  41.  
  42. function chMap(adr) {
  43. var gmap = document.getElementById('gmap');
  44.  
  45. gmap.setAttribute('rel', adr);
  46. showMap();
  47. }
  48.  
  49. if (document.getElementById('gmap')) {
  50. window.onload = loadMap;
  51. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.