【问题标题】:How to call the Mapkit js Geocode method如何调用 Mapkit js Geocode 方法
【发布时间】:2018-08-14 00:54:27
【问题描述】:

我正在尝试使用 Mapkit js Geocode 函数,但我不确定如何调用此函数。这是我的代码,我从警报(数据)中得到一个空值。 https://developer.apple.com/documentation/mapkitjs/mapkit/geocoder/2973884-lookup

var geocoder = new mapkit.Geocoder({
    language: "en-GB",
    getsUserLocation: true
});

geocoder.lookup("450 Serra Mall, Stanford, CA USA", getResult);

function getResult(data) {
    alert(data);
}

【问题讨论】:

    标签: javascript mapkit apple-maps geocoder


    【解决方案1】:

    回调的第一个参数是错误。如果您没有错误,则为空。

    geocoder.lookup('New York', function(err, data) {
        console.log(data);
    });
    

    【讨论】:

      【解决方案2】:

      您非常接近获得所需的信息。更新您的getResult 函数如下:

      function getResult(){
          // Make the results accessible in your browser's debugging console so you can see everything that was returned
          console.log(arguments)
      
          // The results are returned in an array. For example, to get the latitude and longitude
          var lat = arguments[1].results[0].coordinate.latitude
          var lng = arguments[1].results[0].coordinate.longitude
      
          // Show the results in HTML
          var pre = document.createElement('pre');
          pre.innerHTML = "Latitude: " + lat + " / Longitude: " + lng;
          document.body.appendChild(pre)
      }
      

      注意results 数组可能有多个条目。

      【讨论】:

        【解决方案3】:
         <!DOCTYPE html>
         <html>
         <head>
         <meta charset="utf-8">
        
         <script src="https://cdn.apple-mapkit.com/mk/5.x.x/mapkit.js"></script>
        
          <style>
           #map {
             width:  100%;
             height: 400px;
           }
          </style>
         </head>
        
          <body>
            <div id="dvResult" style="width: 100%; height: 20px"></div>
            <div id="map"></div>
        
          <script>
            mapkit.init({
             authorizationCallback: done => { 
                done('your token');
             },
             language: "es"
           });       
        
           var mGeocoder = new mapkit.Geocoder({ language: "en-GB", 
                                             getsUserLocation: true });
        
           mGeocoder.lookup("1000 Coit Rd, Plano TX 75050", (err, data) => {
        
          if(err)
            alert(err);
          else
          {
            console.log(data);  
        
            var lat = data.results[0].coordinate.latitude;
            var lng = data.results[0].coordinate.longitude;
        
            var dvResult = document.getElementById('dvResult');
            dvResult.innerHTML = "Lat: " + lat + " / Lng: " + lng;
         }
        });
         </script>
         </body>
         </html>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-08-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-02-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多