【问题标题】:How to do reverse geocoding on WinJS如何在 WinJS 上进行反向地理编码
【发布时间】: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


    【解决方案1】:

    WinRT API 中没有为此内置任何内容,因此您需要使用第 3 方数据提供程序来为您进行查找。

    (我不确定是否有任何质量合理的免费选项。)

    检查许可证以确保它们适合您的需求。例如,谷歌要求它只能与谷歌地图一起使用(并且不能在你上面展示的场景中使用,除非你还在 UI 上显示了谷歌地图)。

    【讨论】:

    • 看我的回答。此外,geonames.org 的数据质量非常好。通常不仅获得城市名称,而且通常还获得社区名称。 Geonames.org 数据也有市政厅、公园等实体数据。
    • 使用通过 Bing 地图逐点查找位置我得到一个 XML 响应:401UnauthorizedInvalidCredentials访问被拒绝。您可能输入的凭据不正确,或者您可能无权访问请求的资源或操作。
    • 听起来您没有获得服务的密钥。
    【解决方案2】:

    我对 WinJS 一无所知....

    您可以使用Node.js 托管自己的geonames 查找解决方案,或者从http://www.geonames.org 获取API 密钥

    一个这样的模块,geonames (https://github.com/bugs181/geonames) 托管在我的 github 上。
    注意:我是 geonames 的作者。

    它使用以下技术:node.js、mongojs、line-reader 和 geonames.org 数据

    【讨论】:

      猜你喜欢
      • 2010-11-02
      • 1970-01-01
      • 2020-08-30
      • 1970-01-01
      • 2021-01-26
      • 1970-01-01
      • 1970-01-01
      • 2018-05-30
      • 1970-01-01
      相关资源
      最近更新 更多