悠闲的漫步的个人主页

http://bbs.dragonsoul.top/u.php?uid=117  [收藏] [复制]

悠闲的漫步

世界是美好的,空气是清新的

  • 8

    关注

  • 16

    粉丝

  • 56

    访客

  • 等级:精灵王
  • 身份:论坛版主
  • 总积分:3906
  • 男,1980-04-26

最后登录:2017-01-02

更多资料

日志

网页中嵌入GOOGLE MAP

2011-05-03 17:22
1. JAVA代码获得当前指定城市的经度和纬度
  1. package com;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.UnsupportedEncodingException;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLEncoder;
    import java.text.MessageFormat;
    public class Test {
        public static void main(String[] args) {
            Test t = new Test();
           System.out.println(t.getLatlng("Shanghai"));
        }
        /**
    * 利用googlemap api 通过 HTTP 进行地址解析
    * @param address 地址
    * @return HTTP状态代码,精确度(请参见精确度常数),纬度,经度
    */
        private String getLatlng(String address){
           String ret = "";
           if(address != null && !address.equals("")){
            try {
             address = URLEncoder.encode(address,"UTF-8");//进行这一步是为了避免乱码
            } catch (UnsupportedEncodingException e1) {
            }
            //q 是查询的目标地址,output 是响应格式,key 是要使用此 Web 服务的关键字字符串
         //在此响应内,200 是 HTTP 状态代码,表明解析成功;4 是精度常量;41.879535 是经度;-87.624333 是纬度。
      
            String[] arr = new String[4];
            arr[0] = address;
            arr[1] = "csv";
            arr[2] = "true";
            arr[3] = "ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA";
            //http://maps.google.com/maps/geo?q=Chicago&output=cvs&key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA
            String url = MessageFormat.format("http://maps.google.com/maps/geo?q={0}&output={1}&sensor={2}&key={3}",arr);
            URL urlmy = null;
            try {
             urlmy = new URL(url);
             HttpURLConnection con = (HttpURLConnection) urlmy.openConnection();
             con.setFollowRedirects (true );
             con.setInstanceFollowRedirects(false );
             con.connect();
             BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8"));
             String s = "";
             StringBuffer sb = new StringBuffer("");
             while ((s = br.readLine()) != null ) {
              sb.append(s+"\r\n");
             }
             ret = ""+sb;
            } catch (MalformedURLException e) {
            } catch (IOException e) {
            }
           }
           return ret;
        }
    }


2. 页面中定位
例子:
  1. <!DOCTYPE html PUBLIC "-//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" xmlns:v="urn:schemas-microsoft-com:vml">
      <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
        <title>Google 地图 JavaScript API 示例: 简单的地图</title>
        <script src="http://ditu.google.cn/maps?file=api&v=2&key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA&hl=zh-CN"
                type="text/javascript"></script>
        <script type="text/javascript">

        function initialize() {
          if (GBrowserIsCompatible()) {
            var map = new GMap2(document.getElementById("map_canvas"));
            map.setCenter(new GLatLng(31.2303930, 121.4737040), 14);
          }
        }

        </script>
      </head>
      <body onload="initialize()" onunload="GUnload()">
        <div id="map_canvas" style="width: 500px; height: 300px"></div>
      </body>
    </html>



更多例子:http://code.google.com/intl/zh-CN/apis/maps/documentation/javascript/v2/examples/


综合例子:

<!DOCTYPE html PUBLIC "-//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" xmlns:v="urn:schemas-microsoft-com:vml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Google Maps JavaScript API Example:     Extraction of Geocoding Data</title>
    <style type="text/css">
      @import url("http://www.google.com/uds/css/gsearch.css");
      @import url("http://www.google.com/uds/solutions/localsearch/gmlocalsearch.css");
      }
    </style>
    <script src="http://maps.google.com/maps?file=api&v=2.x&key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA"
            type="text/javascript"></script>
    <script src="http://www.google.com/uds/api?file=uds.js&v=1.0" type="text/javascript"></script>
    <script src="http://www.google.com/uds/solutions/localsearch/gmlocalsearch.js" type="text/javascript"></script>
    <script type="text/javascript">

    var map;
    var geocoder;

    function initialize() {
      map = new GMap2(document.getElementById("map_canvas"));
      map.setCenter(new GLatLng(34, 0), 12);
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      map.addControl(new google.maps.LocalSearch(), new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,20)));
      geocoder = new GClientGeocoder();
    }

    // addAddressToMap() is called when the geocoder returns an
    // answer.  It adds a marker to the map with an open info window
    // showing the nicely formatted version of the address and the country code.
    function addAddressToMap(response) {
      map.clearOverlays();
      if (!response || response.Status.code != 200) {
        alert("Sorry, we were unable to geocode that address");
      } 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(place.address + '<br>'/* +
          '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode*/);
      }
    }

    // showLocation() is called when you click on the Search button
    // in the form.  It geocodes the address entered into the form
    // and adds a marker to the map at that location.
    function showLocation() {
      var address = document.forms[0].q.value;
      geocoder.getLocations(address, addAddressToMap);
    }

   // findLocation() is used to enter the sample addresses into the form.
    function findLocation(address) {
      document.forms[0].q.value = address;
      showLocation();
    }
    </script>
  </head>

  <body onload="initialize()" onunload="GUnload()">

    <!-- Creates a simple input box where you can enter an address
         and a Search button that submits the form. //-->
    <form action="#" onsubmit="showLocation(); return false;">
      <p>
        <b>Search for an address:</b>
        <input type="text" name="q" value="" class="address_input" size="40" />
        <input type="submit" name="find" value="Search" />
      </p>
    </form>
    <div id="map_canvas" style="width: 600px; height: 400px"></div>

   <!-- Sample addresses //-->
   <p><b>Try these:</b><br />

   <a href="javascript:void(0)"
     onclick="findLocation('2007 hongmei shanghai');return false;">test</a><br />

   <a href="javascript:void(0)"
     onclick="findLocation('1 Telegraph Hill Blvd, San Francisco, CA, USA');return false;">1
     Telegraph Hill Blvd, San Francisco, CA, <b>USA</b></a><br />

   <a href="javascript:void(0)"
     onclick="findLocation('4141 Avenue Pierre-De-Coubertin, Montr&#x00E9;al, QC, Canada');return false;">4144
     Avenue Pierre-De-Coubertin, Montr&#x00E9;al, <b>Canada</b></a><br />

   <a href="javascript:void(0)"
     onclick="findLocation('Champ de Mars 75007 Paris, France');return false;">Champ
     de Mars 75007 Paris, <b>France</b></a><br />

   <a href="javascript:void(0)"
     onclick="findLocation('Piazza del Colosseo, Roma, Italia');return false;">Piazza
     del Colosseo, Roma, <b>Italia</b></a><br />

   <a href="javascript:void(0)"
     onclick="findLocation('Domkloster 3,  50667 K&#x00F6;ln, Deutschland');return false;">Domkloster
     3,  50667 K&#x00F6;ln, <b>Deutschland</b></a><br />

   <a href="javascript:void(0)"
     onclick="findLocation('Plaza de la Virgen de los Reyes, 41920, Sevilla, Espa&#x00F1;a');return false;">Plaza
     de la Virgen de los Reyes, 41920, Sevilla, <b>Espa&#x00F1;a</b></a><br />

   <a href="javascript:void(0)"
     onclick="findLocation('123 Main St, Googleville');return false;">
     123 Main St, <b>Googleville</b></a>
  </p>
  </body>
</html>




google map key 申请
http://code.google.com/intl/zh-CN/apis/maps/signup.html
http://www.ditu123.com/main/google_map_api.html
分类:默认分类|回复:0|浏览:774|全站可见|转载
 

Powered by phpwind v8.7.1 Certificate Copyright Time now is:05-19 10:37
©2003-2011 龍魂公会 版权所有 Gzip enabled 闽ICP备08004643号-3 Total 0.147308(s)