0

我需要使用 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"
       + "?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)。我怎么能打电话给它。?

4

1 回答 1

0

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

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

创建一个 /pages/map/map.html、map.js、map.css(换句话说,创建一个 /pages/map 并右键单击它并选择添加新项目并选择名为 map 的“页面”类型)

在 map.js 中,在“使用严格”之后包含以下内容,或者包含在另一个 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>
于 2013-05-14T18:16:05.143 回答