Javascript 从回调返回值
我以前没有任何javascript经验 我正在尝试实现以下函数,我希望使用该函数返回lat和lng值:Javascript 从回调返回值,javascript,Javascript,我以前没有任何javascript经验 我正在尝试实现以下函数,我希望使用该函数返回lat和lng值: function get_address() { var geocoder = new google.maps.Geocoder() geocoder.geocode({ address: "SE-17270 Sverige"}, function(locResult) { var lat = locResult[0].geometry.location.
function get_address() {
var geocoder = new google.maps.Geocoder()
geocoder.geocode({ address: "SE-17270 Sverige"},
function(locResult) {
var lat = locResult[0].geometry.location.lat();
var lng = locResult[0].geometry.location.lng();
alert(lat);
alert(lng);
})
}
我该怎么做
所以我想做的是这样的:
function get_address(postcode)
{
var geocoder = new google.maps.Geocoder()
var lat
var lng
geocoder.geocode({ address: "SE-"+postcode+"Sverige"},
function(locResult) {
lat = locResult[0].geometry.location.lat();
lng = locResult[0].geometry.location.lng();
})
return lat,lng
}
声明变量
lat
和lng
外部回调函数。声明变量lat
和lng
外部回调函数。使用回调处理地理编码结果:
function myCallback(lat, lng) {
// Process lat and lng.
}
function get_address(callback) {
var geocoder = new google.maps.Geocoder()
geocoder.geocode({ address: "SE-17270 Sverige"},
function(locResult) {
var lat = locResult[0].geometry.location.lat();
var lng = locResult[0].geometry.location.lng();
callback(lat, lng);
}
);
}
....
get_address(myCallback);
使用回调处理地理编码结果:
function myCallback(lat, lng) {
// Process lat and lng.
}
function get_address(callback) {
var geocoder = new google.maps.Geocoder()
geocoder.geocode({ address: "SE-17270 Sverige"},
function(locResult) {
var lat = locResult[0].geometry.location.lat();
var lng = locResult[0].geometry.location.lng();
callback(lat, lng);
}
);
}
....
get_address(myCallback);
您可以使用全局变量或返回数组中的值,例如: 全局变量方法:
lat = "";
lng = "";
function get_address() {
var geocoder = new google.maps.Geocoder()
geocoder.geocode({ address: "SE-17270 Sverige"},
function(locResult) {
lat = locResult[0].geometry.location.lat();
lng = locResult[0].geometry.location.lng();
alert(lat);
alert(lng);
})
}
function get_address() {
var geocoder = new google.maps.Geocoder()
geocoder.geocode({ address: "SE-17270 Sverige"},
function(locResult) {
var lat = locResult[0].geometry.location.lat();
var lng = locResult[0].geometry.location.lng();
thearray = [];
thearray.push(lat);
thearray.push(lng);
return thearray;
})
}
数组方法:
lat = "";
lng = "";
function get_address() {
var geocoder = new google.maps.Geocoder()
geocoder.geocode({ address: "SE-17270 Sverige"},
function(locResult) {
lat = locResult[0].geometry.location.lat();
lng = locResult[0].geometry.location.lng();
alert(lat);
alert(lng);
})
}
function get_address() {
var geocoder = new google.maps.Geocoder()
geocoder.geocode({ address: "SE-17270 Sverige"},
function(locResult) {
var lat = locResult[0].geometry.location.lat();
var lng = locResult[0].geometry.location.lng();
thearray = [];
thearray.push(lat);
thearray.push(lng);
return thearray;
})
}
您可以使用全局变量或返回数组中的值,例如: 全局变量方法:
lat = "";
lng = "";
function get_address() {
var geocoder = new google.maps.Geocoder()
geocoder.geocode({ address: "SE-17270 Sverige"},
function(locResult) {
lat = locResult[0].geometry.location.lat();
lng = locResult[0].geometry.location.lng();
alert(lat);
alert(lng);
})
}
function get_address() {
var geocoder = new google.maps.Geocoder()
geocoder.geocode({ address: "SE-17270 Sverige"},
function(locResult) {
var lat = locResult[0].geometry.location.lat();
var lng = locResult[0].geometry.location.lng();
thearray = [];
thearray.push(lat);
thearray.push(lng);
return thearray;
})
}
数组方法:
lat = "";
lng = "";
function get_address() {
var geocoder = new google.maps.Geocoder()
geocoder.geocode({ address: "SE-17270 Sverige"},
function(locResult) {
lat = locResult[0].geometry.location.lat();
lng = locResult[0].geometry.location.lng();
alert(lat);
alert(lng);
})
}
function get_address() {
var geocoder = new google.maps.Geocoder()
geocoder.geocode({ address: "SE-17270 Sverige"},
function(locResult) {
var lat = locResult[0].geometry.location.lat();
var lng = locResult[0].geometry.location.lng();
thearray = [];
thearray.push(lat);
thearray.push(lng);
return thearray;
})
}
在标题部分添加google库:
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
在标题部分添加google库:
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>