【发布时间】:2018-04-30 17:14:22
【问题描述】:
我试图添加一个点击事件,当我点击地图上的某个地方时,它会获取用户的位置,然后他们点击一个标记,它会计算路线并显示距离。 到目前为止,这是我的代码,旨在在用户点击时将标记添加到地图
map.addListener('click', function(e) { //allows user to place a marker upon
a click
placeMarker(e.latLng, map);
});
var marker;
function placeMarker(location, map) { //current location marker function,
for direction service origin
if (marker) {
marker.setPosition(location);
} else {
var icon = { //custom icon for the current location marker
url: 'https://cdn2.iconfinder.com/data/icons/snipicons/500/map-
marker-128.png',
scaledSize: new google.maps.Size(45, 45)
};
marker = new google.maps.Marker({
position: location,
map: map,
icon: icon,
animation: google.maps.Animation.DROP
});
}
//place location for direction service
lat3 = marker.getPosition().lat();
lng3 = marker.getPosition().lng();
map.setCenter(location);
clickWindow(false, infowindow, map.getCenter(), map);
marker.addListener('mouseover', function () { //mouseover to remind user
of current location
//resultsMap.setCenter(marker);
infowindow.open(map, this);
infowindow.setContent("Current Location");
});
marker.addListener('mouseout', function () {
infowindow.close(map, marker);
});
}
function clickWindow(input, infowindow, location, map) { //called when
current location marker is placed
infowindow.setContent("Location Set");
infowindow.setPosition(location);
infowindow.open(map);
}
}
但是我的地图现在根本不再加载,有什么建议为什么会发生这种情况?谢谢
【问题讨论】:
-
那么,当用户点击地图时,要显示用户当前位置和点击地点之间的路线?
-
我在地图上已经有标记了,所以当他们点击地图时,我想将其作为他们的“当前位置”,然后他们可以点击一个标记,它会显示从他们到该标记的路线'当前位置' @lavor
标签: javascript google-maps google-maps-api-3