【问题标题】:Getting a json response into variable将 json 响应放入变量中
【发布时间】:2011-08-09 10:48:28
【问题描述】:

我正在尝试使用 google geocoder api 来获取要在地图制作应用中使用的地址的经度和纬度。

我如何从地理编码请求中获取数据(例如:

“http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true”是api网站上使用的例子)成我的脚本中有一个变量,所以我可以从中提取数据?还是可以通过其他方式提取数据?

谢谢!! :)

【问题讨论】:

    标签: javascript json variables request google-geocoder


    【解决方案1】:

    好的,首先要做的事……您正在考虑使用 JavaScript,而不是 Java。

    我要带你去一个不同的方向。您不需要 JSON。

    我假设您使用的是 google maps javascript api?如果不是,你应该是。

    要使用 google maps api,您需要在网页的 <head> 部分包含此 <script> 标签:

    <script src="http://maps.google.com/maps/api/js?sensor=false"></script>
    

    &lt;head&gt; 添加第二个&lt;script&gt; 标记,如下所示:

    <script type="text/javascript">     
       var geocoder;
    
       function Geocode(address){
          geocoder = new google.maps.Geocoder();
    
          geocoder.geocode({ 'address': address }, function (results, status) {
             if (status == google.maps.GeocoderStatus.OK) {
                console.log("Latitude: " + results[0].geometry.location.lat());
                console.log("Longitude: " + results[0].geometry.location.lng());
             }
             else {
                console.log("Geocoding failed: " + status);
             }
          });
       } 
    </script>
    

    上面的 Geocode() 函数接受一个地址作为参数。然后,它通过来自 Google Maps API 的 Geocoder 对象发出 Geocode 请求,并将来自 Geocoder 对象的响应传递给回调函数 (function(results,status){...})。有关传递给回调函数的“结果”参数的完整参考,请查看here

    希望这会有所帮助。

    【讨论】:

    • 在那个例子中,javascript中的对象会被调用什么?抱歉,我对使用 javascript 很陌生!谢谢!!
    • @AltheFuzz:你使用的是 JAVA 还是 JAVASCRIPT?
    • javascript,我想 - 我是在脚本标签中与 html 一起做的 - 真的很抱歉那里的混乱
    • 我之前确实尝试过使用 jquery,但我无法理解它——那太好了,谢谢!如果有帮助的话,我有版本 1.5.2.min :)
    • @AltheFuzz:我收回这一点,jQuery 不是必需的,但您需要使用 Google Maps JavaScript API。
    猜你喜欢
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 2015-02-21
    • 1970-01-01
    相关资源
    最近更新 更多