[英]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.