Finding nearest location Google Places API - google-places-api

I have a project that uses Google places to search for a specific type of business using a keyword search, centered on my user's current location. For example suppose I'm trying to find "Pink Bunny Rabbits" closest to my users.
In some cases, my user is located in a place where none of my specific types of businesses are withing the Google Places API maximum search radius (20KM, IIRC). "No Pink Bunny Rabbits close to you"
Is there some method that will search for the nearest matching business, even if it's outside the initial max search range?
"Closest Pink Bunny Rabbit: 30 miles away in Sometown,IA"
Is Google Places is the correct API to use for this secondary search.
What fallback services can I use to find the "Pink Bunny Rabbit" store closest to my user?

Well, there is a scope of making a place search with larger radius. https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&types=food&name=cruise&key=AddYourOwnKeyHere.
The current search covers a circle with 500 meter around the latitude and longitude provided in search url. The maximum value for that is 50000. Unfortunately Google Places API does not sort the result according to user's current location. You need to manually calculate the distance in between the location of the place from search result and your current location.
// Calculating the distance
float deltaLat = (currentLocation.lat - place.lat);
float deltaLng = (currentLocation.lng - place.lng);
float distanceInDegree = sqrtf(deltaLat * deltaLat + deltaLng * deltaLng);
float distanceInMeter = distanceInDegree * 4000;
printf("distanceInMeter = %f",distanceInMeter);
Find out the place with minimum distance.

Related

Fetching the nearest location of points, while accounting for bodies of water

I've got a database of points (in this case, schools) and in my app, users search for the nearest ones to them. Under the hood, we're currently using ElasticSearch to filter by latlng (using Geo Distance, which gets the distance as the crow flies. For the majority of places, this works fine, but in some coastal areas, this will pick up places that are impossible to get to, in the example below, a radius of 20 miles will pick up schools in Weston-Super-Mare, in reality 55 miles:
I initially decided to use the Google Maps Distance Matrix API to filter my inital as the crow flies search, but there's a limit of 25 destinations per query, and as the requests will be dynamic and user-facing, it's not practical to parcel these requests up into small pieces and pop in a background job.
Is there any way to carry out these calculations while accounting for bodies of water on a database level? The schools are stored in a Postgres database, so I thoughts about using PostGIS and some kind of point in polygon query, but I have no idea where to start looking.
Any ideas are very much appreciated!

Xamarin.Forms: how to get driving distance between two points?

My app currently uses Location Services. During debugging, I retrieve my current location using GetLastKnownLocationAsync: myposition.Latitude is 40.758896 while myposition.Latitude is -73.985130.
My SQL Database has a list of Walmart stores, and there's a Walmart with Latitude & Longitude of 40.660992 & -73.7267629.
How can I calculate the driving distance between my set of coordinates and Walmart's coordinates? It would be something like Google Maps' Driving Directions or Waze.
I understand that I can use this SO link to calculate the distance between two sets of point, but I assume apps like Google Maps or Waze consider the actual driving distance between two sets of points. The link above would be great if there were a straight street between two points. Obviously, that's not the case.
I have used Xamarin.Essential library to find the distance between two point
You can use something like this
var location = new Location(21.705723, 72.998199);
var otherLocation = new Location(22.3142, 73.1752);
double distance = location.CalculateDistance(otherLocation,DistanceUnits.Kilometers);

How to get FLIGHT time between two locations using the Google Maps Distance Matrix API?

I need to get travel time by plane between two locations.
In Distance Matrix API there is no Travel Mode like "flight" - https://developers.google.com/maps/documentation/javascript/distancematrix#travel_modes
Any suggestions?
Unfortunately there is no easy way to do this.
However, you could do a trick and estimate the flight time by using geocoding and the geometry library. At least if you don't need real flight data.
The steps would be:
Transform origin and destination from String to LatLng (Geocoding API)
Create markers for each location. (Geometry lib)
Calculate airline distance using computeDistanceBetween (like this: google.maps.geometry.spherical.computeDistanceBetween(path1, path2))
Calculate approximate flight time using average airplane speed.
Hope this helps.
This is sort of a work-around, but an easy one.
Use the measure distance tool in google maps, then for flight time of regular commercial airliners, divide the distance in miles by 500mph to get flight time in hours. For a small propeller plane (like a cessna), use 165mph.

How to calculate the driving distance between two points?

In my app I am getting the latitude , longitude co-ordinates of places using GeocodeService now I want to get distance between these places.
I've tried GetDistanceTo method to get the distance two location co-ordinates but it gives me stright distance.
How can I calculate driving distance between two locations?
thanks in advance
To calculate the driving distance you'll have to use the Bing Routes API (or Google, but I assume you're using Bing Maps).
There's an example of using the api to find a driving route between two places here: http://msdn.microsoft.com/en-us/library/gg636957.aspx
The general call is:
http://dev.virtualearth.net/REST/V1/Routes/Driving?o=xml&wp.0=location1&wp.1=location2&avoid=minimizeTolls&key=BingMapsKey
You'll have to register for a Bing Maps API key, and then replace location1 and location2 with the locations you like.
The distance should be returned in travelDistance in the json or xml

Find street intersections within an area in using Google Maps API

Given a square area, what is the best way to find the approximate coordinates of every street intersection within the given area ?
Since there is no description of your application, I can't tell if you need to use Google Maps or if another data source would answer your needs.
If http://openstreetmap.org fulfills the requirements of your application, then it's easy:
the OSM API has a request to pull data from a rectangular region. You get XML data.
filter this data to keep only the street you are interested in, probably the "key=highway" tags
filter this to keep only the points belonging to two or more lines.
Please disregard this if Google Maps is a requirement.
But still: since the roads exist independently of the database, the above method will yield roads intersections (in lat/long coordinates) with a pretty high correlation with what you would get from Google maps ;-) You can then use those points to display them over a Google map, knowing that both datasets aren't identical so it won't be perfect.
Might not be the easiest method but I used a seperate database of our countries roads with their linestrings.
I took the first and last points of each line string, then counted the number of roads within 50 m of each start/end point. I then took the nodes from navigation route and used these to compare the number of roads intersecting with each node. I then looked at the direction each start point and the next point along that road, which gives you direction. from that with a bit of maths you can work out the number and angle of the roads at the next intersection. I then made a road rules application that tells you which vehicles to give way to

Resources