以文本方式查看主题

-  课外天地 李树青  (http://www.njcie.com/bbs/index.asp)
--  信息检索原理课件  (http://www.njcie.com/bbs/list.asp?boardid=16)
----  Google地图搜索API的使用方法  (http://www.njcie.com/bbs/dispbbs.asp?boardid=16&id=1018)

--  作者:admin
--  发布时间:2011/3/27 21:08:04
--  Google地图搜索API的使用方法

1)直接显示北京地图
<!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" c/>
    <title>Google Maps JavaScript API Example</title>
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=abcdefg&sensor=true_or_false"
            type="text/javascript"></script>
    <script type="text/javascript">
 
    function initialize() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(39.9208, 116.4001), 13);
        map.setUIToDefault();
      }
    }
 
    </script>
  </head>
  <body >
    <div id="map_canvas" style="width: 800px; height: 600px"></div>
  </body>
</html>


2)显示北京卫星地图
<!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" c/>
    <title>Google Maps JavaScript API Example</title>
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=abcdefg&sensor=true_or_false"
            type="text/javascript"></script>
    <script type="text/javascript">
 
    function initialize() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(39.9208, 116.4001), 13);
        map.setMapType(G_SATELLITE_MAP);
        map.setUIToDefault();
      }
    }
 
    </script>
  </head>
  <body >
    <div id="map_canvas" style="width: 800px; height: 600px"></div>
  </body>
</html>

3)按照时间间隔显示地图
<!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" c/>
    <title>Google Maps JavaScript API Example</title>
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=abcdefg&sensor=true_or_false"
            type="text/javascript"></script>
    <script type="text/javascript">
 
    function initialize() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(39.9208, 116.4001), 13);
        window.setTimeout(function(){
          map.panTo(new GLatLng(32.061, 118.79125));},5000
        );       
        map.setUIToDefault();
      }
    }
 
    </script>
  </head>
  <body >
    <div id="map_canvas" style="width: 800px; height: 600px"></div>
  </body>
</html>

4)显示浮动标注
<!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" c/>
    <title>Google Maps JavaScript API Example</title>
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=abcdefg&sensor=true_or_false"
            type="text/javascript"></script>
    <script type="text/javascript">
 
    function initialize() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(32.061, 118.79125), 13);
        map.openInfoWindow(map.getCenter(), document.createTextNode("Welcome to Nanjing"));   
        map.setUIToDefault();
      }
    }
 
    </script>
  </head>
  <body >
    <div id="map_canvas" style="width: 800px; height: 600px"></div>
  </body>
</html>

5)增加地图控件
<!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" c/>
    <title>Google Maps JavaScript API Example</title>
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=abcdefg&sensor=true_or_false"
            type="text/javascript"></script>
    <script type="text/javascript">
 
    function initialize() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(32.061, 118.79125), 13);
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());    
        map.setUIToDefault();
      }
    }
 
    </script>
  </head>
  <body >
    <div id="map_canvas" style="width: 800px; height: 600px"></div>
  </body>
</html>

6)增加用户可以交互的标注信息
<!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" c/>
    <title>Google Maps JavaScript API Example</title>
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=abcdefg&sensor=true_or_false"
            type="text/javascript"></script>
    <script type="text/javascript">
 
   function createMarker(point,html) {
        var marker = new GMarker(point);
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
        });
        return marker;
      }
 
    function initialize() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(32.061, 118.79125), 13);
        map.setUIToDefault();
        var point = new GLatLng(32.061, 118.79125);
        var marker = createMarker(point,\'<div style="width:240px"><a href=" http://www.beyondspaceandtime.org/ ">鸡鸣寺</a> </div>\')
        map.addOverlay(marker);
      }
    }
 
    </script>
  </head>
  <body >
    <div id="map_canvas" style="width: 800px; height: 600px"></div>
  </body>
</html>

7)基于关键词的地图搜索
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
  <head>
    <meta http-equiv="content-type" c/>
    <title>Google Maps Geocoding</title>
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key="
        type="text/javascript"></script>
    <script type="text/javascript">

    var map = null;
    var geocoder = null;

    function initialize() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(32.061, 118.79125), 13);
                map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());    
        geocoder = new GClientGeocoder();
      }
    }

    function showAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert("找不到" + address);
            } else {
              map.setCenter(point, 13);
              var marker = new GMarker(point);
              map.addOverlay(marker);
              marker.openInfoWindowHtml(address);
            }
          }
        );
      }
    }
    </script>
  </head>

  <body >
    <form action="#" >
      <p>
        <input type="text" size="60" name="address" value="Nanjing" />
        <input type="submit" value="Get Position" />
      </p>
      <div id="map_canvas" style="width: 800px; height: 600px"></div>
    </form>
  </body>
</html>

8)点击识别位置
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" c>
    <title>Google 地图 API 示例: 反向地图解析</title>
    <script src="http://ditu.google.cn/maps?hl=zh-CN&amp;file=api&amp;v=2&amp;key=ABQIAAAAPTwhF4vlVXrO7ZGOjA9sOxTqQ5WtUwE0C8FR1hRk-yj3qFOmhhR6esbxCerNyfzApAoTJGfOHzcKCQ"></script>
    <script type="text/javascript">

    var map;
    var geocoder;
    var address;

    function initialize() {
      if (GBrowserIsCompatible()) {
      var options = {
       listingTypes : "kmlonly"
      };
 
       map = new GMap2(document.getElementById("map_canvas"), {size:new GSize(800, 600)});
       map.setCenter(new GLatLng(32.061, 118.79125), 13);
       map.addControl(new GSmallMapControl());
       GEvent.addListener(map, "click", getAddress);
       geocoder = new GClientGeocoder();
//       map.enableGoogleBar();
      }
    }
   
    function getAddress(overlay, latlng) {
      if (latlng != null) {
        address = latlng;
        geocoder.getLocations(latlng, showAddress);
      }
    }

    function showAddress(response) {
      map.clearOverlays();
      if (!response || response.Status.code != 200) {
        alert("嗯,你点击的这个地方还没有准确地址!"+"状态码(Status Code):" + response.Status.code);
      } else {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1],
                            place.Point.coordinates[0]);
        marker = new GMarker(point);
        map.addOverlay(marker);
        marker.openInfoWindowHtml(
        \'<div style="font-size:13px;">\' +
        \'<b>你现在所点击的地址:</b><br/>\' + place.address + \'<br/><br/>\' +
        \'<b>准确度:</b>\' + place.AddressDetails.Accuracy + \'&nbsp;&nbsp;&nbsp;&nbsp;\' +
        \'<b>国家代码:</b> \' + place.AddressDetails.Country.CountryNameCode) +
        \'</div>\';
      }
    }


    </script>
   </head>
   <body >
    <div id="map_canvas"></div>
  </body></html>