admin管理员组

文章数量:1391981

I am currently using the google maps api to geocode locations on the map and return the address of the street.

I currently return the address with the following code:

        function codeLatLng(markerPos) {
            geocoder.geocode({'latLng': markerPos}, function(results, status) {
              if (status == google.maps.GeocoderStatus.OK) {
                if (results[1]) {
                //Set markerAddress variable
                var markerAddress = results[0].formatted_address;
alert(markerAddress);
...

But what if I don't want to return the formatted address but a more detailed version using Address Component Types, how can I return certain address values like:

Help appreciated.

I am currently using the google maps api to geocode locations on the map and return the address of the street.

I currently return the address with the following code:

        function codeLatLng(markerPos) {
            geocoder.geocode({'latLng': markerPos}, function(results, status) {
              if (status == google.maps.GeocoderStatus.OK) {
                if (results[1]) {
                //Set markerAddress variable
                var markerAddress = results[0].formatted_address;
alert(markerAddress);
...

But what if I don't want to return the formatted address but a more detailed version using Address Component Types, how can I return certain address values like: http://code.google./apis/maps/documentation/geocoding/#Types

Help appreciated.

Share Improve this question edited Dec 24, 2013 at 16:55 Kara 6,22616 gold badges53 silver badges58 bronze badges asked Jan 20, 2012 at 13:26 hairynuggetshairynuggets 3,32122 gold badges57 silver badges90 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

May I ask, why are you checking for results[1] and then using results[0] (or is it just a typo I should ignore)?

As long as status is OK, there will be at least one result. Otherwise, status would be ZERO_RESULTS.

Anyway, you can use something like this:

function codeLatLng(markerPos) {
  geocoder.geocode({'latLng': markerPos}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      var markerAddress = results[0].address_ponents[0].long_name
          + ' (type: ' + results[0].address_ponents[0].types[0] + ')';
      alert(markerAddress);

You can play a lot with the whole address_ponents array, have fun! :)

For even more fun, have a look at the (source code of the) Google Maps API v3 Geocoder Tool at http://gmaps-samples-v3.googlecode./svn/trunk/geocoder/v3-geocoder-tool.html

long_name is the full text description or name of the address ponent as returned by the Geocoder.

 function codeLatLng(markerPos) {
                geocoder.geocode({'latLng': markerPos}, function(results, status) {
                  if (status == google.maps.GeocoderStatus.OK) {
                    if (results[1]) {
                    //Set markerAddress variable
                    var markerAddress = results[0].long_name;
    alert(markerAddress);
...

本文标签: javascriptHow to return address types from google maps geocodeStack Overflow