【发布时间】:2014-01-18 01:04:11
【问题描述】:
我有一个获取用户坐标的 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";
}
);
});
}
基本上是一个函数,需要经度和纬度,这会打印在我从谷歌 XML 响应获得的 textField (signatureAddressStrokeText) 完整地址上
【问题讨论】:
标签: javascript winjs reverse-geocoding