2010-11-21 45 views
0

我正在阅读Google的API,但没有看到任何示例,并且所有其他Google搜索示例都有点旧(大约1-2岁,大部分都是基于API密钥的:S)。Google地理编码API结果在地图上显示?

我想要一个文本输入。发送按钮。和下面的iframe。

如何使用我自己的输入显示地图上的位置?

这时我的iframe看起来像这样:

<iframe width="230" height="180" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="<?php echo $src ?>"> 

输入:(?但据我所知JSON建议)

<input id="googleMaps" name="googleMaps" type="text" /> 

此时此刻我“米,XML试图,但我发现XML只是文字在我的iframe,如:

This document had no style information. 
<GeocodeResponse> 
<status> 
OK 
</status> 
<result> 
<type> 
locality 
</type> 
<type> 
political 
</type> 
<formatted_address> 
(...) 

如何将这些数据传递给谷歌,并得到映射回:)?

非常感谢。

+0

我建议你看看JavaScript API,因为你想在地图上显示结果。 http://code.google.com/apis/maps/documentation/javascript/basics.html有地理编码示例。 – igorw 2010-11-21 14:42:26

+0

您没有提及您需要的地图类型,但是您可能会发现易于使用的静态地图:http://code.google.com/apis/maps/documentation/staticmaps/。您只需要几个基本工具:cURL和SimpleXML。 – stillstanding 2010-11-21 14:46:21

+0

不幸的是我要显示动态地图(像“blabla,纽约,纽约州”的用户类型地址,并且它显示了一个缩放选项等地图。我真的需要使用外部脚本吗?; s – fomicz 2010-11-21 14:52:30

回答

1

您需要使用Google Geocode API将您的地址进行地理编码以获取坐标,然后才能将坐标用于在地图上显示结果。例如,对于我的地址发布至地址参数如下

http://maps.googleapis.com/maps/api/geocode/json?address=154+Metro+Central+Heights+London+UK&sensor=true 

坐标可以从所得JSON来获得在url给出,如下面

"geometry": { 
     "location": { 
     "lat": 51.5001524, 
     "lng": -0.1262362 
     } 

PHP代码以获得JSON

<?php 

$address = $_GET['address']; 
$address=str_replace(" ","+",$address); 
if ($address) { 
    $json = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.$address. 
    '&sensor=true'); 
    echo $json; 
} 

?> 

,JavaScript会发现here。这解析JSON的关键代码如下

$.getJSON("getjson.php?address="+address, 
     function(data){ 
      lat=data.results[0].geometry.location.lat; 
      lng=data.results[0].geometry.location.lng; 
         //.... more map initialization code 
       } 
); 

给出我已经成立了一个工作示例为您刚才的问题here这将有助于您了解如何生成地图上的标记。如果您需要进一步澄清,请告知我。

+0

好吧,但是如果我需要使用XML而不是JSON呢?在这个时候,当我把“http://maps.googleapis.com/maps/api/geocode/json?”放在任何位置时,Opera弹出窗口下载窗口并询问在哪里下载这个“json”文件... – fomicz 2010-11-21 18:40:16

+0

每个浏览器都有不同的方式来处理不同的文件格式如果你打算看到JSON格式,请尝试在firefox或chrome中打开它如果你需要XML,你需要替换json with xml like this'http://maps.googleapis.com/maps/api/geocode/xml?address = your + address&sensor = true'。我向你展示了json格式,因为它更容易解析这个或者使用plain JavaScript或jQuery。 – Philar 2010-11-21 18:45:31

+0

请再次注意,在Safari中,XML呈现的方式与IE相反。如果您需要在Safari中查看原始XML文档,您需要查看源代码。不要跟Opera一样。 – Philar 2010-11-21 18:50:45

相关问题