我正在研究一个项目,它几乎已经完成。问题是我对Javascript很陌生,无法弄清楚如何做一些可能非常简单的事情。这是我的代码的一部分:
var dista;
var p;
var addrbounds;
var addrlat;
var addrlng;
var addr;
var icon = new google.maps.MarkerImage("http://maps.google.com/mapfiles/ms/micons/red.png",
new google.maps.Size(32, 32), new google.maps.Point(0, 0),
new google.maps.Point(16, 32));
var center = null;
var map = null;
var currentPopup;
var bounds = new google.maps.LatLngBounds();
function addMarker(lat, lng, info) {
var address = document.myform.customer.value;
var geocoder = new google.maps.Geocoder();
geocoder.geocode({address: address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var addrbounds = results[0].geometry.location; //get geocoded results into variable
var addrlat = results[0].geometry.location.lat();
var addrlng = results[0].geometry.location.lng();
var p = new google.maps.LatLng(addrlat, addrlng);
var pt1 = new google.maps.LatLng(lat, lng);
dista = pt1.distanceFrom(p); //calculate distance between the points
var di = (dista/1000);
document.myform1.memory.value = di; //add dist to memory text box
return;
} else {
alert(address + ' not found');
}
});
// desired spot for if statement regarding distance
var pt = new google.maps.LatLng(lat, lng);
bounds.extend(pt);
var marker = new google.maps.Marker({
position: pt,
icon: icon,
map: map
});
var popup = new google.maps.InfoWindow({
content: info,
maxWidth: 300
});
google.maps.event.addListener(marker, "click", function() {
if (currentPopup != null) {
currentPopup.close();
currentPopup = null;
}
popup.open(map, marker);
currentPopup = popup;
});
google.maps.event.addListener(popup, "closeclick", function() {
map.panTo(center);
currentPopup = null;
});
}
基本上它的作用是在addMarker()函数中发生的第一件事是对地址进行地理编码,然后计算远离地址的距离。这很好用。当想要获得地理编码器的距离以创建if语句以仅在数据库中添加标记(如果它在特定半径内)时,问题就出现了。如您所见,我尝试将其移动到文本框中。这似乎有效,但它是循环的一部分,并且不会随循环的每次运行而更新。
我被困了几天所以任何帮助都会受到赞赏。我讨厌这么接近完成但是卡住了!
编辑:
我得到了它的工作。我就这样做了:
var dista;
var p;
var addrbounds;
var addrlat;
var addrlng;
var icon = new google.maps.MarkerImage("http://maps.google.com/mapfiles/ms/micons/red.png",
new google.maps.Size(32, 32), new google.maps.Point(0, 0),
new google.maps.Point(16, 32));
var center = null;
var map = null;
var currentPopup;
var bounds = new google.maps.LatLngBounds();
function addMarker(lat, lng, info) {
var address = document.myform.customer.value;
var geocoder = new google.maps.Geocoder();
geocoder.geocode({address: address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var addrbounds = results[0].geometry.location;
var addrlat = results[0].geometry.location.lat();
var addrlng = results[0].geometry.location.lng();
var p = new google.maps.LatLng(addrlat, addrlng);
var pt1 = new google.maps.LatLng(lat, lng);
dista = pt1.distanceFrom(p);
var di = (dista/1000);
document.myform1.memory.value = di;
document.myform1.memory1.value = addrlat;
document.myform1.memory2.value = addrlng;
return;
} else {
alert(address + ' not found');
}
});
var tstlat = document.myform1.memory1.value;
var tstlng = document.myform1.memory2.value;
var tstpt = new google.maps.LatLng(tstlat, tstlng);
var pt = new google.maps.LatLng(lat, lng);
var distan = pt.distanceFrom(tstpt);
var tstdistance = (distan/1000);
if (tstdistance < 20){
// alert (tstdistance);
bounds.extend(pt);
var marker = new google.maps.Marker({
position: pt,
icon: icon,
map: map
});
var popup = new google.maps.InfoWindow({
content: info,
maxWidth: 300
});
google.maps.event.addListener(marker, "click", function() {
if (currentPopup != null) {
currentPopup.close();
currentPopup = null;
}
popup.open(map, marker);
currentPopup = popup;
});
google.maps.event.addListener(popup, "closeclick", function() {
map.panTo(center);
currentPopup = null;
});
}
else
{
return;
}}
现在我只需修复地图刷新问题,然后项目就完成了!
答案 0 :(得分:0)
你不能获取地理编码器回调中的任何内容,地理编码强制执行异步请求,当响应到达时,你永远不会从回调外部知道。
而不是从外部获取某些内容,您必须将某些内容从回调中推送到外部(因为您已经通过设置文本框的值来完成)。您还可以从回调中调用另一个函数并提供距离作为参数。