2011-04-14 67 views
5

我有麻烦,从谷歌地图APIjQuery和谷歌地图JSON响应

获取地理位置信息的代码是非常简单的

$.ajax({ 
    type: "GET", 
    cache: false, 
    url: "http://maps.googleapis.com/maps/api/geocode/json", 
    dataType: "jsonp", 
    data: { 
     address: "Ljubljana " + "Slovenia", 
     sensor: "false" 
    }, 
    jsonpCallback:'json_response', 
    success: function(data) { 
     top.console.debug(data); 
     $('#location_setter').dialog('close'); 
    }, 
    error: function() { 
     alert("Error."); 
    } 
}); 


function json_response(data){ 
    alert("works"); 
} 

我总是得到一个错误回来。 我直接尝试过(我在其他地方见过回调应在年底前成立...

$.ajax({ 
    type: "GET", 
    cache: true, 
    url: "http://maps.googleapis.com/maps/api/geocode/json?address=Ljubljana Slovenia&sensor=false", 
    dataType: "jsonp", 
    jsonpCallback:'json_response', 
    success: function(data) { 
     top.console.debug(data); 
     $('#location_setter').dialog('close'); 
    }, 
    error: function() { 
     alert("Error."); 
    } 
}); 

请求的URL是正确形成:

http://maps.googleapis.com/maps/api/geocode/json?address=Ljubljana%20Slovenia&sensor=false&callback=json_response

,它给我正确的JSON

请指教!

你可以 '玩' 与它在http://jsfiddle.net/PNad9/

回答

2

如果它帮助别人......我放弃了,我彻底改变逻辑本身,现在它按预期工作:

首先我追加谷歌地图JS进入人体

var script = document.createElement('script'); 
     script.type = 'text/javascript'; 
     script.src = 'http://maps.google.com/maps/api/js?sensor=false&callback=get_longlat'; 
     $("body").append(script); 

你可以看到我指定的回调函数get_longlat ...

所以我定义这个功能和使用谷歌的地理编码对象

function get_longlat(address){ 

var geocoder = new google.maps.Geocoder(); 

if(!address){ 
    var p = US.get("location_postal"); 
    var c = US.get("location_city"); 
    var a = US.get("location_address"); 
    var address = a + ', ' + p + ' ' + c + ', Slovenia'; 
} 

if (geocoder) { 
    geocoder.geocode({ 'address': address }, function (results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      US.set("location_lat", results[0].geometry.location.lat(), 60); 
      US.set("location_lon", results[0].geometry.location.lng(), 60); 

      $('#location_setter').dialog('close'); 
     } 
     else { 
      console.log('No results found: ' + status); 
     } 
    }); 
} 

}

美国变量里面,是我们自己的用户设置的命名空间

希望它可以帮助别人,为什么你正在经历从原来的问题行为

+0

感谢队友,这节省了我很多时间。 – Jack 2013-01-04 19:15:45

0

我已经看过这个问题,但我无法弄清楚发生了什么事情。我发现的一件事是,如果我添加一个数据参数到错误,然后alert(data.status),它会给代码200.唯一的地方,我可以找到这个是解决它的v2的成功代码?!

error: function(data) { 
    alert(data.status); 
} 

而且我不知道,你可以使用JSONP因为阅读它似乎把多余的PARAMATERS的查询字符串的文件?当我删除它jsonpcallback并将数据类型更改为json响应代码开始说0.

没有太多的帮助对不起,但我现在得去工作了。

1

Here is a good post。您在(http://maps.googleapis.com/maps/api/geocode/json)上向Google发送请求的网址不会返回jsonp可解析的响应,它只会返回一个原始JSON对象。 Google拒绝在提供的回调中包装JSON。

对于此问题的解决方法(或多或少可以从我所知道的情况来看)是向此网址发出请求:http://maps.google.com/maps/geo,并在您的请求中包含Google Maps API密钥。另外,似乎有必要确保请求中的回调参数是查询字符串的最后一个参数。

这里是从服务(即使因为没有提供有效的谷歌地图API密钥它返回一个错误)可解析响应的的jsfiddle:http://jsfiddle.net/sCa33/

的另一种选择是你有什么已经看起来,并且使用Google的Geocoder对象来运行请求。

0
function codeAddress(address, title, imageURL) { 
      console.log(address); 

      geocoder.geocode({'address': address}, function (results, status) { 
       console.log(results); 
       if (status == google.maps.GeocoderStatus.OK) { 
        map.setCenter(results[0].geometry.location); 

         var marker = new google.maps.Marker({ 
          map: map, 
          position: results[0].geometry.location, 
          icon: "", 
          title: title 
         }); 

        /* Set onclick popup */ 
        var infowindow = new google.maps.InfoWindow({content: title}); 
        google.maps.event.addListener(marker, 'click', function() { 
         infowindow.open(marker.get('map'), marker); 
        }); 
       } 
       else if (status === google.maps.GeocoderStatus.OVER_QUERY_LIMIT) { 
        setTimeout(function() { 
         codeAddress(address, title, imageURL); 
        }, 250); 
       } else { 
        alert("Geocode was not successful for the following reason: " + status); 
       } 
      });