This tutorial is prepared for those Developers who want to get users latitude and longitude. This can be done by many methods, here I’m going to use Google Place API to make such things easier.
The method and logic I’m going to use is when user fills the registration form up, we’d capture his city value (Note: city value would be Google Place Search), before proceeding further you have to read my previous post Auto complete Address Search Module Using Google API and PHP, this is the module from which we ask user to enter their location, further this location is used to capture user’s coordinates using another google api.
What is Latitude and Longitude?
Latitude and Longitude are the entity that represent the coordinates at geographic coordinate system. For example every actual house has its own address (which includes the house number, the street name, city, country, etc), every single dot on the surface of earth can be specified by the latitude and longitude coordinates. Therefore, by using these coordinates we can specify virtually any point on earth.
How to get users latitude and longitude?
Step 1: after submitting registration form catch the user’s location value (read Auto complete Address Search Module Using Google API and PHP to understand “$location” variable bellow).
$location = $_POST['location'];
Step 2: make a request to google place api
$data = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($location)."&sensor=true"); $longitude = json_decode($data)->results->geometry->location->lng; $latitude = json_decode($data)->results->geometry->location->lat;
as you can see the above variables $longitude & $latitude store user’s exact coordinates. You can use these variables as any way as you want.
NOTE: If you want to store these coordinates into database, you must have to create longitude and latitude column with datatype double.
Please share this article with your friends, and get in touch because we are going to post lots of interesting and helpful modules in future, don’t forget to subscribe us to get latest updates.
You may also like