2017-08-08 13 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 Googleに有効あなたは確かにしていることを確認しますAPI? –

+0

'h ttps' << what is that?なぜそこに '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>

関連する問題