我正在从事的这个项目有一个纬度和经度字段。我正在尝试整合对外部信息源的需求,并尝试将 Google Maps API 合并到这个项目中。我对 JavaScript 和 Google Maps API 非常熟悉。我的目标是让您单击一个链接,该链接会打开一个新窗口并显示谷歌地图。然后,您可以滚动、缩放等以找到您想要的内容,然后单击,窗口关闭,纬度/经度神奇地填充在原始页面上。我还想让地图显示已从数据库中单击的现有标记。我一直在关注这个https://developers.google.com/maps/articles/phpsqlajax_v3,现在在底部添加了 Lat/Long 字段。建议我使用
google.maps.event.addListener(marker, 'click', function (event) {
document.getElementById("latbox").value = this.getPosition().lat();
document.getElementById("lngbox").value = this.getPosition().lng();
});
但是,因为我是 n00b,所以我不知道在教程中的代码块中将该代码放在哪里工作:
function load() {
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(47.6145, -122.3418),
zoom: 13,
mapTypeId: 'roadmap'
});
var infoWindow = new google.maps.InfoWindow;
// Change this depending on the name of your PHP file
downloadUrl("getdata.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("name");
var address = markers[i].getAttribute("address");
var type = markers[i].getAttribute("type");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var html = "<b>" + name + "</b> <br/>" + address;
var icon = customIcons[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
icon: icon.icon,
shadow: icon.shadow
});
bindInfoWindow(marker, map, infoWindow, html);
}
});
}
google.maps.event.addListener(marker, 'click', function (event) {
document.getElementById("latbox").value = this.getPosition().lat();
document.getElementById("lngbox").value = this.getPosition().lng();
});
function bindInfoWindow(marker, map, infoWindow, html) {
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
}
function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}
function doNothing() {}
</script>
</head>
<body onload="load()">
<div id="map" style="width: 1024px; height: 800px"></div>
<input type="text" name="latFld" id="latFld" /><br />
<input type="text" name="lngFld" id="lngFld" /><br />
</body>
</html>