sla*_*dau 7 javascript parsing google-maps-api-3
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)
}
Run Code Online (Sandbox Code Playgroud)
这就是我现在使用的,大约90%的时间都可以使用.其他时候,它formatted_address包含一个城镇和一个城市以及国家,其他时间只有城镇和城市,其他时候你可以梦想的一切.
我还没有找到一种始终如一的方式来获取Google Maps API结果中的城市/州.你们中的任何人都有吗?谢谢.
Google在其网页上使用的一个响应示例:
{
"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
}
}
}
} ]
}
Run Code Online (Sandbox Code Playgroud)
有时这些领域没有价值,有时他们也有.
Dr.*_*lle 12
你为什么要解析formatted_address?
有address_components,你可以浏览它们并寻找那些
"types": [ "administrative_area_level_1", "political" ]// the state
"types" : [ "locality", "political" ]//the city
Run Code Online (Sandbox Code Playgroud)
这是一个例子:http://jsfiddle.net/doktormolle/QWPCL/
| 归档时间: |
|
| 查看次数: |
8647 次 |
| 最近记录: |