我正在尝试将经度和纬度坐标转换为用户的国家/地区和城市。目前,我正在使用darkskynet的API来查询坐标。我不确定我是否正确地思考它,但我想通过使用坐标来查询和转换我的坐标,将坐标转换为国家名称和城市名称。任何建议或提示都非常感谢。如果有另一种获取此信息的方式,我很乐意感谢任何建议。感谢我的糟糕筑巢。在此先感谢您的建议。以下是我的代码的副本:
ViewController

function weather() {
function success(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
location.innerHTML = "Latitude:" + latitude + "°" + "Longitude: " + longitude + '°';
var theUrl = url + apiKey + "/" + latitude + "," + longtitude + "?callback=?";
$.getJSON(theUrl, function(data) {
$("#temp").html(data.currently.temperature);
$("#minutely").html(data.minutely.summary)
function getWeather(latitude, longitude) {
$.ajax({
url: 'https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=AIzaSyBpiTf5uzEtJsKXReoOKXYw4RO0ayT2Opc',
dataType: 'jsonp',
success: function(results) {
$("#city").text(results.adress_components.long_name)
$("#country").text(results.sys.country)
}
});
}
});
}
var location = document.getElementById("location");
var apiKey = "3827754c14ed9dd9c84afdc4fc05a1b3";
var url = "https://api.darksky.net/forecast/";
navigator.geolocation.getCurrentPosition(success);
location.innerHTML = "Locating...";
}
$(document).ready(function() {
weather();
})

答案 0 :(得分:0)
确保 activated you JavaScript API 并尝试使用
var Geocoder = new google.maps.Geocoder;
function geocodeLatLng(latlng, cb) {
return Geocoder.geocode({
'location': latlng
}, function(res, status) {
return cb((status === 'OK') ? (res[1] || "Nothing found") : status);
});
}
// The ones you get from weather
var coordinates = {
lat: 45.8150,
lng: 15.9819
}
geocodeLatLng(coordinates, function(data) {
console.log(data.address_components[0].long_name); // City
console.dir(data.address_components[2].long_name); // Country
});
<script src="//maps.googleapis.com/maps/api/js?key="></script>