是否可以使用jQuery来制作Google地图API调用? [英] Is is possible to use jQuery to make Google Maps API calls?

查看:99
本文介绍了是否可以使用jQuery来制作Google地图API调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下内容无效:

  $ .get('http://maps.googleapis.com/maps/ api / geocode / json?& sensor = false& region = nz& address = queen',function(response){
console.debug(response);
});

由于Ajax的原始策略相同,这似乎是不可能的。但是,我正在尝试做一些互动操作,并且我需要JavaScript中的Google Maps API响应才能这样做。 解决方案

Lee的解决方案的一个替代方法是向您自己的服务器脚本发出Ajax请求,然后调用Google地理编码器网址。这是PHP中的一个简单示例。可以在此处找到一个工作示例。

 <?php 

$ address = $ _GET ['address'];
$ address = str_replace(,+,$ address);
if($ address){
$ json = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.$address。
' &安培;传感器=真');
echo $ json;
}

?>

jQuery代码

  $。getJSON(getjson.php?address =+ address,
function(response){
//其余代码
});


The following does not work:

$.get('http://maps.googleapis.com/maps/api/geocode/json?&sensor=false&region=nz&address=queen', function(response){ 
console.debug(response); 
});

It seems like it is not possible due to Ajax's same origin policy. However, I am trying to do something interactive and I need responses from Google Maps API within javascript in order to do so.

解决方案

An alternative to Lee's solution is to make an Ajax Request to your own server script which in turn makes a call to the Google geocoder url. Here's a simple example in PHP. A working example can be found here.

<?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;
}

?>

The jQuery Code

$.getJSON("getjson.php?address="+address,
        function(response){ 
                //rest of your code
             });

这篇关于是否可以使用jQuery来制作Google地图API调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆