首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >反转地理编码

反转地理编码
EN

Stack Overflow用户
提问于 2012-06-12 04:52:41
回答 3查看 354关注 0票数 0

我正在使用以下代码调用地图并进行显示。我想添加该函数来进行反向地理编码,并打印出相应的街道地址。我似乎不能将长/后的余弦传递给函数。如何将经纬度坐标转换为街道地址?

代码语言:javascript
复制
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;
        }
    }
}​
EN

回答 3

Stack Overflow用户

发布于 2012-06-12 05:24:12

除了关于手表按钮的部分,其余的对我来说在Chrome上都工作得很好。

您不是在尝试从file:// URL执行此操作,对吗?

票数 0
EN

Stack Overflow用户

发布于 2012-06-13 21:14:37

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

代码语言:javascript
复制
$(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);
    }
}
票数 0
EN

Stack Overflow用户

发布于 2012-09-28 03:11:19

如果您打算使用其他库,可以查看及其jquery插件的最终地理编码/位置示例。

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

代码语言:javascript
复制
// 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
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10987361

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档