Reputation: 1889
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
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:
Location
will have reliable accuracy
values.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:
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