Purpose:Use Google map to parse Chinese addresses and obtain the default geographical location (longitude and latitude) of a region ).
Principle:Use a gclientgeocoder object.
Environment:Google Maps javascript API version 2, IE6 +, FF, chrome.
Core methods:
/** <Br/> * Param address: address to be resolved, for example, 'hefei city, Anhui Province '.. <br/> * Author: xingxiudong [at] gmail.com <br/> */<br/> function parsestring2glatlng (Address) {<br/> var geocoder = new gclientgeocoder (); <br/> If (geocoder) {<br/> geocoder. getlatlng (address, function (point) {<br/> If (! Point) {<br/> $ ("# console"). append ("Resolution/" "+ address +"/"failed! "). Append (" <br/> "); <br/>}else {<br/> $ (" # console "). append (" resolution successful! "+ Address +" -- latitude: "+ point. LAT () + "longitude:" + point. LNG ()). append ("<br/>"); <br/>}< br/>}); <br/>}< br/>}
Test code:
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <br/> <HTML xmlns = "http://www.w3.org/1999/xhtml"> <br/> <pead> <br/> <title> Google geographic analysis </title> <br/> </pead> <br/> <body> <br/> <Div id = "Console "> </div> <br/> <MCE: script Type = "text/JavaScript" src = "http://maps.google.com/maps? File = API & V = 2 "mce_src =" http://maps.google.com/maps? File = API & V = 2 "> </MCE: SCRIPT> <br/> <MCE: script Type = "text/JavaScript" src = "http://code.jquery.com/jquery-1.4.2.min.js" mce_src = "http://code.jquery.com/jquery-1.4.2.min.js"> </MCE: SCRIPT> <br/> <MCE: script Type = "text/JavaScript"> <! -- <Br/> parsestring2glatlng ("China"); <br/> parsestring2glatlng ("Anhui Province, China"); <br/> parsestring2glatlng ("Anhui Province "); <br/> parsestring2glatlng ("Wuhu City, Anhui Province"); <br/> parsestring2glatlng ("fanchang County, Wuhu City, Anhui Province "); <br/> parsestring2glatlng ("Dai Dian village, fanchang County, Wuhu City, Anhui Province"); <br/>/** <br/> * Param address: address to be resolved, eg: 'hefei city, Anhui Province '.. <br/> * Author: xingxiudong [at] gmail.com <br/> */<br/> function parsestring2glatlng (Address) {<br/> var geocoder = new gclien Tgeocoder (); <br/> If (geocoder) {<br/> geocoder. getlatlng (address, function (point) {<br/> If (! Point) {<br/> $ ("# console"). append ("Resolution/" "+ address +"/"failed! "). Append (" <br/> "); <br/>}else {<br/> $ (" # console "). append (" resolution successful! "+ Address +" -- latitude: "+ point. LAT () + "longitude:" + point. LNG ()). append ("<br/>"); <br/>}< br/> }); <br/>}< br/> // --> </MCE: SCRIPT> <br/> </body> <br/> </ptml> <br/>
Test results:
Resolution successful! China -- latitude: 35.86166 longitude: 104.195397
Resolution successful! Anhui, China --
Latitude: 31.860611 longitude: 117.2847
Resolution successful! Anhui -- latitude: 31.860611 longitude: 117.2847
Resolution successful!
Wuhu City, Anhui Province -- latitude: 31.33879 longitude: 118.385156
Resolution successful! Wuchang County, Wuhu City, Anhui Province --
Latitude: 31.082814 longitude: 118.199484
Resolution successful! Dai Dian village, fanchang County, Wuhu City, Anhui Province -- latitude: 31.125364 longitude: 118.208788
Appendix:More information about geocoding: http://code.google.com/intl/zh-CN/apis/maps/documentation/javascript/services.html#Geocoding