Google Maps geo-analytic and anti-analytic geocode.geocoder detailed

Source: Internet
Author: User

Address resolution is the process of converting an address (such as Guizhou province Guiyang) to geographic coordinates (such as longitude: 106.71, Latitude: 26.57).

The inverse of geo-resolution is the process of converting geographic coordinates (such as Latitude: 26.57, Longitude: 106.71) to Address (7th, Nanming District, Cui Wei Alley, Guiyang, Guizhou Province, China).
Due to local laws and various reasons, many of the domestic map does not include the geo-analytic and anti-analytic functions (geo-analytic and anti-analytic function is not strong enough), Google is always the best. No more nonsense.
To use Google Map geo-parsing and anti-parsing capabilities, we need to understand the Google.maps.Geocoder class, Google Maps to provide us with a powerful API, below we can implement
1. Initialize the map (most basic, not explained)

Initialize maps var map = new Google.maps.Map (document.getElementById ("Map_canvas"), {center:new google.maps.LatLng (26.57, 106 . Zoom:8, MapTypeId:google.maps.MapTypeId.ROADMAP});

2. Instantiation of Google Geocoder services

Instantiate Geocoder service var geocoder = new Google.maps.Geocoder ();

So we can do geo-parsing and anti-parsing, using the code:

Geocoder.geocode (Request:geocoderrequest, Callback:function (array.<geocoderresult>, GeocoderStatus))

I. Data requests:

The data that needs to be requested Geocoderrequest can be 4 properties:

Properties type Description
De style= "margin:0px; padding:0px; border:0px; outline:0px; Vertical-align:baseline; ">addressde> De style= "margin:0px; padding:0px; border:0px; outline:0px; Vertical-align:baseline; ">stringde> Place names that need to be parsed. Optional.
De style= "margin:0px; padding:0px; border:0px; outline:0px; Vertical-align:baseline; ">boundsde> De style= "margin:0px; padding:0px; border:0px; outline:0px; Vertical-align:baseline; ">LatLngBoundsde> Latitude Search range. Optional. (I don't have a specific trial)
De style= "margin:0px; padding:0px; border:0px; outline:0px; Vertical-align:baseline; ">locationde> De style= "margin:0px; padding:0px; border:0px; outline:0px; Vertical-align:baseline; ">LATLNG (note type) de> Longitude and latitude that need to be parsed. Optional.
De style= "margin:0px; padding:0px; border:0px; outline:0px; Vertical-align:baseline; ">regionde> De style= "margin:0px; padding:0px; border:0px; outline:0px; Vertical-align:baseline; ">stringde> Country code. Optional. (I don't have a specific trial)

For parsing we use address, inverse parsing uses location (note the type of incoming), and at least one of the requests is selected.

II: Result Processing:

For the rollback function (that is, the processing function returned after parsing) contains two contents, Geocoderresult (parse result, array type) and Geocoderstatus (parse state)
1. The parsing state is the state returned after parsing with Geocoder (), containing 5 types:

Error (Google Maps service may be wrong)
Invalid_request (Geocoderrequest invalid, that is, the input request is wrong, there may be no choice, or the property is wrong)
OK (parse complete, with corresponding data)
Over_query_limit (Response timeout)
Request_denied (webpage is forbidden Geocoder parsing)
Unknown_error (Unknown error)
Zero_results (0 results)

What we can use is the condition of OK.
2. Parsing results

Properties type Description
De style= "margin:0px; padding:0px; border:0px; outline:0px; Vertical-align:baseline; ">address_componentsde> De style= "margin:0px; padding:0px; border:0px; outline:0px; Vertical-align:baseline; ">Array.<GeocoderAddressComponent>de> De style= "margin:0px; padding:0px; border:0px; outline:0px; Vertical-align:baseline; The >geocoderaddresscomponentde>s array
De style= "margin:0px; padding:0px; border:0px; outline:0px; Vertical-align:baseline; ">formatted_addressde> De style= "margin:0px; padding:0px; border:0px; outline:0px; Vertical-align:baseline; ">stringde> Best match address after formatting (place name can be small to street)
De style= "margin:0px; padding:0px; border:0px; outline:0px; Vertical-align:baseline; ">geometryde> De style= "margin:0px; padding:0px; border:0px; outline:0px; Vertical-align:baseline; ">GeocoderGeometryde> De style= "margin:0px; padding:0px; border:0px; outline:0px; Vertical-align:baseline; The >GeocoderGeometryde> object
De style= "margin:0px; padding:0px; border:0px; outline:0px; Vertical-align:baseline; ">typesde> De style= "margin:0px; padding:0px; border:0px; outline:0px; Vertical-align:baseline; ">Array.<string>de> A string array that represents the type of the returned geocoding element

Each successful analysis will have the above information, we most need two formatted_address and geometry. And address_components is an array of place names, including Long_name (such as returning only the names of provinces and cities), short_name and types, and you can try them yourself.

A. Formatted place name Formatted_address, just call directly

B.geometry returns a de style= "margin:0px; padding:0px; border:0px; outline:0px; Vertical-align:baseline; ">GeocoderGeometryde> object, which contains 4 properties

Properties Type Description
De style= "margin:0px; padding:0px; border:0px; outline:0px; Vertical-align:baseline; ">boundsde> De style= "margin:0px; padding:0px; border:0px; outline:0px; Vertical-align:baseline; ">LatLngBoundsde> The exact bounds of the resolution
De style= "margin:0px; padding:0px; border:0px; outline:0px; Vertical-align:baseline; ">locationde> De style= "margin:0px; padding:0px; border:0px; outline:0px; Vertical-align:baseline; ">LatLngde> Latitude/Longitude coordinates
De style= "margin:0px; padding:0px; border:0px; outline:0px; Vertical-align:baseline; ">location_typede> De style= "margin:0px; padding:0px; border:0px; outline:0px; Vertical-align:baseline; ">GeocoderLocationTypede> Returned de style= "margin:0px; padding:0px; border:0px; outline:0px; Vertical-align:baseline; ">location type de>
De style= "margin:0px; padding:0px; border:0px; outline:0px; Vertical-align:baseline; ">viewportde> De style= "margin:0px; padding:0px; border:0px; outline:0px; Vertical-align:baseline; ">LatLngBoundsde> View range for parsing results

At this point, all about geo-parsing and anti-parsing is almost finished, the specific API see Https://developers.google.com/maps/documentation/javascript/reference#Geocoder.

Feel good to say dizzy! Based on this, I made an application about batch parsing and anti-parsing, see: Http://map.yanue.net/geo.html,http://map.yanue.net/toLatLng. Use details: http://www.yanue.net /archives/207.html,

Let's take a look at the example

Instance code: (You need to copy to a local test)

<!DOCTYPE HTML><HTML><Head><MetaCharSet= "UTF-8"><Scriptsrc= "Http://maps.google.com/maps/api/js?sensor=false&libraries=places"type= "Text/javascript"></Script><title>Google Maps geo-analytic and anti-analytic geocode.geocoder detailed</title><Metaname= "Author"content= "Yanue" /><Metaname= "Copyright"content= "Powered by Yanue" /><Linkrel= "Site"href= "http://map.yanue.net/" /><Scripttype= "Text/javascript">window.onload= function() {//Initialize MapvarMap= NewGoogle.maps.Map (document.getElementById ("Map_canvas"), {center:NewGoogle.maps.LatLng (26.57, 106.72), Zoom:8, MapTypeId:google.maps.MapTypeId.ROADMAP});//Instantiating Geocoder ServicesvarGeocoder= NewGoogle.maps.Geocoder ();//1. Geographic parsing process//request data Geocoderrequest to address, value ' Guiyang 'Geocoder.geocode ({address:'Guiyang'},functionGeoresults (results, status) {//here is the return function (i.e. the result handler function)  //A status of OK indicates a result  if(Status==Google.maps.GeocoderStatus.OK) {//In general, there will be multiple results      //The first result is the result of the best match (matching the most complete result of the place name), here only the first one, the others can be recycled as needed      //the address after formattingAlert ('geo-analytic results:'+results[0].formatted_address); //geometry is a containing bounds (bounds), location (latitude/longitude coordinates), Location_type and viewport (view range)      //get the resolved latitude and longitudeAlert ('geo-analytic results:'+results[0].geometry.location); }Else{alert (": Error" +status); }}); //2. Geo-inverse parsing process//request data Geocoderrequest to location, value type LATLNG so we're going to instantiate the latitude and longitudeGeocoder.geocode ({location:NewGoogle.maps.LatLng (26.57, 106.72)},functionGeoresults (results, status) {//The results here are exactly the same as above .  if(Status==Google.maps.GeocoderStatus.OK) {alert ('geo-analytic results:'+results[0].formatted_address); Alert ('geo-analytic results:'+results[0].geometry.location); }Else{alert (": Error" +status); }});}</Script></Head><Body>    <DivID= "Map_canvas"style= ' width:300px; height:200px; '></Div></Body></HTML>

At this point, the article ends

Http://www.yanue.net/archives/217.html

Google Maps geo-analytic and anti-analytic geocode.geocoder detailed

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.