Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding nearest locations using Google Maps API

Tags:

Hi I'm writing an app that shows the bike stations near an address. I have the list of latitude and longitude locations of each bike station from a service.

I can mark my current location, or any address so far. How do I show all the bike stations near my location on map. Do I first get nearest Google places from the current location and then access the database for bike location and make markers on the map? What is the best approach?

like image 671
Geoplex Avatar asked May 15 '13 09:05

Geoplex


People also ask

How do I find nearby in Google Maps?

To search categories near you: On your Android phone or tablet, open the Google Maps app. . Under the search bar, tap a suggestion, such as Restaurants, Coffee, or Hotels.

How do I get a list of places on Google Maps API?

Go to the Google Cloud Console. Click the Select a project button, then select the same project you set up for the Maps JavaScript API and click Open. From the list of APIs on the Dashboard, look for Places API. If you see the Places API in the list, it's already enabled.

Is Google Places API free?

Places API is not free, however, once you set up your billing account, you will be entitled for a one time $300 free credit(usable for Google Cloud Platform products) and a monthly recurring $200 free credit(exclusive for Google Maps Platform products), after consuming the credits, you will receive an OVER_QUERY_LIMIT ...

What can you do with Google Places API?

The Places API lets you search for place information using a variety of categories, including establishments, prominent points of interest, and geographic locations. You can search for places either by proximity or a text string.


2 Answers

If you already have the coordinates of your bike stations and if you trust this data, then just use it to draw your markers. Now you have to define what "near" means.

You have different solutions for this. Either you choose to draw the markers for all the bike stations within the map bounds (but that could be many markers to draw depending on the zoom level, or you have to prevent your script from drawing the markers before a certain zoom level is reached) or you can draw the markers within n kilometers around a location (can be the user location, the map center coordinates, etc.).

For the second solution, and if you are storing your bike stations in a MySQL database, you could do a query like that:

$sql = "SELECT *, ( 6371 * acos( cos( radians(" . $db->real_escape_string($lat) . ") ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(" . $db->real_escape_string($lng) . ") ) + sin( radians(" . $db->real_escape_string($lat) . ") ) * sin( radians( lat ) ) ) ) AS distance FROM your_table_name HAVING distance < 15"; 

$lat and $lng being your center coordinates

lat and lng being your MySQL column names

15 being the radius (in km) around your coordinates

This uses the Haversine formula. Good information can be found here.

Hope this helps, but we don't really know how you organized your data in your app. Give us more info if you need more help on this!

like image 196
MrUpsidown Avatar answered Sep 24 '22 03:09

MrUpsidown


Just In case someone is looking for a correct answer in 2016.

There is a very useful library created by google, The Geometry library has many different method which might help solving this problem:

computeDistanceBetween()

This answer explains exactly how to use it.

This will typically calculate distance between two passed LatLng objects.

So you can simply:

  • Get all bike stations location.
  • Store them in array.
  • Find the user current location.
  • Loop your array, and convert locations to distances using the mentioned method.
  • Sort you array to get smallest distance.

Distance results are expressed in meters.

The previous algorithm may not be optimized, because some of us may have an array of thousands of coordinates, this where the method containsLocation() becomes handy, as you can narrow your searching region by specifying a polygon you can search within.

This might not be the most optimum method to find nearest location, but I believe it will do the job if you have a reasonable number of stations in your database.

like image 36
ProllyGeek Avatar answered Sep 24 '22 03:09

ProllyGeek