ITeye博客

google map 示例

fly.net.cn · 2007-08-11 15:19 · google api · HTML · Google · XML · Microsoft · JavaScript · Markdown

代码

  1. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
  3. <head>
  4. <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
  5. <title>Google Maps JavaScript API Exampletitle>
  6. <script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAALZrWbwv15xcW6o6Rz2I9OBT8BhQlyUVBQhI7yb8dqdkP7sHdrRTkgtKBUMAsHSYcx-k3lrwvWHbe9w"
  7. type="text/javascript">script>
  8. <script type="text/javascript">
  9. function load() {
  10. if (GBrowserIsCompatible()) {
  11. // Create a base icon for all of our markers that specifies the shadow, icon
  12. // dimensions, etc.
  13. var baseIcon = new GIcon();
  14. baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
  15. baseIcon.iconSize = new GSize(20, 34);
  16. baseIcon.shadowSize = new GSize(37, 34);
  17. baseIcon.iconAnchor = new GPoint(9, 34);
  18. baseIcon.infoWindowAnchor = new GPoint(9, 2);
  19. baseIcon.infoShadowAnchor = new GPoint(18, 25);
  20. // Create our "tiny" marker icon
  21. var icon = new GIcon();
  22. icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
  23. icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
  24. icon.iconSize = new GSize(12, 20);
  25. icon.shadowSize = new GSize(22, 20);
  26. icon.iconAnchor = new GPoint(6, 20);
  27. icon.infoWindowAnchor = new GPoint(5, 1);
  28. // Center the map on Palo Alto
  29. var map = new GMap(document.getElementById("map"));
  30. //  map.addControl(new GSmallMapControl());
  31. map.addControl(new GLargeMapControl());
  32. //map.addControl(new GSmallZoomControl());
  33. map.addControl(new GMapTypeControl());
  34. map.centerAndZoom(new GPoint(106.083984375,36.94989178681327),13);
  35. // Download the data in data.xml and load it on the map. The format we
  36. // expect is:
  37. // <markers>
  38. //   <marker lat="37.441" lng="122.141"/>
  39. //   <marker lat="37.322" lng="121.213"/>
  40. // markers>
  41. var request = GXmlHttp.create();
  42. request.open("GET", "http://www.step1.cn/GoogleAPI/map/data.xml", true);
  43. request.onreadystatechange = function() {
  44. if (request.readyState == 4) {
  45. var xmlDoc = request.responseXML;
  46. var markers = xmlDoc.getElementsByTagName("marker");
  47. for (var i = 0; i < markers.length; i++) {
  48. var point = new GPoint(parseFloat(markers[i].getAttribute("lng")),
  49. parseFloat(markers[i].getAttribute("lat")));
  50. var marker = createMarker(point,i);
  51. map.addOverlay(marker);
  52. }
  53. }
  54. }
  55. request.send(null);
  56. // Creates a marker whose info window displays the given number
  57. function createMarker(point, index) {
  58. // Create a lettered icon for this point using our icon class from above
  59. var letter = String.fromCharCode("A".charCodeAt(0) + index);
  60. var icon = new GIcon(baseIcon);
  61. icon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
  62. var marker = new GMarker(point, icon);
  63. // Show this marker's index in the info window when it is clicked
  64. var html = "Marker " + letter + "b>";
  65. GEvent.addListener(marker, "click", function() {
  66. marker.openInfoWindowHtml(html);
  67. });
  68. return marker;
  69. }
  70. // Add 10 random markers in the map viewport
  71. var bounds = map.getBoundsLatLng();
  72. var width = bounds.maxX - bounds.minX;
  73. var height = bounds.maxY - bounds.minY;
  74. for (var i = 0; i < 1; i++) {
  75. var point = new GPoint(bounds.minX + width * Math.random(),
  76. bounds.minY + height * Math.random());
  77. var marker = createMarker(point, i + 1);
  78. map.addOverlay(marker);
  79. }
  80. // Add a polyline with 4 random points. Sort the points by longitude so that
  81. // the line does not intersect itself.
  82. var points = [];
  83. for (var i = 0; i < 5; i++) {
  84. points.push(new GPoint(bounds.minX + width * Math.random(),
  85. bounds.minY + height * Math.random()));
  86. }
  87. points.sort(function(p1, p2) { return p1.x - p2.x; });
  88. map.addOverlay(new GPolyline(points));
  89. var polyline = new GPolyline([new GPoint(116.1419, 37.4419),
  90. new GPoint(122.1519, 40.4519)],
  91. "#ff0000", 10);
  92. map.addOverlay(polyline);
  93. }//end if
  94. } //end load()
  95. script>
  96. head>
  97. <body onload="load()" onunload="GUnload()">
  98. <div id="message">div>
  99. <div id="map" style="width: 750px; height: 550px">div>
  100. body>
  101. html>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。