2013-05-14 90 views
0

我需要使用bing地圖獲取城市名稱的經度和緯度。這是我的代碼。使用HTML獲取城市名稱的經緯度,bing地圖中的js

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)。我可以怎樣稱呼它?

回答

0

可能是因爲您是從本地環境中運行此操作。創建一個iframe並在該HTML文件中添加代碼。

我把代碼包裝在名稱空間中。通過這種方式,您可以定義函數的調用位置,而不是將函數粘貼到全局名稱空間中。注意我註釋了你直接調用的函數,它不需要。

創建一個/pages/map/map.html,map.js,map.css(換句話說,創建一個/ pages/map並右擊它並選擇添加新項目並選擇'page'type named 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>
相關問題