如何让这个函数返回经纬度字符串? 我是新手,真是费了很多脑筋实在不行了。。。。请大家帮忙啊 我alert 这个方法的返回值,显示是undefinedT_T 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 哦,你这个是异步的,出不出得看他的心情。你可以把它变成同步的试试看。function codeAddress(address) { var address = address; var result; //改成同步 $.ajaxSetup({ async: false }); geocoder.geocode({address:address},function geoResults(results, status){ if (status == google.maps.GeocoderStatus.OK) { result = results[0].geometry.location.lat().toString()+','+results[0].geometry.location.lng(); }else{ alert(":error " + status); } }); //改回来为好 $.ajaxSetup({ async: true }); return result; } 其实我就是想让这个方法返回经纬度字符串,然后我赋值给一个变量然后在地图上画线连接两个地点,可是就是无论如何也返回不了,总是undefined。求大家帮助呀 var geocoder; var map; window.onload=function(){ $("mapLoading").style.display='none'; $("map_canvas").style.display='block'; initialize();}function initialize() {//初始化 //alert(<?php echo $facebookid; ?>); geocoder = new google.maps.Geocoder(); var latlng = new google.maps.LatLng(41.80,123.42); var myOptions = { zoom: 7, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP } map = new google.maps.Map($("map_canvas"), myOptions); var latlng = codeAddress2("shenyang"); alert(latlng);var lineSymbol = { path: 'M 0,-1 0,1', strokeOpacity: 1, scale: 4};var line = new google.maps.Polyline({ path: [new google.maps.LatLng(41.84,123.79), new google.maps.LatLng(42.85,125.12)], strokeColor: "#BBD278", strokeOpacity: 0, icons: [{ icon: lineSymbol, offset: '0', repeat: '20px' }], map: map}); var input = document.getElementById('address'); new google.maps.places.Autocomplete(input); var latLngControl = new LatLngControl(map); // Register event listeners google.maps.event.addListener(map, 'mouseover', function(mEvent) { latLngControl.set('visible', true); }); google.maps.event.addListener(map, 'mouseout', function(mEvent) { latLngControl.set('visible', false); }); google.maps.event.addListener(map, 'mousemove', function(mEvent) { latLngControl.updatePosition(mEvent.latLng); }); //按下enter键执行 enterPress();} 这个是我的 初始化地图的方法 但不知道为什么您给的方法不执行了 不执行?到不了 alert(latlng);这步么? 恩对,alert 也不执行了,alert底下的代码也不执行了 严重不同意教人家把异步请求强制改成同步的行为,这种行为就是反js,就是反计算机!var latlng = codeAddress2("shenyang");//这样是无法获取到latlng的//要有js异步编程思想,要明白js是js,不是C不是Java//js中到处都是异步方法,到处都是回调,你设计API的时候也要考虑这个API是不是异步的//这直接影响API使用方式和代码组织方式//教人家async: false直接干脆,那是害了人家啊codeAddress2("shenyang", function(latlng ){ //latlng });//这样才是js的正确打开方式,//为什么CSDN上每次有人遇到异步获取数据无法同步返回,一致的回答都是async: false//这不是缺心眼吗?你自己遇到这个问题也这样处理么?//不吐槽了,洗洗睡//看LZ自己的造化了 帮忙看下这个jQuery小插件。 js 运算符优先级 和 getElementsByTagName 问题 javascript调试 如何读取XML 如何定义<a>标签获得焦点时的样式 请问如何获得innerHtml中的对象 问个简单的问题,在提交表单后如何知道表单中的checkbox已经选中 JavaScript函数的功能 下拉菜单,请帮我看看坐标有啥不对??? 由FORM提交进入下一个新开的页面,怎样才能由一个FORM能打开几个不同的新页面 如何让easyui的validatebox初始化的时候不验证 网站被劫持怎么办
function codeAddress(address) {
var address = address;
var result;
//改成同步
$.ajaxSetup({
async: false
});
geocoder.geocode({address:address},function geoResults(results, status){
if (status == google.maps.GeocoderStatus.OK) {
result = results[0].geometry.location.lat().toString()+','+results[0].geometry.location.lng();
}else{
alert(":error " + status);
}
});
//改回来为好
$.ajaxSetup({
async: true
});
return result;
}
var map;
window.onload=function(){
$("mapLoading").style.display='none';
$("map_canvas").style.display='block';
initialize();
}
function initialize() {//初始化
//alert(<?php echo $facebookid; ?>);
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(41.80,123.42);
var myOptions = {
zoom: 7,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map($("map_canvas"), myOptions);
var latlng = codeAddress2("shenyang");
alert(latlng);
var lineSymbol = {
path: 'M 0,-1 0,1',
strokeOpacity: 1,
scale: 4
};var line = new google.maps.Polyline({
path: [new google.maps.LatLng(41.84,123.79), new google.maps.LatLng(42.85,125.12)],
strokeColor: "#BBD278",
strokeOpacity: 0,
icons: [{
icon: lineSymbol,
offset: '0',
repeat: '20px'
}],
map: map
});
var input = document.getElementById('address');
new google.maps.places.Autocomplete(input);
var latLngControl = new LatLngControl(map);
// Register event listeners
google.maps.event.addListener(map, 'mouseover', function(mEvent) {
latLngControl.set('visible', true);
});
google.maps.event.addListener(map, 'mouseout', function(mEvent) {
latLngControl.set('visible', false);
});
google.maps.event.addListener(map, 'mousemove', function(mEvent) {
latLngControl.updatePosition(mEvent.latLng);
});
//按下enter键执行
enterPress();
}
这个是我的 初始化地图的方法 但不知道为什么您给的方法不执行了
alert(latlng);
这步么?
var latlng = codeAddress2("shenyang");
//这样是无法获取到latlng的//要有js异步编程思想,要明白js是js,不是C不是Java
//js中到处都是异步方法,到处都是回调,你设计API的时候也要考虑这个API是不是异步的
//这直接影响API使用方式和代码组织方式
//教人家async: false直接干脆,那是害了人家啊codeAddress2("shenyang", function(latlng ){
//latlng
});//这样才是js的正确打开方式,
//为什么CSDN上每次有人遇到异步获取数据无法同步返回,一致的回答都是async: false
//这不是缺心眼吗?你自己遇到这个问题也这样处理么?
//不吐槽了,洗洗睡
//看LZ自己的造化了