Imri Persiado
Imri Persiado

Reputation: 1889

Using the getAccuracy wrong

I'm building a location based application and I would like to avoid from this kind of location: "Tel Aviv, Tel Aviv, Israel" which is very not accurate.

I use the following condition before using a location:

if(!(this.location == null || this.location.getAccuracy() > 700))
    // use the location

But I keep getting this locations (Tel Aviv, Tel Aviv, Israel), and Tel Aviv is not small enough to pass this condition.

I'm missing something? What's wrong?

Upvotes: 1

Views: 630

Answers (1)

andr
andr

Reputation: 16064

You haven't specified a couple of parameters involved but I'll give it a try.

The exact Location parameters depend on the source of that particular Location, i.e. how it was obtained and using what geolocation source(s). For example:

  • If you use GPS, the Location will have reliable accuracy values.
  • If you use cellular network radio stations (BTS), the accuracy you'll receive will be limited to the database of BTS around the world, their location and range.
  • If you use Wi-Fi networks and their reported signal strength, the accuracy will depend on the quality of the database of the Wi-Fi networks. Unfortunately such databases are not cheap to maintain and create. For example Google buys such databases and uses Google Street View cars for scanning such networks.
  • If you use localization based on public IP address, your accuracy will be limited to a town, because that is the most accurate information contained in IANA IP address space registry. Also, it can be fooled by using a proxy servers.

The accuracy you'll receive from a location service represents a distance (in meters) from the reported location in which the device is likely to be in the moment the location was obtained.

So (if I understand your question correctly), I presume your using a low-accuracy method of obtaining current device location. You simply must switch to a source of Location with greater accuracy.

Please also note that the location-acquiring method (i.e. the service used to find the location as described above) used by the location service might actually not be able to provide accuracy for returned Location. To understand what I mean by that consider a following scenario and their two outcomes - note that they both come from the same locating method based on Wi-Fi networks:

  1. You are in range of several Wi-Fi networks. You ask the location service about device location. All Wi-Fi networks in range happen to be in location service DB. The location service is thus able to get your location with decent accuracy.
  2. You are in range of several Wi-Fi networks. However, none of them happens to be well known to location service in that their origin (town) is known, but their exact location and range is not known. In that case the location service is able to tell you which country/town you are in, but has no idea on your precise location. In turn, location service can give you a location with no accuracy.

You must make yourself prepared for such an occasion by testing if the accuracy is available at all. You can do this by issuing Location.hasAccuracy(). If the returned value is false, you can't call Location.getAccuracy() (actually you can, but beware - it'll return 0!). So, your condition should be more like:

if (this.location != null && this.location.hasAccuracy() && this.location.getAccuracy() < 700) {
    // do something
}

Upvotes: 3

Related Questions