What is the simplest and most robust way to get th

2018-12-30 23:31发布

The LocationManager API on Android seems like it's a bit of a pain to use for an application that only needs an occasional and rough approximation of the user's location.

The app I'm working on isn't really a location app per se, but it does need to get the user's location in order to display a list of nearby businesses. It doesn't need to worry about if the user is moving around or anything like that.

Here's what I'd like to do:

  1. Show the user a list of nearby locations.
  2. Preload the user's location so that by the time I need it in Activity X, it will be available.
  3. I don't particularly care about accuracy or frequency of update. Just grabbing one location is sufficient as long as it's not way off. Maybe if I want to be fancy I'll update the location once every few mins or so, but it's not a huge priority.
  4. Work for any device as long as it has either a GPS or a Network Location provider.

It seems like it shouldn't be that hard, but it appears to me that I have to spin up two different location providers (GPS and NETWORK) and manage each's lifecycle. Not only that, but I have to duplicate the same code in multiple activities to satisfy #2. I've tried using getBestProvider() in the past to cut the solution down to just using one location provider, but that seems to only give you the best "theoretical" provider rather than the provider that's actually going to give you the best results.

Is there a simpler way to accomplish this?

26条回答
听够珍惜
2楼-- · 2018-12-31 00:34

A bit late here but what I would in such a situation is to use Google Maps API and mark the nearby locations using lat and long API of google maps. Plus user experience is better if you can show his/her location on a map. No need to bother about the updation of user location or frisking with android api. Let google maps handle the internals for you.

@emmby may have got it resolved in his app but for future reference a look at Google maps API for location specific stuff is what I recommend to fellow developers.

Edit: Link for displaying user location in google maps

查看更多
何处买醉
3楼-- · 2018-12-31 00:36

I am not sure if the Location-Based Services can get the location from other infrastructures other than GPS, but according to that article, it does seem possible:

Applications can call on any of several types of positioning methods.

Using the mobile phone network: The current cell ID can be used to identify the Base Transceiver Station (BTS) that the device is communicating with and the location of that BTS. Clearly, the accuracy of this method depends on the size of the cell, and can be quite inaccurate. A GSM cell may be anywhere from 2 to 20 kilometers in diameter. Other techniques used along with cell ID can achieve accuracy within 150 meters.

Using satellites: The Global Positioning System (GPS), controlled by the US Department of Defense, uses a constellation of 24 satellites orbiting the earth. GPS determines the device's position by calculating differences in the times signals from different satellites take to reach the receiver. GPS signals are encoded, so the mobile device must be equipped with a GPS receiver. GPS is potentially the most accurate method (between 4 and 40 meters if the GPS receiver has a clear view of the sky), but it has some drawbacks: The extra hardware can be costly, consumes battery while in use, and requires some warm-up after a cold start to get an initial fix on visible satellites. It also suffers from "canyon effects" in cities, where satellite visibility is intermittent.

Using short-range positioning beacons: In relatively small areas, such as a single building, a local area network can provide locations along with other services. For example, appropriately equipped devices can use Bluetooth for short-range positioning.

查看更多
登录 后发表回答