We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

damarev on 07/04/07


Tagged


Versions (?)


Who likes this?

3 people have marked this snippet as a favorite

alvaroisorna
neuroasis
blackabee


Google Maps API


Published in: JavaScript 


  1. // Check to see if this browser can run the Google API
  2. if (GBrowserIsCompatible()) {
  3.  
  4. var map;
  5.  
  6. var gmarkers = [];
  7. var htmls = [];
  8. var to_htmls = [];
  9. var from_htmls = [];
  10. var i=0;
  11.  
  12. // A function to create the marker and set up the event window
  13. function createMarker(point,name,html) {
  14. var marker = new GMarker(point);
  15.  
  16. // The info window version with the "to here" form open
  17. to_htmls[i] = html + '<br>Directions: <b>To here</b> - <a href="javascript:fromhere(' + i + ')">From here</a>' +
  18. '<br>Start address:<form action="http://maps.google.com/es" method="get" target="_blank">' +
  19. '<input type="text" SIZE=20 MAXLENGTH=60 name="saddr" id="saddr" value="" />' +
  20. '<INPUT value="GO" TYPE="SUBMIT">' +
  21. '<input type="hidden" name="daddr" value="' + name + '" /><br>';
  22. // The info window version with the "to here" form open
  23. from_htmls[i] = html + '<br>Directions: <a href="javascript:tohere(' + i + ')">To here</a> - <b>From here</b>' +
  24. '<br>End address:<form action="http://maps.google.es/maps" method="get"" target="_blank">' +
  25. '<input type="text" SIZE=20 MAXLENGTH=60 name="daddr" id="daddr" value="" />' +
  26. '<INPUT value="Get Directions" TYPE="SUBMIT">' +
  27. '<input type="hidden" name="saddr" value="' + name + '" /><br>';
  28. // The inactive version of the direction info
  29. html = html + '<br>Directions: <a href="javascript:tohere('+i+')">To here</a> - <a href="javascript:fromhere('+i+')">From here</a>';
  30.  
  31. GEvent.addListener(marker, "click", function() {
  32. marker.openInfoWindowHtml(html);
  33. });
  34. gmarkers[i] = marker;
  35. htmls[i] = html;
  36. i++;
  37. return marker;
  38. }
  39.  
  40. // functions that open the directions forms
  41. function tohere(i) {
  42. gmarkers[i].openInfoWindowHtml(to_htmls[i]);
  43. }
  44. function fromhere(i) {
  45. gmarkers[i].openInfoWindowHtml(from_htmls[i]);
  46. }
  47.  
  48.  
  49. function initGMap() {
  50. // Display the map, with some controls and set the initial location
  51. map = new GMap2(document.getElementById("gmap"));
  52. map.addControl(new GLargeMapControl());
  53. map.addControl(new GMapTypeControl(1));
  54.  
  55. var MyPoint = new GLatLng( 41.6648, -91.3095);
  56. map.setCenter(MyPoint, 6);
  57.  
  58. var marker = createMarker(MyPoint,'1951 Delta Ave, West Branch, IA','1951 Delta Ave<br l>West Branch, IA 52358');
  59. map.addOverlay(marker);
  60.  
  61.  
  62. }
  63.  
  64. $(initGMap);
  65.  
  66.  
  67. }
  68.  
  69.  
  70. else {
  71. //alert("Sorry, the Google Maps API is not compatible with this browser");
  72. }
  73.  
  74. // This Javascript is based on code provided by the
  75. // Blackpool Community Church Javascript Team
  76. // http://www.commchurch.freeserve.co.uk/
  77. // http://www.econym.demon.co.uk/googlemaps/
  78.  
  79. $(document).onunload = GUnload;

Report this snippet 

You need to login to post a comment.