简体   繁体   English

如何从中获得城市名称?

[英]How do I get the city name out of this?

I hhave this in the html 我在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>

and I have this in a function.js file: 我在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");
}
});

from this I get the coordinates, but I have no idea at all on how to get the city name... or the state name. 由此我得到了坐标,但是我完全不知道如何获得城市名...或州名。 I saw a question similar to this answered but they seemed to be using json. 我看到了一个与此答案类似的问题,但他们似乎正在使用json。 I don't want a map, just the info, it can be text or an alert. 我不想要地图,仅想要信息,它可以是文本或警报。 Please any ideas? 有什么想法吗?

You can use reverse Geocoding API 您可以使用反向地址解析API

Sample call: 样品电话:

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

You can replace your getLocation function like that (make appropriate tests before accessing data.results[2]): 您可以像这样替换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");
}

You can check the following fiddle to see it in action: 您可以检查以下小提琴以查看其作用:

https://jsfiddle.net/5tzxks10/ https://jsfiddle.net/5tzxks10/

+Nowres Rafed I changed your code a little bit: + 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");

it gives more information that way. 这样可以提供更多信息。 Thank you so much. 非常感谢。 What I would like to know (if it's possible) is where can I read or know how to fetch a specific answer, I read that when you get the results, it returns more than one result. 我想知道(如果可能)是在哪里可以阅读或知道如何获取特定答案,我读到当您获得结果时,它将返回多个结果。 I changed the number as you mentioned, testing the different options. 正如您提到的,我更改了号码,测试了不同的选项。 But, is there somewhere where I can look for those results? 但是,在哪里可以找到这些结果? forgive the bother, I'm just new to this. 原谅我,这是我的新手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用纬度和经度获取位置名称或城市? - How do I get location name or city using latitude & longitudes? 如何从地址解析器中仅获取城市名称? - How can I get only the city name from geocoder? 如何从经纬度点获取城市名称? - How can I get city name from a latitude and longitude point? 我如何从三个按钮中获取 2 个的类名? - How do i get the class name of 2 out of three buttons? 如何使用地理位置获取城市名称并重定向到该城市的URL? - How to get city name using geolocation and redirect to an URL for that city? 如何在Google地图中通过IP获取城市名称? - How to get city name by ip in google map? 我怎样才能从纬度和经度得到一个带有国家、城市和街道名称的字符串 - how can i get a string with country, city and street name from Latitude and Longitude 如何从节点 express mongoose mongoDb 中的地理位置数据中获取城市名称 - How can I get city name from geolocation data in node express mongoose mongoDb 如何使用此处的反向地理编码从所有检索到的数据中获取城市名称? - How can I get the name of the city from all the retrieved data using here reverse-geocoding? 如何通过许可获取用户城市名称位置并将该城市名称位置发送到后端? - How to get user city name location by permission and than send that city name location to backend?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM