Use Google map to parse Chinese addresses

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.