Get weather update of current location? - android

What I have: Latitude and Longitude.
What I want: Get weather update for these coordinates.

If you want to use the Google Weather API, you'll have to pass it either a City, State or a Zip code. To do this, you'll need to GeoCode your lat/long to get this info.
Here's the URL to the Google Weather API: http://www.google.com/ig/api?weather=Seattle,WA
Here's a sample code to take lat/long and convert to zip:
Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(latitude, longitude, 3);
} catch (IOException ex) {
//Handle IOException
}
for (int i = 0; i < addresses.size(); i++) {
Address address = addresses.get(i);
if (address.getPostalCode() != null)
String zipCode = address.getPostalCode();
}
Then pass the Zip Code (Or City, State) to the Google Weather API and parse the returning XML. Good luck!

google API is down, so you should look for an alternative.
http://openweathermap.org/api

You need to use an API. You'll have to do some research on your own, here's one good one.
http://www.weather.gov/xml/

Related

Geocoder does not work for certain addresses

I am using the goecoder to try to convert address to lat/lng. However, it seems the geocoder works for some addresses but not others. If I search the address on google maps then it pinpoint it perfectly.
Here is an example 38 Crichton Street, Ottawa, ON, Canada
This address returns 0 results when I run the code below
Location loc = null;
Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> address = geoCoder.getFromLocationName(addr, 1);
if (address.size() == 0) {
return null;
}
double latitude = address.get(0).getLatitude();
double longitude = address.get(0).getLongitude();
loc = new Location("Unknown");
loc.setLatitude(latitude);
loc.setLongitude(longitude);
} catch (IOException ioException) {
Log.e(TAG, ioException.toString());
} catch (IllegalArgumentException illegalArgumentException) {
Log.e(TAG, illegalArgumentException.toString());
}
Any idea why or how I can fix it
Thank you
Not sure why native Android API geocoder cannot find this address. I can see that this address is found in Geocoding API web service:
https://maps.googleapis.com/maps/api/geocode/json?address=38%20Crichton%20Street%2C%20Ottawa%2C%20ON%2C%20Canada&key=MY_API_KEY
You can also see it in Geocoder tool:
https://google-developers.appspot.com/maps/documentation/utils/geocoder/#q%3D38%2520Crichton%2520Street%252C%2520Ottawa%252C%2520ON%252C%2520Canada
As a workaround you can consider using the Geocoding API web service. Please note that there is a Java client library for web services that you can find on Github:
https://github.com/googlemaps/google-maps-services-java
The Javadoc for client library is located at
https://googlemaps.github.io/google-maps-services-java/v0.2.5/javadoc/
I hope this helps!

Android Geocoder returns null city name

I am new to Android development, following is my code about use Geocoder to get city name of current location, it returns null:
private void updateCurrentLocation(Location location) {
double lat = 0.0, lng = 0.0;
if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
Log.i("tag", "Latitute is" + lat + ", Longtitute is" + lng);
} else {
City_Name = "Unavailable";
}
List<Address> list = null;
Geocoder geocoder = new Geocoder(this.getActivity());
try {
list = geocoder.getFromLocation(lat, lng, 1);
} catch (IOException e) {
e.printStackTrace();
}
//may provide multiple locations.
if (list != null && list.size() > 0) {
Address address = list.get(0);
City_Name = address.getLocality();
}
Log.i("Try", "CityName:" + City_Name);
//send empty message
handler.sendEmptyMessage(1);
}
I opened GPS services, add ACCESS_FINE_LOCATION and INTERNET permission in Manifest already. Also, I searched similar questions in Stackoverflow about Geocoder returns null, but haven't found useful solutions. One of them is analyze JSON from Geocoder website, but it doesn't work either.
Can anyone help with this? Thank you!
BTW, is there any better solution to receive a city name? Thank you!
If the "getFromLocation" method gives you an empty set then it's because the server that is being looked up doesn't have the address information for the coordinates you're passing it. This is also noted in the docs. So I think that you should let it go and use another service like the Google Maps geocoding service or another one like Nominatim from the OpenStreetMap project.

How to get continuous Location (Lat and Lng) along with location address in same project?

I want to show a continuous update of Lat and long along with location address .
I was able to receive continuous lat/lng but wasn't able to find the location address .
Basically , want to combine these two :
https://developer.android.com/training/location/receive-location-updates.html
https://developer.android.com/training/location/display-address.html
How can I achieve this?
Thanks.
Hi you can get a location address using Geocoder class
Geocoder gcd = new Geocoder(this, Locale.getDefault());
List<Address> addresses;
String city;
try {
addresses = gcd.getFromLocation(lat,lon, 1);
if (addresses.size() > 0)
city = addresses.get(0).getLocality();
} catch (IOException e) {
e.printStackTrace();
}

How to get Full address from lat/lng using Google Play Service ( Not use Geocoder)

I have a position at 10.792986, 106.670004.
How to get FULL address like as :
207 Lê Văn Sỹ, 14, Phú Nhuận
Hồ Chí Minh, Vietnam
If using Geocoder, I couldn't get Full Adress.
List<Address> addresses = null;
String addressText="";
try {
addresses = geocoder.getFromLocation(latitude, longitude,1);
} catch (IOException e) {
e.printStackTrace();
}
So, is there any way to use Gooogle Play Service ( ex: GoogleApiClient) to get full address from lat /lng Or get placeId from lat/lng.
I mean, what is Android Api class for
Reverse Geocoding for a Latitude/Longitude
Have you try with googleapis.com, geocode get location from lat an lng
simply try to pass your lat and long to the following query string, and you will get a json array, fetch your city from there
http://maps.googleapis.com/maps/api/geocode/json?latlng=10.792986,106.670004&sensor=true

get latitude and longitude using zipcode

i want to get the latitude and longitude using the zipcode for android application
This is a much simpler solution, assuming the geocoder services are present:
final Geocoder geocoder = new Geocoder(this);
final String zip = "90210";
try {
List<Address> addresses = geocoder.getFromLocationName(zipCode, 1);
if (addresses != null && !addresses.isEmpty()) {
Address address = addresses.get(0);
// Use the address as needed
String message = String.format("Latitude: %f, Longitude: %f",
address.getLatitude(), address.getLongitude());
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
} else {
// Display appropriate message when Geocoder services are not available
Toast.makeToast(this, "Unable to geocode zipcode", Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
// handle exception
}
There is no rival of Google in GeoCoding :)
You have to just make an http Get request for the following link.
Here's a link
Just change the postal code Element According to your requirements. And Enjoy.
For further reference follow the link.Maps Geocoding Reference
You could use YQL like so: select centroid from geo.places where text="ZIPCODE_HERE"
You will have to use a Geocoding Service, like the ones provided by Google or the freely available Nominatim. Note that since you will be just providing a ZIP code, you might not get the results you want due to the inaccuracy of the address itself.

Categories

Resources