eve开发者_JAVA百科ryone. I am building an android application where I have obtain the latitude and longitude coordinates of a user's location when he/she clicks specific buttons in my application. I know the methods getLatitude() and getLongitude() are supposed to do this, but I am not sure as to how to use them correctly to get what I need. Could somebody shed some light on the matter so I can understand how to use these methods correctly please?? Any help would be much appreciated :)
It's not that simple. The Android device only looks for a location when you ask it to, so much of the time, the device will not know the location. So when you click the button and need a location, it may take a long time to get a location fix, especially if you're reliant on GPS in a city where buildings mean limited signal.
You can use the getLastKnownLocation() method which MAY provide a location, but often you will need to request a new one. The process is that you 1) request locations, 2) you then WAIT until you get one.
As the process is slow, you need to accept that either you get the location earlier (e.g. in the background when the app starts) or you accept there may be a significant delay when you request a location. One way to improve things is to request NETWORK locations (i.e. wifi or mobile network) as these often come quicker than GPS, but there's no guarantee (welcome to the wonderful world of mobile).
Read this page and you will understand much better how Android locations work: http://developer.android.com/guide/topics/location/obtaining-user-location.html
精彩评论