2

I am having a list of tourist locations with me and i am willing to write a service that could tell me which tourist location is nearest to me and then second nearest and likewise without using a map. How can i do it. The idea is that i have a database of all the tourist locations in a city and their lat/long, I may be in any place in the city and want to find which tourist attraction is nearest to me and second nearest and like wise so that i may plan to visit them based on how much time i have. I tried this and found google maps api but i dont want to display a map for the same. Or give users map to search things.

sushil bharwani
  • 29,685
  • 30
  • 94
  • 128

2 Answers2

0

You don't need Google maps.

  1. Get the user's location via the geolocation API.
  2. Map over the list of points, augmenting your object with the distance between the user and the point as calculated using the great-circle distance algorithm.
  3. Sort the list via the distance.
alex
  • 479,566
  • 201
  • 878
  • 984
  • I dont want to get the user's location automatically; I want user to add his location in a textfield and look for options. Because i want the user to be able to search for and suggest other users also. I may give him a option to do a "find my location" thing. – sushil bharwani Aug 06 '12 at 07:59
  • The user will type their own lat and lon? I think you over estimate how keen tourists are to know about their surroundings. You can use the Google Maps API to get lat/lon from a human readable address. – alex Aug 06 '12 at 08:03
  • Lets say somebody asks me where can i visit today I am in x Location, So i can use this service type his x location and find out all the nearest places to him. I didnt get your point abt over estimation. – sushil bharwani Aug 06 '12 at 10:56
  • Great Circle Distance only gives areal distance between two locations, There may be a case that no road route exists between these two places. – sushil bharwani Aug 06 '12 at 11:53
  • @sushilbharwani Then you will need to use an API such as Google Maps API. – alex Aug 06 '12 at 12:00
  • yes but then it embeds a map in the system, is there something else where i do not have a map. Just the distance calculations. – sushil bharwani Aug 06 '12 at 12:04
  • @sushilbharwani You don't need to show the user a map to use the Google Maps API. – alex Aug 06 '12 at 12:05
  • 1
    I found this https://developers.google.com/maps/documentation/javascript/distancematrix for calcluating distance with google maps but it says its necessary needs to show google maps. – sushil bharwani Aug 06 '12 at 12:44
0

If you're not going to display a map, then you can't use Google Maps API (it violates their TOS).

If you are looking to get the lat/lon from an address without Google Maps or similar (because similar services have similar TOS) then you'll want to look for something like LiveAddress API (and apparently I'm supposed to disclose that I work at SmartyStreets)

This example works for US addresses. International addresses require a different API.

An API like the US Street Address API doesn't require you to show a map, returns geo coordinates, and will verify the validity of the address as it returns its payload.

Here's a Javascript example.

    <script type="text/javascript" src="liveaddress.min.js"></script>
    <script type="text/javascript">
    LiveAddress.init(123456789); // API key

    // Make sure you declare or obtain the starting or ending lat/lon somewhere.
    // This example only does one of the points.

    LiveAddress.geocode(addr, function(geo) {
        var lat2 = geo.lat, lon2 = geo.lon;

        // Distance calculation from: http://stackoverflow.com/questions/27928/how-do-i-calculate-distance-between-two-latitude-longitude-points
        var R = 6371; // Radius of the earth in km
        var dLat = (lat2-lat1).toRad();  // Javascript functions in radians
        var dLon = (lon2-lon1).toRad(); 
        var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * 
                Math.sin(dLon/2) * Math.sin(dLon/2); 
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
        var d = R * c; // Distance in km
    });
    </script>
Józef Podlecki
  • 10,453
  • 5
  • 24
  • 50
Matt
  • 22,721
  • 17
  • 71
  • 112