Domanda

I have a metro application (WinJS) which gets user coordinates, this is the code:

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;
    }
}

this prints on my textField (signatureAddressStrokeText) the coordinates, but I want to get location address string by using these coordinates, I need to make a reverse geocoding for achieve that, but so far I haven't found anything about this topic, how to make reverse geocoding on WinJS??

thanks in advance for the support


EDIT: I could make it work thanks to the Link Provided by WiredPrairie, this is the code generated by me:

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";
            }

            );

    });
}

basicly is a function, requires longitude and latitude, and this prints on my textField (signatureAddressStrokeText) full address obtained from google XML response

È stato utile?

Soluzione

Nothing is built into the WinRT API for this, so you'll need to use a 3rd party data provider to do the look up for you.

(I'm not sure if there are any free options that are of reasonable quality.)

Check the licenses to be sure they are suitable for your needs. Google's for example, requires that it only be used in conjunction with a Google Map (and could not be used in the scenario you show above unless you've also shown a Google Map on the UI).

Altri suggerimenti

I know nothing of WinJS however....

You could host your own geonames lookup solution using Node.js or alternatively get an API key from http://www.geonames.org

One such module, geonames (https://github.com/bugs181/geonames) is hosted on my github.
NOTE: I am the author of geonames.

It uses the following technologies: node.js, mongojs, line-reader, and geonames.org data

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top