谷歌地图


使用谷歌地图前可以申请key地址为
http://www.google.com/apis/maps/signup.html

将获取的key替换yourkey即可使用

<script src="http://ditu.google.cn/maps?file=api&amp;v=2&amp;key=yourkey"
            type="text/javascript"></script>

将下面的代码

<script type="text/javascript"> 
    function initialize() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("zan_canvas"));
        var center = new GLatLng(39.917,116.397);
        map.setCenter(center, 14);
 
        var marker = new GMarker(center, {draggable: true});
 
        GEvent.addListener(marker, "dragstart", function() {
          map.closeInfoWindow();
        });
        GEvent.addListener(marker, "dragend", function() {
         var windowHtml="<span style='color:gray'>";
         windowHtml=windowHtml+"纬度:"+marker.getLatLng().lat()+"<br />经度:"+marker.getLatLng().lng();
         windowHtml=windowHtml+"</span>";
          marker.openInfoWindowHtml(windowHtml);
          document.getElementById("lat").value=marker.getLatLng().lat();
          document.getElementById("lng").value=marker.getLatLng().lng();
        });
 
        map.addOverlay(marker);
        map.addControl(new GSmallMapControl());
        map.disableDragging()//禁用地图拖拽,默认为允许,禁止拖拽后,缩放好像自动会被禁止
        map.disableDoubleClickZoom()//禁用双击缩放,默认为允许
      }
    }
    </script> 
  </head> 
  <body onload="initialize()" onunload="GUnload()"> 
    <div id="zan_canvas" style="width: 500px; height: 300px"></div>
    <div>
    纬度:<input type="text" name="lat" id="lat" />
    经度:<input type="text" name="lng" id="lng" />
    </div>
    <div>
    做客户端程序的时候,加上form,并将input改为hidden即可
    http://code.google.com/intl/zh-CN/apis/maps/documentation/javascript/v2/examples/
    </div>
  </body> 

 

 加入你相应的jsp。

运行看到相应的地图出来,证明你运行成功了。

如果使用json的形式,可以使用对应的clienct进行测试

private static HttpMethod getGetMethod() throws IOException {

		GetMethod get = new GetMethod("/maps/api/geocode/json");

		NameValuePair simcard = new NameValuePair("latlng",
				"40.714224,-73.961452");

		NameValuePair simcard1 = new NameValuePair("sensor", "false");

		NameValuePair simcard2 = new NameValuePair("language", "zh-CN");

		get.setQueryString(new NameValuePair[] { simcard, simcard1, simcard2 });

		// InputStream input = new FileInputStream(new
		// File("/home/ubuntu/my.txt"));

		// "".getBytes("ISO8859-1")

		// InputStream input = new
		// StringBufferInputStream("my test aaaaaaaaaa");

		// post.setRequestBody(input);

		return get;

	}

	public static void main(String[] args) throws IOException {

		HttpClient client = new HttpClient();

		client.getHostConfiguration().setHost("ditu.google.com", 80, "http");

		HttpMethod method = getGetMethod();// 使用GET方式提交数据

		client.executeMethod(method);

		// 打印服务器返回的状态

		System.out.println(method.getStatusLine());

		// 打印结果页面

		;

		//        
		try {

			BufferedReader rd = new BufferedReader(new InputStreamReader(method
					.getResponseBodyAsStream(),

			"UTF-8"));

			StringBuffer sb = new StringBuffer();

			String line;

			while ((line = rd.readLine()) != null) {

				sb.append(line);

			}

			rd.close();

			System.out.println("接受到的流是:" + sb);

		} catch (IOException e) {

			throw new RuntimeException("error", e);

		}

		method.releaseConnection();

	}

看到相应的json的形式的字符即可。

猜你喜欢

转载自dakuishache.iteye.com/blog/1253739