Javascript 在bing地图中使用HTML、js从城市名称获取纬度和经度
我需要使用bing地图按城市名称获取纬度和经度。这是我的密码Javascript 在bing地图中使用HTML、js从城市名称获取纬度和经度,javascript,windows-store-apps,bing-maps,Javascript,Windows Store Apps,Bing Maps,我需要使用bing地图按城市名称获取纬度和经度。这是我的密码 function Geocode() { //Create Bing Maps REST Services request to geocode the address provided by the user var geocodeRequest = "http://dev.virtualearth.net/REST/v1/Locations/" + "Colombo" + "?outp
function Geocode() {
//Create Bing Maps REST Services request to geocode the address provided by the user
var geocodeRequest = "http://dev.virtualearth.net/REST/v1/Locations/"
+ "Colombo"
+ "?output=json"
//Set the callback function
+ "&jsonp=GetLocationCoordinates"
+ "&key=Ai5r7K1Jy95BfrDbOV9PPvoBqYicNNe3Bapi7PczGda-l30CjbpHeLnK8XQmYcKl";
//Submit the request
MakeServiceRequest(geocodeRequest);
}
function MakeServiceRequest(request) {
var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", request);
document.body.appendChild(script);
GetLocationCoordinates();
}
function GetLocationCoordinates(geocodeResponse) {
if(geocodeResponse==null) document.getElementById("txt").innerText = "This is null";
if (geocodeResponse &&
geocodeResponse.resourceSets &&
geocodeResponse.resourceSets.length > 0 &&
geocodeResponse.resourceSets[0].resources &&
geocodeResponse.resourceSets[0].resources.length > 0) {
setLoc(geocodeResponse.resourceSets[0].resources[0].geocodePoints.coordinates[0], geocodeResponse.resourceSets[0].resources[0].geocodePoints.coordinates[1], 10);
}
else {//The location could not be geocoded
var md = new Windows.UI.Popups.MessageDialog("The location could not be geocoded");
md.showAsync();
}
}
但在这里它从未调用函数GetLocationCoordinates(geocodeResponse)。我该如何调用它呢?可能是因为您是从本地上下文运行此操作的。创建一个iframe并将代码添加到生成的html文件中 我已将代码包装在名称空间中。通过这种方式,您可以定义调用函数的位置,而不是将函数固定在全局命名空间中。注意,我注释掉了您对函数的直接调用,它不是必需的 创建/pages/map/map.html、map.js、map.css(换句话说,创建/pages/map并右键单击它,选择add new item并选择名为map的“页面”类型) 在map.js中,在“use strict”之后包含以下内容,或包含在另一个javascript文件中。这取决于你
WinJS.Namespace.define("LocationServices", {
GetLocationCoordinates: function (geocodeResponse) {
if (geocodeResponse == null) document.getElementById("txt").innerText = "This is null";
if (geocodeResponse &&
geocodeResponse.resourceSets &&
geocodeResponse.resourceSets.length > 0 &&
geocodeResponse.resourceSets[0].resources &&
geocodeResponse.resourceSets[0].resources.length > 0) {
setLoc(geocodeResponse.resourceSets[0].resources[0].geocodePoints.coordinates[0], geocodeResponse.resourceSets[0].resources[0].geocodePoints.coordinates[1], 10);
}
else {//The location could not be geocoded
var md = new Windows.UI.Popups.MessageDialog("The location could not be geocoded");
md.showAsync();
}
},
Geocode: function () {
//Create Bing Maps REST Services request to geocode the address provided by the user
var geocodeRequest = "http://dev.virtualearth.net/REST/v1/Locations/"
+ "Colombo"
+ "?output=json"
//Set the callback function
+ "&jsonp=LocationServices.GetLocationCoordinates"
+ "&key=Ai5r7K1Jy95BfrDbOV9PPvoBqYicNNe3Bapi7PczGda-l30CjbpHeLnK8XQmYcKl";
//Submit the request
this.MakeServiceRequest(geocodeRequest);
},
MakeServiceRequest: function (request) {
var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", request);
document.body.appendChild(script);
// GetLocationCoordinates();
}
});
依次加载map.html(其中包括对map.js的引用,并且输入id='txt')
<iframe src="ms-appx-web:///pages/map/map.html"></iframe>