将纬度和日志作为天气 API 中的输入传递的任何选项

any option in passing lat and log as a input in a weather api

本文关键字:输入 选项 任何 API 纬度 日志      更新时间:2023-09-26

>我正在使用天气API。 我想将纬度和经度从地理位置页面传递到 API,以便它获取当前天气。 有没有任何选择来实现这一点。请帮忙。您的帮助将不胜感激。 这是我的JS代码:

function SearchLocation(e) {
    var searchLocationInput = {
        query: 'Chennai',
        format: 'JSON',
        timezone: 'yes',
        popular: '',
        num_of_results: '',
        callback: 'SearchLocationCallback'
    };
    JSONP_SearchLocation(searchLocationInput);
    e.preventDefault();
}
function SearchLocationCallback(searchLocation) {
    output = "<br/> Area Name: " + locationSearch.search_API.result[0].areaName[0].value;
    output += "<br/> Country: " + locationSearch.search_API.result[0].country[0].value;
    output += "<br/> Latitude: " + locationSearch.search_API.result[0].latitude;
    output += "<br/> Longitude: " + locationSearch.search_API.result[0].longitude;
    output += "<br/> Population: " + locationSearch.search_API.result[0].population;
    output += "<br/> Region: " + locationSearch.search_API.result[0].region[0].value;
    resultContainer.empty();
    resultContainer.html(output);
}

这是我的地理位置代码:

     <html>
   <head>
     <title>Getting Geolocation for Weather Details</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
       <meta charset="UTF-8">
    <style type="text/css">
     html, body {
      height: 100%;
      margin: 0;
      padding: 0;
     }
      #map_canvas {
      height: 100%;
     }
     @media print {
       html, body {
        height: auto;
      }
       #map_canvas {
      height: 650px;
      }
     }
    </style>
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=true"></script>
    <script type="text/javascript">
     var map;
      function initialize() {
       var myOptions = {
        zoom: 16,
         mapTypeId: google.maps.MapTypeId.ROADMAP
       };
       map = new google.maps.Map(document.getElementById('map_canvas'),
        myOptions);
       // Try HTML5 geolocation
       if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
        var pos = new google.maps.LatLng(position.coords.latitude,
                                         position.coords.longitude);
          var infowindow = new google.maps.InfoWindow({
          map: map,
          position: pos,
          content: '<a href="weatherdetails.html">click here for weather details</a>'
           });
           map.setCenter(pos);
         }, function() {
           handleNoGeolocation(true);
         });
         } else {
        // Browser doesn't support Geolocation
        handleNoGeolocation(false);
        }
    }
     function handleNoGeolocation(errorFlag) {
       if (errorFlag) {
        var content = 'Error: The Geolocation service failed.';
       } else {
         var content = 'Error: Your browser doesn''t support geolocation.';
       }
       var options = {
      map: map,
       position: new google.maps.LatLng(60, 105),
      content: content
      };
      var infowindow = new google.maps.InfoWindow(options);
     map.setCenter(options.position);
    }
    google.maps.event.addDomListener(window, 'load', initialize);
   </script>
   </head>
    <body>
    <div id="map_canvas"></div>
    </body>
  </html>

此 API 有据可查,传递纬度和经度,通过 q -参数用逗号分隔。

简单的实现:

if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      $.getJSON('http://api.worldweatheronline.com/free/v1/weather.ashx?callback=?',
                {format:'json',
                 key:'yourKey',
                 q:[position.coords.latitude, position.coords.longitude].join(',')
                },
                function(r){
                  alert(r.data.current_condition[0].weatherDesc[0].value);
                })
    });
}

演示:http://jsfiddle.net/doktormolle/xzxZb/