Javascript Google Maps API返回lat/long但未捕获错误
读这个标题可能会让人搞不清楚到底发生了什么……我同意 所以我有一个地址,我想在谷歌地图上显示。我没有lat/long,所以我需要调用GoogleMapsAPI提供的Geocoder服务 当我请求lat/long以获取地址时,服务返回一个合法值,但是当在geocode函数的最后一行上单步执行javascript时,代码进入geocoder.js精简代码,并且永远不会返回(没有成功的结果,没有异常) 我已经做了很多关于使用GoogleMapsAPI的研究,据我所知,我已经实现了示例所显示的内容。我哪里出了问题 下面是调用lat/long服务的javascript代码:Javascript Google Maps API返回lat/long但未捕获错误,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,读这个标题可能会让人搞不清楚到底发生了什么……我同意 所以我有一个地址,我想在谷歌地图上显示。我没有lat/long,所以我需要调用GoogleMapsAPI提供的Geocoder服务 当我请求lat/long以获取地址时,服务返回一个合法值,但是当在geocode函数的最后一行上单步执行javascript时,代码进入geocoder.js精简代码,并且永远不会返回(没有成功的结果,没有异常) 我已经做了很多关于使用GoogleMapsAPI的研究,据我所知,我已经实现了示例所显示的内容。我哪
function getLatLong(street, city, state, zip) {
var geocoder = new google.maps.Geocoder();
var address = street && street.length > 0 ? street + ' ' : '';
address += city && city.length > 0 ? city + ', ' : '';
address += state && state.length > 0 ? state + ' ' : '';
address += zip && zip.length > 0 ? zip : '';
var latLong = {
lat: 0.0,
long: 0.0
};
try {
geocoder.geocode({ 'address' : address }, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
return {
lat: results[0].geometry.location.lat(),
long: results[0].geometry.location.lng()
};
} else {
return latLong;
}
});
} catch (exception) {
alert(e.message);
}
}
(如果您不想单步执行,则在上面代码中的catch
上方的行中,它“迷路了”)
如果您想看到它的实际应用,并亲自完成,请访问以下URL:
编辑:如果有帮助,下面是添加地图和标记的函数,它调用上面的getLatLong函数:
function plotMapHardCodePos(position) {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 9,
center: position
});
$.each(tournaments.Result, function (i, item) {
var latLong = {};
if (!item.Latitude || !item.Longitude) {
latLong = getLatLong(item.Street, item.City, item.State, item.ZipCode);
if (latLong) {
item.Latitude = latLong.lat;
item.Longitude = latLong.long;
}
}
if (!(item.Latitude === 0.0 && item.Longitude === 0.0)) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(item.Latitude, item.Longitude),
map: map
});
marker.setIcon('http://maps.google.com/mapfiles/ms/icons/blue-dot.png');
var infoWindow = new google.maps.InfoWindow({
content: "<div class=''><h4><a href=\"#\">" + item.Name + "</a></h4></div><br/><span>" +
item.Body1 + "</span>" + "<br/><span>" + item.Body2 + "</span>"
});
google.maps.event.addListener(marker, 'click', function () {
infoWindow.open(map, marker);
});
}
});
}
功能图MaphardCodePos(位置){
var map=new google.maps.map(document.getElementById('map'){
缩放:9,
中锋:位置
});
$.each(锦标赛、结果、功能(i、项目){
var latLong={};
如果(!item.Latitude | |!item.Longitude){
latLong=getLatLong(item.Street、item.City、item.State、item.ZipCode);
如果(拉特朗){
项目纬度=latLong.lat;
item.Longitude=latLong.long;
}
}
如果(!(item.Latitude==0.0和item.Latitude==0.0)){
var marker=new google.maps.marker({
位置:新的google.maps.LatLng(item.Latitude,item.Longitude),
地图:地图
});
marker.setIcon('http://maps.google.com/mapfiles/ms/icons/blue-dot.png');
var infoWindow=new google.maps.infoWindow({
内容:“
”+
item.Body1+“”+”
“+item.Body2+“”
});
google.maps.event.addListener(标记,'click',函数(){
信息窗口。打开(地图、标记);
});
}
});
}
您不能从异步回调函数中返回任何内容,您需要在回调函数中的数据可用时/在可用时使用它您不能从异步回调函数中返回任何内容,您需要在回调函数中的数据可用时/在可用时使用它不确定为什么值得进行下一票,但是谢谢你的回答@geocodezip@geocodezip现在可以了,谢谢@geocodezip您可以添加您的评论作为答案,这样我就可以给您评分了吗?