Reputation: 83
I am fairly new to developing android apps. What I am trying to do is a button click event which will find your current location. It will will then display the name of the city and the current weather. I am planning on using weather2 as an API. Anyway i havent got the faintest idea to do this i have searched tutorials and all i can find is the use of the location manager which results in the latatude and longatude along with a google map. Could anyone tell me how i could use the gps to use the API instead?
Upvotes: 0
Views: 354
Reputation: 7672
Your question isn't entirely clear. You can't use the GPS to use an API, you write code to use an API...
That being said, it looks like using weather2 requires simply that you send an HTTP GET request to it. To actually call into their client you will use an HttpClient (included in Android's library). You can find some bits of example code to do this here:
timewasted dot net/?p=127
The query you give it will follow these guidelines:
http://www.myweather2.com/developer/apis.aspx?uref=becda844-8299-4bf6-899b-d771a92b9dbf
One of the parameters that you can send to this API is your current longitude and latitude. You can use the locationmanager to obtain this. You will do a Context.getSystemService to get a LocationManager object, and then request that it inform you where you are. You might also resort to using LocationManager.getLastKnownLocation if the update never happens (this could happen, for instance, if the user is inside a building). You can find a simple LocationManager tutorial here:
blogoscoped dot com/archive/2007-11-19-n27.html
The Location object that you get back in your listener will tell you the latitude and longitude that you want, and you can package this up as a query to send to weather2. For example, here is a query string you could send to weather2 (using the HttpClient and GET request I mentioned above):
http://www.myweather2.com/developer/forecast.ashx?uac=&query=24.15,56.32&temp_unit=f
Here you could replace those coordinates with the ones you get from your LocationManager instead.
Remember that the link I gave is not entirely correct. You should not perform network operations in the main thread of an application. Instead you should perform in something like an AsyncTask, a Service, etc.. and then use a Messenger to talk between the two (using AsyncTask simplifies this, however..)
Sorry, you'll have to replace the 'dot's with actual periods, as I have a low reputation :-)
Upvotes: 2