Google Maps API有时不会返回AddressDetails中的PostalCode [英] Google Maps API sometimes not returning PostalCode within AddressDetails
问题描述
我创建了一个函数来获取位置输入,并将其传递给Google Maps JavaScript API V2,并返回经纬度/ 标准化地址,我必须将其分割为数据库中的不同字段(即城市,州,邮政)。我至少需要 5级/邮政编码准确性,以便从Google返回的数据将始终有...
- 国家
- 城市(地点)
- 州(行政区)
- 邮政 Long
以下是关于Google返回的JSON对象结构的一些信息。
http://code.google。 com / apis / maps / documentation / javascript / v2 / services.html#Geocoding_Structured
问题:对于某些情况, .AddressDetails将不包含邮政编码,而汇总的Placemark.address将包含邮政编码。
例如,以下输入示例将在地标内返回邮政编码.address,但不是Placemark.AddressDetails;
- 美国威斯康星州Milwaukee 10432 Innovation Drive
- MOMA
- 帝国大厦 - >这实际上是在SubAdministrativeArea.Locality下的一些奇怪的DependentLocality中返回邮政编码。
我的疑难解答代码:
< script type =text / javascriptsrc =http://ajax.googleapis.com /ajax/libs/jquery/1.4.2/jquery.min.js\"></script>
<! - 您必须在此处插入您的地图API脚本。 - >
< script language =javascripttype =text / javascriptsrc =http://www.json.org/json2.js>< / script>
< script type =text / javascript>
$(document).ready(function()
{
var geocoder = new GClientGeocoder();
$ b $ .geolocateAddress = function(address)
{
geocoder.getLocations(address,function(response)
{
if(response!= null&&& response.Placemark!= undefined)
{
var placemark = response.Placemark [0];
//地标具有邮政编码或更高的准确性 - > http://code.google.com/apis/maps/documentation/javascript/v2 /reference.html#GGeoAddressAccuracy
if(placemark.AddressDetails.Accuracy> = 5)
{
alert(placemark.address);
try {
alert( placemark.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber);
} catch(err){
alert('什么是deuce?为什么没有邮政编码?...我敢打赌,placemark.address有邮政编码。');
alert(JSON.stringify(地标));
}其他{
alert('对不起,这需要至少一个邮政编码输入以保证准确性')
}
} else {
alert ('无法找到地址');
}
});
};
$ b $(#location)。blur(function(event)
{
var address = $(#location)。val();
$ .geolocateAddress(address);
});
});
< / script>
< form action =method =get>
< input title =Locationtype =textvalue =id =location/>
< / form>
唷......有什么我只是不了解Google地图地标的模式?我能通过升级到Google Maps JavaScript API V3来解决这个问题吗? (V2 JSON对象似乎更直观IMO)
http:/ /code.google.com/apis/maps/documentation/javascript/services.html
我即将在这方面付诸表决。希望有人能提供帮助!
自从存在地理编码服务以来,这一直是个问题。
稍后,请参阅我在2008年发布到问题跟踪器的错误报告/功能请求:
http://code.google.com/p/gmaps-api-问题/问题/详细信息?id = 606
在该主题中,Pamela Fox和Thor Mitchell是当时属于地图团队成员的Google员工。
一年半后,Google回应了一些改进:
https://groups.google.com/group/google-maps-api/browse_thread / thread / 4d0ade19dadcda4f#
事情有所改善,但地理编码并不是一门精确的科学。
在一个您可能正在使用旧版本的地理编码器,因为我收到了带有此请求的邮政编码:
应该返回相同的数据。
I created a function to take location input, pass it to Google Maps Javascript API V2, and return a latitude/longitude/normalized address that I'll have to split into different fields in a database (i.e. city, state, postal). I'm requiring at least a level 5/post code accuracy for the input so that the returned data from Google will always have...
- Country
- City (Locality)
- State (Administrative Area)
- Postal
- Lat/Long
Here's some info on the JSON object structure that Google returns. http://code.google.com/apis/maps/documentation/javascript/v2/services.html#Geocoding_Structured
Problem: For some instances, Placemark.AddressDetails will not include a postal code, while the summarized Placemark.address will include the postal code.
For example, the following input examples will return a postal code inside Placemark.address, but not Placemark.AddressDetails;
- 10437 Innovation Drive, Milwaukee, WI 53226
- MOMA
- Empire State Building --> this actually returns the postal code inside of some weird "DependentLocality" under SubAdministrativeArea.Locality
Here's my troubleshooting code:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<!-- You must insert your maps API script here. -->
<script language="javascript" type="text/javascript" src="http://www.json.org/json2.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
var geocoder = new GClientGeocoder();
$.geolocateAddress = function(address)
{
geocoder.getLocations(address, function(response)
{
if (response != null && response.Placemark != undefined)
{
var placemark = response.Placemark[0];
// Placemark has Post code or higher accuracy -> http://code.google.com/apis/maps/documentation/javascript/v2/reference.html#GGeoAddressAccuracy
if (placemark.AddressDetails.Accuracy >= 5)
{
alert(placemark.address);
try {
alert(placemark.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber);
} catch (err) {
alert('What the deuce? Why is there no postal code?... I bet you the placemark.address has the postal code.');
alert(JSON.stringify(placemark));
}
} else {
alert('Sorry, this requires at least a postal code input for accuracy')
}
} else {
alert('Failed to geo locate address');
}
});
};
$("#location").blur(function(event)
{
var address = $("#location").val();
$.geolocateAddress(address);
});
});
</script>
<form action="" method="get">
<input title="Location" type="text" value="" id="location" />
</form>
Phew... Is there something I'm just not understanding about schema of Google Maps placemarks? Would I be able to solve this problem by upgrading to Google Maps Javascript API V3? (V2 JSON objects seems more intuitive IMO) http://code.google.com/apis/maps/documentation/javascript/services.html
I'm about to throw in the towel on this. Hopefully somebody can help!
This has been a problem ever since the geocoding service existed.
For a bit of background, see the "bug report/feature request" I posted to the issue tracker in 2008:
http://code.google.com/p/gmaps-api-issues/issues/detail?id=606
In that thread, Pamela Fox and Thor Mitchell are Google employees who were part of the Maps team at the time.
A year and a half later, Google responded with some improvements:
https://groups.google.com/group/google-maps-api/browse_thread/thread/4d0ade19dadcda4f#
and things improved a lot, but geocoding is not an exact science.
In any case, you might be using an old version of the geocoder because I get a postal code with this request:
That's a direct http request, but the API V3 should be returning the same data.
这篇关于Google Maps API有时不会返回AddressDetails中的PostalCode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!