【问题标题】:How do I get the city name out of this?我如何从中获得城市名称?
【发布时间】:2016-04-18 18:01:06
【问题描述】:

我在 html 中有这个

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
<title>Geolocation</title>
<script   src="https://code.jquery.com/jquery-2.2.3.min.js"   integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo="   crossorigin="anonymous"></script>
<script src="funciones.js"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> 
</head>
<body>
<button id="startGeo">Click here to check your geolocation abilities</button>
</body>
</html>

我在 function.js 文件中有这个:

$(document).ready(function(){
$("#startGeo").click(checkLocation);

function checkLocation(){
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(getLocation,locationFailed);
        //document.write("you have geolocation");
    }
    else {
        document.write("you don't have geolocation");
    }
}//ends checkLocation()

function getLocation(position){
    var latitud = position.coords.latitude;
    var longitud = position.coords.longitude;
    var exactitud = position.coords.accuracy;

    alert("latitud: " + latitud + " longitud: " + longitud + " exactitud: " + exactitud);
    //document.write("we received your location");
}   
function locationFailed(){
    document.write("we didn't get your location. Please check your settings");
}
});

从中我得到坐标,但我完全不知道如何获得城市名称......或州名。我看到了一个与此类似的问题,但他们似乎正在使用 json。我不想要地图,只想要信息,它可以是文本或警报。请问有什么想法吗?

【问题讨论】:

    标签: javascript api geolocation location


    【解决方案1】:

    您可以使用反向地理编码 API

    示例调用:

    http://maps.googleapis.com/maps/api/geocode/json?latlng=50.123413,-22.12345&sensor=true

    您可以像这样替换您的 getLocation 函数(在访问 data.results[2] 之前进行适当的测试):

    function getLocation(position){
        var latitud = position.coords.latitude;
        var longitud = position.coords.longitude;
        var exactitud = position.coords.accuracy;
    
        $.get('https://maps.googleapis.com/maps/api/geocode/json?latlng=' + latitud + ',' + longitud + '&sensor=true', function(data) {
            alert(data.results[2].address_components[0].long_name);
        });
    
        //alert("latitud: " + latitud + " longitud: " + longitud + " exactitud: " + exactitud);
        //document.write("we received your location");
    }
    

    您可以检查以下小提琴以查看它的实际效果:

    https://jsfiddle.net/5tzxks10/

    【讨论】:

    • 非常感谢您。确实感谢您的帮助,我获得了信息,但是错误的信息给了我一个我不知道的地址。这可能是api中的json有问题吗?我运行了其他代码,它根据我使用的浏览器给了我不同的位置。我想知道这是否与 data.results[2] 有关。如果是这样,我怎么知道该使用哪一个?
    • 确实要看数据结果[2]我这里放2只是举例
    • 请查看 API 文档以更好地了解响应:
    • 谢谢你,我明白多了。但是(我在回答中问过这个问题,但我知道它是否会影响到你)我在哪里可以看到不同的结果?有页面吗?
    【解决方案2】:

    +Nowres Rafed 我稍微修改了你的代码:

    function getLocation(position){
    var latitud = position.coords.latitude;
    var longitud = position.coords.longitude;
    var exactitud = position.coords.accuracy;
    
    $.get('https://maps.googleapis.com/maps/api/geocode/json?latlng=' + latitud + ',' + longitud, function(data) {
        alert(data.results[1].formatted_address);
    });
    
    //alert("latitud: " + latitud + " longitud: " + longitud + " exactitud: " + exactitud);
    //document.write("we received your location");
    

    它以这种方式提供更多信息。太感谢了。我想知道(如果可能的话)是我在哪里可以阅读或知道如何获取特定答案,我读到当您获得结果时,它会返回多个结果。我改变了你提到的数字,测试了不同的选项。但是,有什么地方可以找到这些结果吗?请原谅我的麻烦,我是新手。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-26
      相关资源
      最近更新 更多