Varinder
Varinder

Reputation: 202

How to get exact location name with lat & long google map?

I want to get location name using latitude and longitude of user like any theater, restaurant, famous park, vacation place, shopping store. I am using Google map api but they only shows area name not any store name. How I get location name?

Upvotes: 2

Views: 1785

Answers (1)

David Noah
David Noah

Reputation: 69

You can use Google Places API to get this information. Google Maps will not return store or place data based on latitude and longitude parameters.

I would enable your API key to work with Google Places API and then make a call to the getNearbyPlaces endpoint. The endpoint requires latitude, longitude, and radius (distance in meters in which the results must be found) parameters. Remember, this is still a query so your response will contain multiple results and by default, the results are ordered by popularity. I like to also specify a "type=establishment" as an optional parameter so I don't get abstract results like "route".

You can test this out in RapidAPI here. I've linked you directly to the getNearbyPlaces endpoint. Just fill in your apiKey, latitude, longitude, radius (I like to keep it around 20 meters for specificity), and any other optional parameters. Click "TEST function" to see a detailed JSON response. I'll show you a screenshot of what this looks like.

responseScreenshot

In this example, I looked up the latitude and longitude for Bob's Doughnuts. Boom! My first result was in fact, Bob's Doughnuts in San Francisco. RapidAPI also lets you generate a code snippet that you can copy and paste directly into your own code. You just have to click the "CODE" button above the response, sign in, and choose the language you're using. The code snippet provided from the above call looks like:

enter image description here

Hope this helps! One more thing.. Since the results are ordered by popularity, the first result might not always be the ideal result. The API provides a "rankBy" parameter, but I think Google is still working out some bugs with it. In the meantime, I would build a loop that finds the closest result item by distance. All you need to do is create a distance function using the Haversine formula. I'll build you a quick function! I used the haversine formula function from this post. Heres the code.

// Still using my Bob's Doughnuts example
const startLatitude = "37.7918904"; // your original query latitude
const startLongitude = "-122.4209966"; // your original query longitude

function closestResult(results) {
  let closestResult = null;
  let shortestDistance = null;

  for (let i = 0; i < results.length; i++) {
    let location = results[i].geometry.location;
    let currentDistance =
      getDistanceFromLatLonInKm(startLatitude, startLongitude, location.lat, location.lng);
    if (shortestDistance === null) {
      closestResult = results[i];
      shortestDistance = currentDistance;
    } else if (currentDistance < shortestDistance) {
      closestResult = results[i];
      shortestDistance = currentDistance;
    }
  }
  return closestResult;
}

function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
  var R = 6371; // Radius of the earth in km
  var dLat = deg2rad(lat2-lat1);  // deg2rad below
  var dLon = deg2rad(lon2-lon1);
  var a =
    Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
    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
  return d;
}

function deg2rad(deg) {
  return deg * (Math.PI/180);
}

Now your success callback can look like this:

enter image description here

Happy coding!

Upvotes: 1

Related Questions