我有麻烦越来越从谷歌地图API的地理位置信息
I am having troubles getting geo location info from google maps api
在code是pretty的直线前进
the code is pretty straight forward
$.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");
}
我总是得到一个错误回来。 我想直接过(我读的地方,回调应设置在最后...
I always get an error back. I tried directly too (I read somewhere that the callback should be set at the end...
$.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.");
}
});
您输入的网址正确形成的:
the request url is correctly formed:
http://maps.googleapis.com/maps/api/geo$c$c/json?address=Ljubljana%20Slovenia&sensor=false&callback=json_response
和它给了我正确的JSON
and it gives me the correct json
请指教!
您可以'玩'与它在 http://jsfiddle.net/PNad9/
如果它可以帮助别人......我放弃了,我彻底改变逻辑本身,现在它按预期工作:
if it helps somebody... I gave up and I completely change the logic itself and now it works as expected:
首先,我追加谷歌地图JS进入体内
first I appended google maps js into the body
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 ...
as you can see I specified the callback function get_longlat...
所以我定义这个功能和使用谷歌的地理code对象
so I defined this function and used google's geocode object
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);
}
});
}
}
美国变量里面,是我们自己的命名空间为用户设置
The US variables inside is our own namespace for USER SETTINGS
希望它可以帮助别人