• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Coding an app to retrieve a stored map based on geolocation

Contributor ,
Feb 21, 2020 Feb 21, 2020

Copy link to clipboard

Copied

I've created a web site that allows people to retrieve maps for apartment complexes. It works fine, but they need to know certain information for a search, like the name, address, bounding streets, etc. I'd like to let them click one button, once they're on or very near the property to retrieve the appropriate map.

 

Could someone share a high-level view of no more than a few bullet points to guide me on the software and/or CF coding needed to do this? Google searches drive me nuts, for reasons I'm sure I don't have to explain. I'm thinking that I could store the GPS coordinates for each property in my database. How do I make the connection between those and my user's phone?

 

You can see the site here: http://aptmaps.us. It covers only a small geographic area at the moment.

 

Thanks!

John

TOPICS
Advanced techniques

Views

1.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 21, 2020 Feb 21, 2020

Copy link to clipboard

Copied

Let me start the list:

  • Store lat/long coordinates for each record in my maps table
  • Use some software combo to retrieve my user's current lat/long
  • Use those coordinates to query my database —I also need a way for my search to get "near" my stored values, as my user's and my database's values will not match. 
  • I don't need to display maps from google or anyone else. Just need current coordinates to plug into a search form against my own data.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 24, 2020 Feb 24, 2020

Copy link to clipboard

Copied

What database brand do you have?

You said:

  • Use those coordinates to query my database —I also need a way for my search to get "near" my stored values, as my user's and my database's values will not match. 

I would do the following instead:

Use those coordinates - from the user - as input coordinates in a dynamic query. In the query's SQL, you calculate the minimum distance between the user's coordinates and those of each of the locations in the database table.

 

I asked what your database brand is because some have geographical functions you could use. See, for example, https://stackoverflow.com/questions/13026675/calculating-distance-between-two-points-latitude-longit...

 

Alternatively, you could do all the calculations in ColdFusion rather than in the database. This method is likely to be inefficient. I am giving it just for completeness.

Here, your query would return the coordinates of the property locations. You could then loop through, calculating distances by means of formulas you can find on the web. For example, https://www.geeksforgeeks.org/program-distance-two-points-earth/

 

Here's the calculation expressed as a coldfusion function:

 

 

<cfscript>
	distance=calculateDistanceOnEarth(53.32055555555556,-1.7297222222222221,53.31861111111111,-1.6997222222222223);
    writeoutput(distance);
    
	public numeric function calculateDistanceOnEarth(numeric lt1, numeric lg1, numeric lt2, numeric lg2) 
    { 
  
        // The math module contains a function 
        // named toRadians which converts from 
        // degrees to radians. 
        var lat1 = toRadians(arguments.lt1); 
        var lon1 = toRadians(arguments.lg1); 
        var lat2 = toRadians(arguments.lt2); 
        var lon2 = toRadians(arguments.lg2); 
  
        // Haversine formula  
        var deltaLong = lon2 - lon1;  
        var deltaLat = lat2 - lat1; 
        var a = sin(deltaLat / 2)^2 
                 + cos(lat1) * cos(lat2) 
                 * sin(deltaLong / 2)^2; 
              
        var c = 2 * asin(sqr(a)); 
  
        // Radius of earth in kilometers. Use 3956  
        // for miles 
        var r = 6371; 
  
        // calculate the resulting distance in kilometers 
        return(c * r); 
    }  
  
    public numeric function toRadians(numeric deg) {
    	return arguments.deg*pi()/180;
    }
    
</cfscript>

 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 24, 2020 Feb 24, 2020

Copy link to clipboard

Copied

To make it clearer, I mean

In the query's SQL, you calculate the distance between the user's coordinates and those of each of the locations in the database table, then find the minimum.

That gives the location(s) closest to the user.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Feb 25, 2020 Feb 25, 2020

Copy link to clipboard

Copied

LATEST

What database are you using?  If Microsoft SQL Server, you could use the geography data type.  I also store lat/lng, but use it for mapping, user edit and auto-generation of the geography data type.

 

CREATE:  [GeoData] [geography] NULL

 

INDEX: CREATE SPATIAL INDEX SIndx_SpatialTable_GeoData ON AddressData(GeoData);

 

UPDATE: UPDATE AddressData

SET GeoData = geography::STGeomFromText('POINT('+convert(varchar(20),Longitude)+' '+convert(varchar(20),Latitude)+')',4326)

WHERE GeoData IS NULL AND Longitude IS NOT NULL AND Latitude IS NOT NULL

 

QUERY: SELECT DISTINCT T.ZipCode, T.CityMixedCase
FROM ZipCodes T JOIN ZipCodes T2 ON T2.Zipcode = '#Zip#'
WHERE T.GeoData.STDistance(T2.GeoData) <= (#VAL(MileRadius)# * 1609.344)

 

This code is very fast and more accurate  We prefer to use a separate zipcode database so that we don't have to add geography data to every client database.

 

There's also a way to quickly calculate the distance as-the-bird-flies between any two (2) geodata items as well as find-closest-to-me and then rank according to distance.  When we use this, it's usually to locate entries within a <50 mile radius. (We've developed a couple national networking websites and also specialize in MLS.)

 

NOTE:  You can license US city/state/zip data from zip-codes.com.  We prefer to download it in CSV format, perform a bulk import and then add the geography data.  (The address data is also available in Access, Excel and SQL-formatted.)

 

As for directions (ie, street/routing data), that's a different task altogether.  I believe HERE.com has an API for that and may be worth checking out.

 
 
 
 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation