最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Parsing citystate from Google Maps request - Stack Overflow

programmeradmin4浏览0评论
var geocoder = new google.maps.Geocoder();
            geocoder.geocode({'latLng': foundLoc}, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    if (results[1]) {
                        var loc = getCityState(results);
                    }
                }
            }); 

function getCityState(results)
        {
            var citystateArray = results[1].formatted_address.split(",",2);
            var city = citystateArray[0];
            var state = citystateArray[1].substring(1, 3);
            return (city + ', ' + state)
        }

This is what I use now, and this works about 90% of the time. Other times, the formatted_address contains a town and a city along with the state, other times just the town and city, other times everything you can dream of.

I haven't yet found a CONSISTENT way of ALWAYS getting the city/state from a Google Maps API result. Do any of you guys have one? Thanks.

Example of one response that Google uses as an example on their page:

{
  "status": "OK",
  "results": [ {
    "types": [ "street_address" ],
    "formatted_address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
    "address_ponents": [ {
      "long_name": "1600",
      "short_name": "1600",
      "types": [ "street_number" ]
    }, {
      "long_name": "Amphitheatre Pkwy",
      "short_name": "Amphitheatre Pkwy",
      "types": [ "route" ]
    }, {
      "long_name": "Mountain View",
      "short_name": "Mountain View",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "California",
      "short_name": "CA",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "United States",
      "short_name": "US",
      "types": [ "country", "political" ]
    }, {
      "long_name": "94043",
      "short_name": "94043",
      "types": [ "postal_code" ]
    } ],
    "geometry": {
      "location": {
        "lat": 37.4219720,
        "lng": -122.0841430
      },
      "location_type": "ROOFTOP",
      "viewport": {
        "southwest": {
          "lat": 37.4188244,
          "lng": -122.0872906
        },
        "northeast": {
          "lat": 37.4251196,
          "lng": -122.0809954
        }
      }
    }
  } ]
}

Sometimes those fields don't have values, sometimes they do.

var geocoder = new google.maps.Geocoder();
            geocoder.geocode({'latLng': foundLoc}, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    if (results[1]) {
                        var loc = getCityState(results);
                    }
                }
            }); 

function getCityState(results)
        {
            var citystateArray = results[1].formatted_address.split(",",2);
            var city = citystateArray[0];
            var state = citystateArray[1].substring(1, 3);
            return (city + ', ' + state)
        }

This is what I use now, and this works about 90% of the time. Other times, the formatted_address contains a town and a city along with the state, other times just the town and city, other times everything you can dream of.

I haven't yet found a CONSISTENT way of ALWAYS getting the city/state from a Google Maps API result. Do any of you guys have one? Thanks.

Example of one response that Google uses as an example on their page:

{
  "status": "OK",
  "results": [ {
    "types": [ "street_address" ],
    "formatted_address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
    "address_ponents": [ {
      "long_name": "1600",
      "short_name": "1600",
      "types": [ "street_number" ]
    }, {
      "long_name": "Amphitheatre Pkwy",
      "short_name": "Amphitheatre Pkwy",
      "types": [ "route" ]
    }, {
      "long_name": "Mountain View",
      "short_name": "Mountain View",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "California",
      "short_name": "CA",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "United States",
      "short_name": "US",
      "types": [ "country", "political" ]
    }, {
      "long_name": "94043",
      "short_name": "94043",
      "types": [ "postal_code" ]
    } ],
    "geometry": {
      "location": {
        "lat": 37.4219720,
        "lng": -122.0841430
      },
      "location_type": "ROOFTOP",
      "viewport": {
        "southwest": {
          "lat": 37.4188244,
          "lng": -122.0872906
        },
        "northeast": {
          "lat": 37.4251196,
          "lng": -122.0809954
        }
      }
    }
  } ]
}

Sometimes those fields don't have values, sometimes they do.

Share Improve this question edited Jul 1, 2011 at 1:59 Yi Jiang 50.1k16 gold badges138 silver badges136 bronze badges asked Jul 1, 2011 at 1:34 slandauslandau 24.1k43 gold badges124 silver badges186 bronze badges 3
  • Can you provide a few different types of responses - perhaps a regular expression could be constructed that would reliably work. – Sampson Commented Jul 1, 2011 at 1:52
  • Added an example of one response that happens to have full values – slandau Commented Jul 1, 2011 at 1:56
  • Can you show a couple other examples of formatted_address that you might receive? – Sampson Commented Jul 1, 2011 at 1:57
Add a ment  | 

2 Answers 2

Reset to default 13

Why do you parse formatted_address ?

There are the address_ponents, you can walk through them and look for the ones with

"types": [ "administrative_area_level_1", "political" ]// the state
"types" : [ "locality", "political" ]//the city

Here's an example: http://jsfiddle/doktormolle/QWPCL/

This seems to work for me so far...

  readCityAndStateFromResult: (data) ->
    displayName = []
    for ponent in data[0].address_ponents
      if ponent.types and ponent.types.length
        switch true
          when 'locality' in ponent.types
            displayName.push ponent.long_name
          when 'administrative_area_level_1' in ponent.types or 'administrative_area_level_2' in ponent.types
            displayName.push ponent.short_name
    return displayName.join ', ' if displayName.length
    return null
发布评论

评论列表(0)

  1. 暂无评论