2017-08-08 67 views
-1

我想将用户的经度和纬度坐标转换为国家和城市。目前,我正在使用darkskynet的API来查询坐标。我不知道我是否正确想到它,但我想通过使用坐标来查询和转换我的坐标,将坐标转换为国家名称和城市名称。任何建议或提示,不胜感激。如果还有另外一种获取这些信息的方法,我会很乐意提供任何建议。请原谅我可怜的嵌套。在此先感谢您的建议。下面是我的代码的副本:地理编码器 - 将坐标转换为国家和城市

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(); 
 
})
<script src="app.js"></script> 
 

 

 
<head> 
 
    <meta charset="utf-8" /> 
 
    <meta name="viewport" content="width=device-width, intitial-scale = 1" /> 
 
    <link rel="stylesheet" href="style.css" /> 
 
    <title>Weather</title> 
 
    <link href="https://fonts.googleapis.com/css?family=Monoton" rel="stylesheet"> 
 
    <script src=h ttps://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js></script> 
 
</head> 
 

 

 

 
<body> 
 
    <header> 
 
    <h1> 
 
     <div id="temp"></div> 
 
     <div id="minutely" id="tempunit"></div> 
 
    </h1> 
 
    <h2> 
 
     <div id="location"></div> 
 
    </h2> 
 
    </header> 
 
    <p><span id="city" </span><span id="country"></span></p> 
 
    <button class="btn btn-primary" id="BT1">Change Metric</button> 
 

 
</body>

+0

确实尝试您激活您的JS谷歌API? –

+0

'h ttps' <<那是什么?为什么你在那里放置app.js? ...很多错误... –

回答

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>