How to get weather at current location
Asked Answered
M

7

12

Possible duplicate:

Get weather update of current location?

How can I get the weather in current location from Latitude and Longitude?

I'm developing an app, it can update weather at current location. I have googled and found how to use Google API. But now Google API not available, and I used Yahoo weather instead.

My questions:

  1. How to get weather use Yahoo Weather API

  2. Tutorial or Source Code

Midge answered 15/3, 2013 at 5:19 Comment(0)
R
11

I have personally used this API its free and you can retrieve weather forecast for any location via postcode, zipcode or latitude and longitude

Free Local Weather REST API

**NOTE:**The Local Weather API returns weather data in XML, JSON and CSV format and contains weather elements like temperature, precipitation (rainfall), weather description, weather icon and wind speed.

**UPDATE:**The www.worldweatheronline.com/ api is not free anymore.

Try these

  1. accuweather.com
  2. https://openweathermap.org/
  3. www.apixu.com
Reach answered 15/3, 2013 at 5:36 Comment(2)
Thank Amitabh Sarkar so muchMidge
Amitabh Sarkar - is it a free one? I need to access local weather report by lat-log.Helenahelene
F
13

let me tell you what you should NOT use.. Google API ( it has been stopped )

http://thenextweb.com/google/2012/08/28/did-google-just-quietly-kill-private-weather-api/

Then, you have other options like

Yahoo Weather API

http://developer.yahoo.com/weather/

Eg.. To get the forecast for Paris, France, with degrees Celsius and other metric units: http://weather.yahooapis.com/forecastrss?w=615702&u=c

for other example visit the link http://developer.yahoo.com/weather/#examples

WunderGround Weather API

http://www.wunderground.com/weather/api/

Another Good Alternate is Open Weather Map API , it supports JSON output by passing just the latitude and longitude of the location.

http://openweathermap.org/wiki/API/JSON_API
Fluviatile answered 15/3, 2013 at 5:22 Comment(2)
@MahmoudFarahat that is up to you whichever solves your purpose is the bestFluviatile
Can you please suggest how to pass dynamic lat long in yahoo weather API. I am getting lat long of the current location and when I am trying to pass it in Yahoo weather API URL it is giving me 401 but when I pass static lat long it is giving me the result. URL : 'weather-ydn-yql.media.yahoo.com/forecastrss?lat=' + latitude + '&lon=' + longitude + '&format=json&oauth_consumer_key=' + Appconstants.info.WEATHER_CONSUMER_KEY + '&oauth_signature_method=HMAC-SHA1&oauth_timestamp=' + timeStamp + '&oauth_nonce=+myAuthNonce+&oauth_version=1.0&oauth_signature=+myauthSgnature' pls suggestBowse
R
11

I have personally used this API its free and you can retrieve weather forecast for any location via postcode, zipcode or latitude and longitude

Free Local Weather REST API

**NOTE:**The Local Weather API returns weather data in XML, JSON and CSV format and contains weather elements like temperature, precipitation (rainfall), weather description, weather icon and wind speed.

**UPDATE:**The www.worldweatheronline.com/ api is not free anymore.

Try these

  1. accuweather.com
  2. https://openweathermap.org/
  3. www.apixu.com
Reach answered 15/3, 2013 at 5:36 Comment(2)
Thank Amitabh Sarkar so muchMidge
Amitabh Sarkar - is it a free one? I need to access local weather report by lat-log.Helenahelene
A
2

Here's a collection of weather APIs that you can experiment with via your browser and then generate source code for in your favorite language:

https://live.temboo.com/library/keyword/weather/

Full disclosure: I work at Temboo.

Aligarh answered 15/3, 2013 at 13:40 Comment(0)
F
2

I have personally used OpenWeather API. its totally free and you can retrieve weather forecast for any location via city name, zipcode or latitude and longitude etc.

<?php
 $city    = 'london';
    $jsonfile    = file_get_contents( 'http://api.openweathermap.org/data/2.5/weather?q=' . $city . '&units=metric&lang=en&appid=c0c4a4b4047b97ebc5948ac9c48c0559' );
    $jsondata    = json_decode( $jsonfile );
    $temp        = $jsondata->main->temp;
    $pressure    = $jsondata->main->pressure;
    $mintemp     = $jsondata->main->temp_min;
    $maxtemp     = $jsondata->main->temp_max;
    $wind        = $jsondata->wind->speed;
    $humidity    = $jsondata->main->humidity;
    $desc        = $jsondata->weather[0]->description;
    $maind       = $jsondata->weather[0]->main;
    $currentTime = time();
    ?>
    <style>
    body {
        font-family: Arial;
        font-size: 0.95em;
        color: #929292;
    
    }
    
    .report-container {
        border: #E0E0E0 1px solid;
        padding: 20px 40px 40px 40px;
        border-radius: 2px;
        width: 550px;
        margin: 0 auto;
    }
    
    .weather-icon {
        vertical-align: middle;
        margin-right: 20px;
    }
    
    .weather-forecast {
        color: #212121;
        font-size: 1.2em;
        font-weight: bold;
        margin: 20px 0px;
    }
    
    span.min-temperature {
        margin-left: 15px;
        color: #929292;
    }
    
    .time {
        line-height: 25px;
    }
    </style>
    <body>
    <div class="report-container">
            <h2><?php echo $jsondata->name; ?> Weather Status</h2>
            <div class="time">
                <div><?php echo date( 'l g:i a', $currentTime ); ?></div>
                <div><?php echo date( 'jS F, Y', $currentTime ); ?></div>
                <div><?php echo $desc; ?></div>
            </div>
            <div class="weather-forecast">
                <img
                    src="http://openweathermap.org/img/w/<?php echo $jsondata->weather[0]->icon; ?>.png"
                    class="weather-icon" /> <?php echo $mintemp; ?>°C<span
                    class="min-temperature"><?php echo $maxtemp; ?>°C</span>
            </div>
            <div class="time">
                <div>Humidity: <?php echo $humidity; ?> %</div>
                <div>Wind: <?php echo $wind; ?> km/h</div>
            </div>
        </div>
        </body>
Frager answered 30/9, 2020 at 7:37 Comment(0)
D
1

OpenWeatherMap is the best weather open API that you can use: https://openweathermap.org/api

  • It provides free weather data and forecast API suitable for any cartographic services like web and smartphones applications
  • It uses JSON/XML formats to provide weather data for your app.
  • It offers a wide array of useful weather-related information including current weather, historical weather, forecast, wind, clouds, weather station data, and more
  • How can we get weather information for a selected location? Openweathermap provides different two modes to look for a city. One uses name pattern and another using geo-coordinates.
  • Free to use

Here is a sample app android-openweathermap-app

Distrustful answered 30/4, 2018 at 11:10 Comment(1)
Note that as of September 2022 it's not exactly free. You have to have billing information on file even to use the limited free access, and they charge (instead of capping) overuseWorkaday
V
0

You can get the Integration weather API in android but it need little bit of knowledge on parsing a xml response.

weather data to display current weather

Vernita answered 29/1, 2014 at 4:28 Comment(0)
J
0

Add to build.gradle

implementation 'com.github.androdocs:Simple-HTTP-Request:v1.0'

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_gradient"
android:orientation="vertical"
android:padding="25dp">

<RelativeLayout
    android:id="@+id/mainContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="visible">

    <LinearLayout
        android:id="@+id/addressContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:textColor="#FFFFFF"
            android:id="@+id/address"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="DHAKA, BD"
            android:textSize="24dp" />

        <TextView
            android:textColor="#FFFFFF"
            android:id="@+id/updated_at"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="20 April 2012, 20:08 PM"
            android:textSize="14dp" />
    </LinearLayout>


    <LinearLayout
        android:id="@+id/overviewContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">

        <TextView
            android:textColor="#FFFFFF"
            android:id="@+id/status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Clear Sky"
            android:textSize="18dp" />

        <TextView
            android:textColor="#FFFFFF"
            android:id="@+id/temp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:fontFamily="sans-serif-thin"
            android:text="29°C"
            android:textSize="90dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal">

            <TextView
                android:textColor="#FFFFFF"
                android:id="@+id/temp_min"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Min Temp: 05:05 AM" />

            <Space
                android:layout_width="50dp"
                android:layout_height="wrap_content" />

            <TextView
                android:textColor="#FFFFFF"
                android:id="@+id/temp_max"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Max Temp: 05:05 PM" />
        </LinearLayout>

    </LinearLayout>


    <LinearLayout
        android:id="@+id/detailsContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="3">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#3CF1EBF1"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="8dp">

                <ImageView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:src="@drawable/sunrise"
                    android:tint="#FFFFFF" />

                <Space
                    android:layout_width="wrap_content"
                    android:layout_height="5dp" />

                <TextView
                    android:textColor="#FFFFFF"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Sunrise"
                    android:textSize="12dp" />

                <TextView
                    android:textColor="#FFFFFF"
                    android:id="@+id/sunrise"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="06:40 AM"
                    android:textSize="14dp" />
            </LinearLayout>

            <Space
                android:layout_width="10dp"
                android:layout_height="wrap_content" />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#3CF1EBF1"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="8dp">

                <ImageView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:src="@drawable/sunset"
                    android:tint="#FFFFFF" />

                <Space
                    android:layout_width="wrap_content"
                    android:layout_height="5dp" />

                <TextView
                    android:textColor="#FFFFFF"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Sunset"
                    android:textSize="12dp" />

                <TextView
                     android:textColor="#FFFFFF"
                    android:id="@+id/sunset"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="06:40 AM"
                    android:textSize="14dp" />
            </LinearLayout>

            <Space
                android:layout_width="10dp"
                android:layout_height="wrap_content" />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#3CF1EBF1"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="8dp">

                <ImageView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:src="@drawable/wind"
                    android:tint="#FFFFFF" />

                <Space
                    android:layout_width="wrap_content"
                    android:layout_height="5dp" />

                <TextView
                    android:textColor="#FFFFFF"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Wind"
                    android:textSize="12dp" />

                <TextView
                    android:textColor="#FFFFFF"
                    android:id="@+id/wind"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="06:40 AM"
                    android:textSize="14dp" />
            </LinearLayout>
        </LinearLayout>

        <Space
            android:layout_width="wrap_content"
            android:layout_height="10dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="3">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#3CF1EBF1"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="8dp">

                <ImageView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:src="@drawable/pressure"
                    android:tint="#FFFFFF" />

                <Space
                    android:layout_width="wrap_content"
                    android:layout_height="5dp" />

                <TextView
                    android:textColor="#FFFFFF"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Pressure"
                    android:textSize="12dp" />

                <TextView
                    android:textColor="#FFFFFF"
                    android:id="@+id/pressure"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="06:40 AM"
                    android:textSize="14dp" />
            </LinearLayout>

            <Space
                android:layout_width="10dp"
                android:layout_height="wrap_content" />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#3CF1EBF1"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="8dp">

                <ImageView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:src="@drawable/humidity"
                    android:tint="#FFFFFF" />

                <Space
                    android:layout_width="wrap_content"
                    android:layout_height="5dp" />

                <TextView
                    android:textColor="#FFFFFF"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Humidity"
                    android:textSize="12dp" />

                <TextView
                    android:textColor="#FFFFFF"
                    android:id="@+id/humidity"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="06:40 AM"
                    android:textSize="14dp" />
            </LinearLayout>

            <Space
                android:layout_width="10dp"
                android:layout_height="wrap_content" />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#3CF1EBF1"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="8dp">

                <ImageView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:src="@drawable/info"
                    android:tint="#FFFFFF" />

                <Space
                    android:layout_width="wrap_content"
                    android:layout_height="5dp" />

                <TextView
                    android:textColor="#FFFFFF"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Created By"
                    android:textSize="12dp" />

                <TextView
                    android:textColor="#FFFFFF"
                    android:id="@+id/about"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="AndroDocs"
                    android:textSize="14dp" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>


<ProgressBar
    android:id="@+id/loader"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:visibility="gone" />

<TextView
    android:id="@+id/errorText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="Something went wrong"
    android:visibility="gone" />

 public class WeatherActivity extends AppCompatActivity {
//    String CITY = "surat";
String API = "8118ed6ee68db2debfaaa5a44c832918";
String locality;
 TextView addressTxt, updated_atTxt, statusTxt, tempTxt, temp_minTxt, temp_maxTxt, 
sunriseTxt,   sunsetTxt, windTxt, pressureTxt, humidityTxt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (Build.VERSION.SDK_INT >= 21) {
        getWindow().clearFlags(67108864);
        getWindow().setStatusBarColor(ContextCompat.getColor(this, 
R.color.weatheract));
    }
    setContentView(R.layout.activity_weather);

    addressTxt = findViewById(R.id.address);
    updated_atTxt = findViewById(R.id.updated_at);
    statusTxt = findViewById(R.id.status);
    tempTxt = findViewById(R.id.temp);
    temp_minTxt = findViewById(R.id.temp_min);
    temp_maxTxt = findViewById(R.id.temp_max);
    sunriseTxt = findViewById(R.id.sunrise);
    sunsetTxt = findViewById(R.id.sunset);
    windTxt = findViewById(R.id.wind);
    pressureTxt = findViewById(R.id.pressure);
    humidityTxt = findViewById(R.id.humidity);

    LocationManager locationManager = (LocationManager) 
getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    if (ContextCompat.checkSelfPermission(this, 
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
            && ContextCompat.checkSelfPermission(this, 
Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

        Location location = 
locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, 
false));
        if (location != null) {
            getCity(location.getLatitude(), location.getLongitude());
        }
    }

}

private final void getCity(double d2, double d3) {
    List list;

    try {
        list = new Geocoder(getApplicationContext(), 
Locale.getDefault()).getFromLocation(d2, d3, 1);
        if (list != null && (!list.isEmpty())) {
            locality = ((Address) list.get(0)).getLocality();
            new weatherTask().execute();

        }
    } catch (NullPointerException e2) {

    } catch (IOException e) {
        e.printStackTrace();
    }
}

class weatherTask extends AsyncTask<String, Void, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        /* Showing the ProgressBar, Making the main design GONE */
        findViewById(R.id.loader).setVisibility(View.VISIBLE);
        findViewById(R.id.mainContainer).setVisibility(View.GONE);
        findViewById(R.id.errorText).setVisibility(View.GONE);
    }

    protected String doInBackground(String... args) {
        String response = 
HttpRequest.excuteGet("https://api.openweathermap.org/data/2.5/weather?q=" + locality 
+ "&units=metric&appid=" + API);
        return response;
    }

    @Override
    protected void onPostExecute(String result) {


        try {
            JSONObject jsonObj = new JSONObject(result);
            JSONObject main = jsonObj.getJSONObject("main");
            JSONObject sys = jsonObj.getJSONObject("sys");
            JSONObject wind = jsonObj.getJSONObject("wind");
            JSONObject weather = jsonObj.getJSONArray("weather").getJSONObject(0);

            Long updatedAt = jsonObj.getLong("dt");
            String updatedAtText = "Updated at: " + new SimpleDateFormat("dd/MM/yyyy 
hh:mm a", Locale.ENGLISH).format(new Date(updatedAt * 1000));
            String temp = main.getString("temp") + "°C";
            String tempMin = "Min Temp: " + main.getString("temp_min") + "°C";
            String tempMax = "Max Temp: " + main.getString("temp_max") + "°C";
            String pressure = main.getString("pressure");
            String humidity = main.getString("humidity");

            Long sunrise = sys.getLong("sunrise");
            Long sunset = sys.getLong("sunset");
            String windSpeed = wind.getString("speed");
            String weatherDescription = weather.getString("description");

            String address = jsonObj.getString("name") + ", " + 
sys.getString("country");


            /* Populating extracted data into our views */
            addressTxt.setText(address);
            updated_atTxt.setText(updatedAtText);
            statusTxt.setText(weatherDescription.toUpperCase());
            tempTxt.setText(temp);
            temp_minTxt.setText(tempMin);
            temp_maxTxt.setText(tempMax);
            sunriseTxt.setText(new SimpleDateFormat("hh:mm a", 
Locale.ENGLISH).format(new Date(sunrise * 1000)));
            sunsetTxt.setText(new SimpleDateFormat("hh:mm a", 
Locale.ENGLISH).format(new Date(sunset * 1000)));
            windTxt.setText(windSpeed);
            pressureTxt.setText(pressure);
            humidityTxt.setText(humidity);

            /* Views populated, Hiding the loader, Showing the main design */
            findViewById(R.id.loader).setVisibility(View.GONE);
            findViewById(R.id.mainContainer).setVisibility(View.VISIBLE);


        } catch (JSONException e) {
            findViewById(R.id.loader).setVisibility(View.GONE);
            findViewById(R.id.errorText).setVisibility(View.VISIBLE);
        }

    }
}


}
Jaguar answered 8/5, 2020 at 11:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.