2013-12-18 47 views
1

需要这个帮助。谷歌地图API v3返回ZERO_RESULTS,但Maps.Google.com显示罚款

我使用Google Maps API v3获取存储在我的数据库中的人类可读地址的经度和纬度,然后在Google Map画布上放置1个标记。非常简单。

我遇到的问题是,一些地址被踢回的状态代码 - > ZERO_RESULTS

如果键入这些相同的地址到maps.google.com,他们会发现,特定区域并将其映射用一个近似的标记。

我在我的数据库中的一些地址不是特定的,这意味着他们可能没有邮政编码和/或他们可能没有城市,但都有一个省/地区和国家。另外需要注意的是,他们都没有街道地址。

有些人可读的地址可具有破折号( - ),空间(),撇号('),和逗号(,)

被踢回ZERO_RESULTS一个地址的一个例子是:

Soul-t'ukpyolsi, Korea, South 

在该特定示例中,国家在名称中包含逗号和空格,并且该地区还有一个撇号和短划线。

这里是我使用的Javascript代码:

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&key=MYKEY&sensor=false"></script> 
<script> 
function initialize() { 
    var myLatlng = new google.maps.LatLng(<?php echo $lat; ?>,<?php echo $long; ?>); 
    var mapOptions = { 
    zoom: 5, 
    center: myLatlng 
    } 
    var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions); 

    var marker = new google.maps.Marker({ 
     position: myLatlng, 
     map: map 
    }); 
} 

google.maps.event.addDomListener(window, 'load', initialize); 

</script> 

我使用PHP生成基于存储在我的数据库地址内容的详细地址路径。下面是我使用生成的路径,然后发出geolocator请求代码:

记住,城市和邮政编码/ ZIP可选的元素

// Replace any spaces in vars 
if (isset($gmaps_city)){ 
    $gmaps_city = str_replace(" ","+",$gmaps_city); 
} 
if (isset($gmaps_province)){ 
    $gmaps_province = str_replace(" ","+",$gmaps_province); 
} 
if (isset($gmaps_country)){ 
    $gmaps_country = str_replace(" ","+",$gmaps_country); 
} 
if (isset($gmaps_pcode)){ 
    $gmaps_pcode = str_replace(" ","+",$gmaps_pcode); 
} 

// If Postal Code and City 
if (isset($gmaps_city) AND isset($gmaps_pcode)){ 
    $gmaps_path = $gmaps_city.",+".$gmaps_province.",+".$gmaps_country.",+".$gmaps_pcode."&sensor=false"; 

// If City but no Postal Code 
} else if (isset($gmaps_city) AND !isset($gmaps_pcode)){ 
    $gmaps_path = $gmaps_city.",+".$gmaps_province.",+".$gmaps_country."&sensor=false"; 

// If Postal Code but no City 
} else if (!isset($gmaps_city) AND isset($gmaps_pcode)){ 
    $gmaps_path = $gmaps_province.",+".$gmaps_country.",+".$gmaps_pcode."&sensor=false"; 

// Only Province and Country 
} else { 
    $gmaps_path = $gmaps_province.",+".$gmaps_country."&sensor=false"; 
} 

// Determine Lat and Long of Address 
$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$gmaps_path); 

$output= json_decode($geocode); 

$lat = $output->results[0]->geometry->location->lat; 
$long = $output->results[0]->geometry->location->lng; 

$status = $output->status; 

任何帮助是极大的赞赏。

+0

为什么你认为这个地址'Soul-t'ukpyolsi,Korea,South'在谷歌地图中有效?请提供一个好的结果链接(我得到[首尔](https://maps.google.com/maps?q=Soul-t'ukpyolsi,+Korea,+South))。 – geocodezip

+0

相信问题的根源是拼写错误,它应该是 - > Seoul-t'ukpyolsi,韩国,南...相信它在谷歌地图中的作品,因为去Maps.Google.com和输入“灵魂韩国,南韩“(不正确的拼写)确实在首尔放置了一个标记。通过JSON生成ZERO_RESULTS – nmcwilli

回答

2

该示例可能由于拼写错误而导致问题。以下内容: “Seoul-t'ukpyolsi,Korea,South” 不返回ZERO_RESULTS。

破折号和撇号不应该根据您的代码导致问题。请举另一个例子。

+0

同样在这里:'Seoul-t'ukpyolsi,Korea,South'提供完整的JSON结果。 –

+0

看起来拼写错误是问题的根源。谢谢你的收获。即使如此,当在“Maps.Google.com”中输入“Soul-t'ukpyolsi,Korea,South”(拼写错误)时,它会在首尔正确放置标记。有没有什么方法可以复制相同的效果,为正确的拼写提供JSON结果?而不是从错误的拼写中取回ZERO_RESULTS。 – nmcwilli

+0

我问过同样的事情,但还没有找到谷歌提供的方式来检查这一点。防止这种情况的一种可能的方法是在输入或提交时检查用户输入,并检查ZERO_RESULTS并提示拼写错误。但是,它很可能会计入您的查询计数,因此不会很理想。 – Lothilius