google map开发学习笔记(三)

今天我们来学习Google的地址解析。

从上面两节我们可以试着做一个以某一个地理坐标为中心的矩形地图,这个没问题,但是在实际操作当中人们是不会输入地理坐标的,而是输入一些人们可以认知的描述地址信息的字符串,当接收到这样的字符串以后我们就需要地址解析了,即将字符串解析为地理坐标。我们需要用到这么一个对象GClientGeocoder,来看看他的用法吧:

 

地址解析对象:GClientGeocoder

可以通过 GClientGeocoder 对象访问 Google 地图 API 地址解析服务。使用 GClientGeocoder.getLatLng() 将字符串地址转换为 GLatLng 。此方法采用要转换的字符串地址以及在检索到地址后要执行的回调函数作为参数。该回调函数是必要的,他的参数就是解析出来的地理坐标,这下知道该怎么处理了吧,很简单。

 

上代码更容易理解:

 

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "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 Example</title>  
  6.     <mce:script src="http://ditu.google.cn/maps?file=api&v=2&key=ABQIAAAAChdCrN7wC9izi2oT-RnZVBQJamtJgghXUNimK5S1pZ0gfcTvLxTipoO9NuARtlJJIzXyxbfbVzVaAA&sensor=false" mce_src="http://ditu.google.cn/maps?file=api&v=2&key=ABQIAAAAChdCrN7wC9izi2oT-RnZVBQJamtJgghXUNimK5S1pZ0gfcTvLxTipoO9NuARtlJJIzXyxbfbVzVaAA&sensor=false"   
  7.             type="text/javascript"></mce:script>  
  8.     <!--这里就是Google map API开发的JavaScript代码 -->   
  9.     <mce:script type="text/javascript"><!--  
  10.    
  11.     var geocoder = null;  
  12.     var map = null;  
  13.     function initialize() {  
  14.       if (GBrowserIsCompatible()) {        
  15.         map = new GMap2(document.getElementById("map_canvas"));  
  16.         geocoder = new GClientGeocoder(); //地址解析对象  
  17.         map.addControl(new GLargeMapControl());  
  18.         map.addControl(new GMapTypeControl());  
  19.         map.setMapType(G_NORMAL_MAP);  
  20.         map.setCenter(new GLatLng(39.9493, 116.3975), 13);   
  21.       }   
  22.     }  
  23.     function showAddress(address) {  
  24.         if(geocoder){  
  25.             geocoder.getLatLng(   
  26.             address,   
  27.             function(point) {   
  28.               if (!point) {   
  29.                 alert("无法解析:" + address);   
  30.               } else {   
  31.                 map.setCenter(point, 13);  
  32.                 //创建一个标记对象   
  33.                 var marker = new GMarker(point);   
  34.                 //创建一个标记叠加层  
  35.                 map.addOverlay(marker);   
  36.                 //打开一个信息窗口  
  37.                 marker.openInfoWindowHtml(address);   
  38.               }   
  39.             }   
  40.           );   
  41.         }  
  42.     }  
  43.       
  44. // --></mce:script>   
  45.   </head>  
  46.   <body onload="initialize()" onunload="GUnload()">  
  47.     <input type="text" id="address" name="address" /> <button onclick="showAddress(document.getElementById('address').value);">提交</button>    
  48.     <div id="map_canvas" style="width:800px;height:600px;"></div>   
  49.   </body>   
  50. </html>  

 

代码简介:

var marker = new GMarker(point);
以地理坐标对象为参数创建一个标记

map.addOverlay(marker);

添加一个标记形式的叠加层

什么是叠加层请看这里:http://code.google.com/intl/zh-CN/apis/maps/documentation/overlays.html

 

//打开一个信息窗口
marker.openInfoWindowHtml(address);

什么是信息窗口请看这里:http://code.google.com/intl/zh-CN/apis/maps/documentation/introduction.html#Info_Windows

 

想要了解更多关于地址解析对象的请看这里:http://code.google.com/intl/zh-CN/apis/maps/documentation/services.html#Geocoding

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值