0

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) {
  • `'ve tried comparing only 3 decimal places for each latitude and longitude,` That is ok. Why would that not work? – greenapps May 18 '18 at 10:16
  • Further you are asking to solve two problems. Better was to concentrate on comparing lat,lon only. – greenapps May 18 '18 at 10:16
  • Pseudocode: `delta = abs(deliveryLatitude - endLatitude) + abs(deliveryLongitude - endLongitude)`. Create one delta for each json file you have, sort them in ascending order to list matches nearest first. No? – Timir May 18 '18 at 10:20
  • Comparing floating point values is not normally done by checking for equality but by checking whether they are within some small value *epsilon* of one another. Or you can use some geospatial library to calculate the distance between them on the Earth's surface. – David Conrad May 18 '18 at 10:29

3 Answers3

2

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.

0

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.

George K
  • 481
  • 2
  • 13
0

For this kind of tasks geoHash is very usefull. There are several open source geoHash libs you can use. For example.

  • Point A(x1,y1), geohash = abcdef
  • Point B(x2,y2) geohash = abcdoz

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

Danila Zharenkov
  • 1,720
  • 1
  • 15
  • 27
  • Interesting, I'venever heard of a geoHash before. On your second suggestion, wouldn't rounding the coordinates be a much quicker way of checking if they are close enough to each other? – George K May 18 '18 at 10:38
  • @GeorgeK it will be easier, but less accurate I think. – Danila Zharenkov May 18 '18 at 11:10