【问题标题】:How to call the Mapkit js Geocode method如何调用 Mapkit js Geocode 方法
【发布时间】:2018-08-14 00:54:27
【问题描述】:
【问题讨论】:
标签:
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>