Parsing city/state from Google Maps request

前端 未结 2 1600
终归单人心
终归单人心 2020-12-10 16:32
var geocoder = new google.maps.Geocoder();
            geocoder.geocode({\'latLng\': foundLoc}, function(results, status) {
                if (status == google.maps         


        
相关标签:
2条回答
  • 2020-12-10 16:58

    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
    
    0 讨论(0)
  • 2020-12-10 17:06

    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/

    0 讨论(0)
提交回复
热议问题