Android: I want to obtain location using GPS in th

2019-06-12 14:17发布

问题:

One of the activities for my company's app has a directions utility that allows the user to input their postcode, choose one of our offices, and Maps will route from the given postcode to one of our offices.

Leaving the postcode field blank results in Maps defaulting to 'your location', however you need to click 'Go' in the Maps app for it to work, while providing a postcode will automatically do the routing without having to make that extra click.

This is a small but necessary thing that needs to be solved for ease of use purposes.

I understand that i will need to use locationManager etc, however i am unsure how to use it properly. A lot of the tutorials i've seen implement a full mapView, however i am simply wanting to obtain the users location via GPS, and geocode that into a postcode all in the background so the user doesn't actually see anything.

I'm sure it's possible, but what do i need to do?

Thanks.

回答1:

Here is the overview of location services in Android. Here is the LocationManager class at the heart of location services in Android.

Unfortunately, it does not appear that Android ships with any examples of the location API, which is somewhat surprising.

You can download the source code from this book -- look for the Internet/Weather and Service/WeatherPlus demos, each of which uses LocationManager. Or, you can download the source from this another book -- look for the 23-Location tutorial for some examples of using LocationManager.

This is another tutorial.



回答2:

And this is the one I used when doing the same: Obtaining User Location