0

I am using the following code to call up a map and display. I would like to add the the function to do a reverse geocode and print out the corresponding street address. I cannot seem to get the long/lat coords passed over to the function. How can I get the long/lat coordinates converted to a street address?

function getMyLocation() {
    if (navigator.geolocation) {

        navigator.geolocation.getCurrentPosition(
        displayLocation, displayError, {
            enableHighAccuracy: true,
            timeout: 9000
        });

        var watchButton = document.getElementById("watch");
        watchButton.onclick = watchLocation;
        var clearWatchButton = document.getElementById("clearWatch");
        clearWatchButton.onclick = clearWatch;
    }
    else {
        alert("Oops, no geolocation support");
    }
}

function displayLocation(position) {
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;

    var div = document.getElementById("location");
    div.innerHTML = "You are at Latitude: " + latitude + ", Longitude: " + longitude;
    div.innerHTML += " (with " + position.coords.accuracy + " meters accuracy)";

    var km = computeDistance(position.coords, ourCoords);
    var distance = document.getElementById("distance");
    distance.innerHTML = "You are " + km + " km from the WickedlySmart HQ";

    if (map == null) {
        showMap(position.coords);
        prevCoords = position.coords;
    }
    else {
        var meters = computeDistance(position.coords, prevCoords) * 1000;
        if (meters > 20) {
            scrollMapToPosition(position.coords);
            prevCoords = position.coords;
        }
    }
}​
4

3 回答 3

0

如果你喜欢使用额外的库,你可以在这里查看geos它的 jquery 插件的最终地理编码/位置示例。

该示例使用该方法自动填充表单setLocation,但您也可以轻松地打印位置:

// location geocoded -- print address
var onGeocodeSuccess = function (location) {
  console.log(location.toAddress());
}

// location retrieved -- geocode it.
var onUserLocationSuccess = function (location) {
  geous.geocode (location, { 
    reverse: true,
    success: onGeocodeSuccess
  });
}

// Try and get user location
geous.getUserLocation({
  success: onUserLocationSuccess
});
于 2012-09-27T19:11:18.857 回答
0

对不起。误解了这个问题。这是我几周前为一个项目整理的一些代码。它只是提取邮政编码,但您可以从中获取地址。它是为 jQuery 编写的。此外,我正在动态加载 Google Maps API。如果需要,您可以直接加载它。

$(document).ready(function() {
    if (navigator.geolocation) {
        var script   = document.createElement("script");
        script.type  = 'text/javascript';
        script.src = 'http://www.google.com/jsapi?callback=loadMapsAPI';
        document.getElementsByTagName('head')[0].appendChild(script);
    } else {
        console.log('NOTE: Browser does not support geolocation.');
    }
}); 


function loadMapsAPI() {
    google.load('maps', '3', {
        'other_params': 'sensor=false',
        'callback' : mapLoaded
        }
    );
}


function mapLoaded() {
    $('#geolocateButton').css('display','block');
}


function findMyLocation() {
    navigator.geolocation.getCurrentPosition(
        function(position) {
            var latLng = new google.maps.LatLng(
                                position.coords.latitude,
                                position.coords.longitude);
            var coder = new google.maps.Geocoder();
            coder.geocode({ 'latLng': latLng }, fillZipcode);
        },
        function() {
            alert("Could not determine your position.");
        }
    );
}   


function fillZipcode(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        if (results[1]) {
            var parts = results[1].address_components;  
            for(var i = 0; i < parts.length; i++) {
                if(parts[i].types[0] == 'postal_code') {
                    alert('Your Zip Code is: ' + parts[i].short_name);
                    return;
                }
            }
        } else {
            alert("Could not figure out your zip code.");
        }
    } else {
        alert("Geocoder failed due to: " + status);
    }
}
于 2012-06-13T13:14:36.537 回答
0

除了关于 watchbutton 的区域外,其余的在 Chrome 中对我来说都很好。

您不是要从 file:// URL 执行此操作,是吗?

于 2012-06-11T21:24:12.387 回答