使用HTML,js在bing map中获取城市名称的纬度和经度

时间:2013-05-14 04:10:58

标签: javascript windows-store-apps bing-maps

我需要使用bing map获取城市名称的纬度和经度。这是我的代码。

 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)。我怎样才能打电话给它。?

1 个答案:

答案 0 :(得分:0)

可能是因为您从本地上下文运行它。创建一个iframe并在生成的html文件中添加代码。

我已将代码包装在命名空间中。这样,您可以定义函数的调用位置,而不是将函数粘贴到全局命名空间中。注意我注释了你对该函数的直接调用,它不是必需的。

创建一个/pages/map/map.html,map.js,map.css(换句话说,创建一个/ pages / map并右键单击它并选择添加新项目并选择'page'类型命名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>