2

我正在尝试使用 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);
}
4

3 回答 3

3

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

geocoder.lookup('New York', function(err, data) {
    console.log(data);
});
于 2019-01-23T14:31:58.390 回答
1

您非常接近获得您正在寻找的信息。更新您的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数组可能有多个条目。

于 2018-10-09T04:24:42.133 回答
0
 <!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>
于 2019-12-16T15:51:42.510 回答