在 46 字符串中,如果我手动键入点名称而不是变量res
[点名称] 它可以工作。但如果没有,那就行不通了。
它的工作原理是:
- 点击点
- 取点名称
- 在维基百科中搜索
- 解析xml
- 从 xml 获取 URL
- 在 iframe 中打开 URL
我哪里做错了?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>
<script type="text/javascript">
var map = null;
function GetMap() {
// Initialize the map
map = new Microsoft.Maps.Map(document.getElementById("mapDiv"), {
credentials: "An-KyHsPpYClz9oHXMZ6X3SW6I44Ei2DTTrUS_QlXUW43qqsvOAQrT9ekVEIBT9u"
});
Microsoft.Maps.Events.addHandler(map, 'click', LatLong);
}
function LatLong(e) {
if (e.targetType == "map") {
var point = new Microsoft.Maps.Point(e.getX(), e.getY());
var loc = e.target.tryPixelToLocation(point);
lola = "lo,loe".replace('lo', loc.latitude).replace('loe', loc.longitude);
}
MakeGeocodeRequest('An-KyHsPpYClz9oHXMZ6X3SW6I44Ei2DTTrUS_QlXUW43qqsvOAQrT9ekVEIBT9u');
}
function ClickGeocode(credentials) {
map.getCredentials(MakeGeocodeRequest);
}
function MakeGeocodeRequest(credentials) {
var geocodeRequest = "http://dev.virtualearth.net/REST/v1/Locations/" + lola + "/?output=json&jsonp=GeocodeCallback&key=" + credentials;
CallRestService(geocodeRequest);
}
function GeocodeCallback(result) {
res = (result.resourceSets[0].resources[0].name);
console.log(res);
$(document).ready(function () {
$.ajax({
type: "GET",
url: "http://en.wikipedia.org/w/api.php?action=opensearch&limit=1&format=xml&search="+res,
dataType: "xml",
success: xmlParser
});
});
}
function xmlParser(xml) {
var str = jQuery(xml).find("Url").text()
alert(str);
document.getElementById("iframe").src=str.replace('en.', 'm.');
}
function CallRestService(request) {
var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", request);
document.body.appendChild(script);
}
</script>
</head>
<body onload="GetMap();">
<iframe align="right" height="500px" id="iframe" src="" width="48%"></iframe>
<div id='mapDiv' style="position:relative; width:48%; height:500px;"></div>
</body>
</html>