从Google地图请求中解析城市/州 [英] Parsing city/state from Google Maps request

查看:97
本文介绍了从Google地图请求中解析城市/州的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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);
}
}
});

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

这是我现在使用的是什么,这个工作大约有90%的时间。其他时候, formatted_address 包含一个城镇和一个城市以及州,其他时间只是城镇和城市,其他时间是您梦寐以求的一切。



我还没有找到一种总是从Google Maps API结果中获得城市/州的一致方式。你们有没有一个人?感谢。

谷歌在其网页上使用的一个响应示例:

  {
status:OK,
results:[{
types:[street_address],
formatted_address: 1600 Amphitheater 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,
:[route]
},{
long_name:Mountain View,
short_name:Mountain View,
types:[ b,b,b,b,b,b, ,政治]
},{
long_name:美国,
short_name:美国,
types:[country, 政治]
},{
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
}
}
}
} ]
}

有时候这些字段没有值,为什么要解析 formatted_address



有address_components,你可以通过它们来寻找那些带有

 types :[administrative_area_level_1,political] //州
ty pes:[locality,political] //城市

下面是一个例子: a href =http://jsfiddle.net/doktormolle/QWPCL/ =noreferrer> http://jsfiddle.net/doktormolle/QWPCL/


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.

解决方案

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/

这篇关于从Google地图请求中解析城市/州的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆