我有一个获取用户坐标的Metro应用程序(WinJS),这是代码:
var loc = null;
function getloc() {
if (loc == null) {loc = new Windows.Devices.Geolocation.Geolocator();}
if (loc != null) {loc.getGeopositionAsync().then(getPositionHandler, errorHandler);}
}
function getPositionHandler(pos) {
signatureAddressStrokeText.value = pos.coordinate.latitude + "," + pos.coordinate.longitude;
}
function errorHandler(e) {
signatureAddressStrokeText.value = e.message + getStatusString(loc.locationStatus);
}
function getStatusString(locStatus) {
switch (locStatus) {
case Windows.Devices.Geolocation.PositionStatus.ready:
return "Location is available.";
break;
case Windows.Devices.Geolocation.PositionStatus.initializing:
// This status indicates that a GPS is still acquiring a fix
return "A GPS device is still initializing.";
break;
case Windows.Devices.Geolocation.PositionStatus.noData:
// No location data is currently available
return "Data from location services is currently unavailable.";
break;
case Windows.Devices.Geolocation.PositionStatus.disabled:
// The app doesn't have permission to access location,
// either because location has been turned off.
return "Your location is currently turned off. " +
"Change your settings through the Settings charm " +
" to turn it back on.";
break;
case Windows.Devices.Geolocation.PositionStatus.notInitialized:
// This status indicates that the app has not yet requested
// location data by calling GetGeolocationAsync() or
// registering an event handler for the positionChanged event.
return "Location status is not initialized because " +
"the app has not requested location data.";
break;
case Windows.Devices.Geolocation.PositionStatus.notAvailable:
// Location is not available on this version of Windows
return "You do not have the required location services " +
"present on your system.";
break;
default:
break;
}
}
这打印在我的textField(signatureAddressStrokeText)坐标上,但是我想通过使用这些坐标来获取位置地址字符串,我需要进行反向地理编码才能实现,但到目前为止我还没有找到关于这个主题的任何内容,如何在WinJS上进行反向地理编码?
提前感谢支持
编辑:由于WiredPrairie提供的Link,我可以让它工作,这是我生成的代码:
function webServiceReverseGeolocation(latitude, longitude) {
return new WinJS.Promise(function (complete, error) {
var options = {
url: "http://maps.googleapis.com/maps/api/geocode/xml?latlng=" + latitude + "," + longitude + "&sensor=true",
responseType: "document"
};
WinJS.Promise.timeout(constants.WEB_SERV_TIMEOUT, WinJS.xhr(options)).then(
function (request) {
var doc = request.responseXML.documentElement;
var output = doc.getElementsByTagName("formatted_address");
signatureAddressStrokeText.value = output[0].textContent;
complete(true);
},
function (error) {
signatureAddressStrokeText.value = "Error getting location: " + error.status + " " + error.statusText, "Status";
complete(false);
},
function (progress) {
signatureAddressStrokeText.value = "Getting current address from coordinates provided by GPS device... please wait";
}
);
});
}
基本上是一个函数,需要经度和纬度,这打印在我的textField(signatureAddressStrokeText)从谷歌XML响应获得的完整地址
答案 0 :(得分:3)
WinRT API没有内置任何内容,因此您需要使用第三方数据提供程序来查找。
(我不确定是否有任何质量合理的免费选项。)
检查许可证以确保它们适合您的需要。例如,谷歌要求它只与谷歌地图一起使用(除非你也在用户界面上显示了谷歌地图,否则无法在上面显示的场景中使用)。
答案 1 :(得分:0)
但我对WinJS一无所知......
您可以使用geonames
托管自己的Node.js
查找解决方案,或者从http://www.geonames.org获取API密钥
一个这样的模块,geonames
(https://github.com/bugs181/geonames)托管在我的github上。
注意:我是geonames
的作者。
它使用以下技术:node.js,mongojs,line-reader和geonames.org数据