Reputation: 5
Background: I'm trying to use google maps API and I've got to the point where I can get data from json files. I'm trying to update my dataset with the json file's information, but I'm having difficulties.
You'll be given an address range or street and a Latitude Longitude from the google's json files, and I need to check which address is corresponding to which json address.
The json file will have multiple blocks of code like what you see below:
"distance" : {
"text" : "999 ft",
"value" : 999
},
"duration" : {
"text" : "999 min",
"value" : 999
},
"end_address" : "3722-3788 Street, City, ST 11111, USA",
"end_location" : {
"lat" : 40.0000000,
"lng" : 100.0000000
},
My data will have Latitude, Longitude and an address where I can compare to see if my addresses are part of the json's addresses. The problem is, My data will have exactly where a house address is, while Google's (above) will have approximate/ the street they reside on.
Problem: My data will sometimes have a longitude of 40.0005001, latitude 99.799999 (which are close, but not equal) and an address of 3750 Street, City, ST 11111, USA (which is in the within the bounds, but not exactly the same).
Q: How do I check if my the json's end_address is close enough to my actual address? I don't need the house numbers exactly, mostly just the right street name, etc. (Note: all the fields we'll be comparing are strings, but can be converted to double if needed.)
I've tried comparing only 3 decimal places for each latitude and longitude, but it's not getting the comparisons right. Each would be lat/lng below would be rounded.
if (deliveryLatitude == endLatitude && deliveryLongitude == endLongitude) {
Upvotes: 0
Views: 1977
Reputation:
If your data is stored on some database server check for geospatial support. You may have built-in functions to query over geospatial data that should be really efficient.
The simplest solution I can think of is to check whether some point A is close enough to some point B on the planet Earth. So you pass point A and point B to this:
https://stackoverflow.com/a/16794680/9810761
or this java function
https://stackoverflow.com/a/3694410/9810761
And check whether distance is below some threshold/epsilon like maybe 10 meters - experiment with that.
Upvotes: 2
Reputation: 1863
For this kind of tasks geoHash is very usefull. There are several open source geoHash libs you can use. For example.
The main idea of geoHash is: the more literals in the begining of geoHash are same - the closer these points are.
Or you can calculate distance between these two points and define how far they are. For example ESRI geometry API
has methods for distance calculations on Earth
Upvotes: 0
Reputation: 499
You should round your exact values to a certain degree to check if google's approximate values match. How much you round your values depends on how accurate results you need. The more rounding the more the loss of accuracy.
You could use JavaScript's floor() method.
Upvotes: 0