我有一个 wiki 网站,其中仅包含每个 wiki 页面中的名称和地址,我需要将其映射到谷歌地图中。我已经在https://developers.google.com/maps/articles/phpsqlsearch_v3上确定了 Google 的示例,它使用了 Google Maps 的 API 版本 3。
我已经到了能够从每个 wiki 页面提取名称和地址信息并将其传递给文件 phpsqlsearch_genxml.php 文件的地步,该文件为 javascript 代码(见下文)生成 xml 文本以解析为谷歌地图。
目前,只有当我在 xml 文件中包含通用距离、纬度和经度时,javascript 代码(见下文)才有效,如下所示:
<markers>
<marker name="Radio City Music Hall" address="1260 Avenue of the Americas, New York, NY"
distance="50.0000" lat="40.7142" lng="-74.0000"/>
<marker name="W Hotel, Times Square" address="1567 Broadway at 47th St, New York City, NY"
distance="50.0000" lat="40.7142" lng="-74.0000"/>
<marker name="Columbia University Student Service Centre" address="1140 Amsterdam Ave., New York,
distance="50.0000" lat="40.7142" lng="-74.0000"/>
</markers
(PS 我只是在 xml 中提供了一些位置,就像这里的示例一样)。
50.0000 的距离和 40.7142 的纬度以及 -74.0000 的 lng 只是我输入以获取下面的 javascript 的通用数字。是的,我意识到这些数字对于上述位置实际上并不正确,但它确实可以生成位置图钉。
我想让下面的 Javascript 使用我的 wiki 站点实际提供的有限的唯一 XML 信息(名称和地址),即:
<markers>
<marker name="Radio City Music Hall" address="1260 Avenue of the Americas, New York, NY"/>
<marker name="W Hotel, Times Square" address="1567 Broadway at 47th St, New York City, NY"/>
<marker name="Columbia University Student Service Centre" address="1140 Amsterdam Ave., New York,
NY"/>
</markers>
我已经阅读了上面 google 网站上的文档,并且我尝试过重构,但我无法获得仅限名称和地址的有限 xml 来工作。
有谁知道我可以如何重构下面的代码,以便只有 Google 地图的地理编码器会查找 XML 的地址并提供必要的纬度和经度以在地图上放置位置图钉?我知道谷歌限制了地理编码请求的数量,但我可以担心地图缓存和创建我自己的纬度和经度。以后坐标。在下面的代码中,我也不需要计算距离,所以我想我可以删除方法 searchLocations() 和 searchLocationsNear(center)。
另外,我将如何摆脱用户输入?现在,javascript bode 会生成一个用户搜索框,用户在其中单击“搜索”按钮,然后它会提供信息。但对于我的地图,我只想生成一张显示纽约市多个位置图钉的地图,并使用一个工具
我是谷歌地图的新手,所以提前感谢您的帮助!
Javascript code from https://developers.google.com/maps/articles/phpsqlsearch_v3:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<title>Google Maps AJAX + mySQL/PHP Example</title>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false"
type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
var map;
var markers = [];
var infoWindow;
var locationSelect;
function load() {
map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(40.7142, -74.0000),
zoom: 4,
mapTypeId: 'roadmap',
mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}
});
infoWindow = new google.maps.InfoWindow();
locationSelect = document.getElementById("locationSelect");
locationSelect.onchange = function() {
var markerNum = locationSelect.options[locationSelect.selectedIndex].value;
if (markerNum != "none"){
google.maps.event.trigger(markers[markerNum], 'click');
}
};
}
function searchLocations() {
var address = document.getElementById("addressInput").value;
var geocoder = new google.maps.Geocoder();
geocoder.geocode({address: address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
searchLocationsNear(results[0].geometry.location);
} else {
alert(address + ' not found');
}
});
}
function clearLocations() {
infoWindow.close();
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(null);
}
markers.length = 0;
locationSelect.innerHTML = "";
var option = document.createElement("option");
option.value = "none";
option.innerHTML = "See all results:";
locationSelect.appendChild(option);
}
function searchLocationsNear(center) {
clearLocations();
var radius = document.getElementById('radiusSelect').value;
var searchUrl = 'phpsqlsearch_genxml.php?lat=' + center.lat() + '&lng=' + center.lng() +
'&radius=' + radius;
downloadUrl(searchUrl, function(data) {
var xml = parseXml(data);
var markerNodes = xml.documentElement.getElementsByTagName("marker");
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < markerNodes.length; i++) {
var name = markerNodes[i].getAttribute("name");
var address = markerNodes[i].getAttribute("address");
var distance = parseFloat(markerNodes[i].getAttribute("distance"));
var latlng = new google.maps.LatLng(
parseFloat(markerNodes[i].getAttribute("lat")),
parseFloat(markerNodes[i].getAttribute("lng")));
createOption(name, distance, i);
createMarker(latlng, name, address);
bounds.extend(latlng);
}
map.fitBounds(bounds);
locationSelect.style.visibility = "visible";
locationSelect.onchange = function() {
var markerNum = locationSelect.options[locationSelect.selectedIndex].value;
google.maps.event.trigger(markers[markerNum], 'click');
};
});
}
function createMarker(latlng, name, address) {
var html = "<b>" + name + "</b> <br/>" + address;
var marker = new google.maps.Marker({
map: map,
position: latlng
});
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
markers.push(marker);
}
function createOption(name, distance, num) {
var option = document.createElement("option");
option.value = num;
option.innerHTML = name + "(" + distance.toFixed(1) + ")";
locationSelect.appendChild(option);
}
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.responseText, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}
function parseXml(str) {
if (window.ActiveXObject) {
var doc = new ActiveXObject('Microsoft.XMLDOM');
doc.loadXML(str);
return doc;
} else if (window.DOMParser) {
return (new DOMParser).parseFromString(str, 'text/xml');
}
}
function doNothing() {}
//]]>
</script>
</head>
<body style="margin:0px; padding:0px;" onload="load()">
<div>
<input type="text" id="addressInput" size="10"/>
<select id="radiusSelect">
<option value="25" selected>25mi</option>
<option value="100">100mi</option>
<option value="200">200mi</option>
</select>
<input type="button" onclick="searchLocations()" value="Search"/>
</div>
<div><select id="locationSelect" style="width:100%;visibility:hidden"></select></div>
<div id="map" style="width: 100%; height: 80%"></div>
</body>
</html>