Parsing city/state from Google Maps request

2019-01-19 01:45发布

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_components": [ {
      "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.

2条回答
家丑人穷心不美
2楼-- · 2019-01-19 02:22

Why do you parse formatted_address ?

There are the address_components, 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.net/doktormolle/QWPCL/

查看更多
迷人小祖宗
3楼-- · 2019-01-19 02:29

This seems to work for me so far...

  readCityAndStateFromResult: (data) ->
    displayName = []
    for component in data[0].address_components
      if component.types and component.types.length
        switch true
          when 'locality' in component.types
            displayName.push component.long_name
          when 'administrative_area_level_1' in component.types or 'administrative_area_level_2' in component.types
            displayName.push component.short_name
    return displayName.join ', ' if displayName.length
    return null
查看更多
登录 后发表回答