Get longitude and latitude as a user machine in PHP

How can I get longitude and latitude using a PHP user machine?

I need it to be so long if the north is a positive value, and if in the south it is a negative value /

And in terms of latitude, if the east is a positive value, and if the west is a negative value.

How can i do this?

+6
source share
3 answers

The only way to determine the location on the server side is to use the lookup table for the IP address. There are services that provide this for you, so you can do this:

$ip = !empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']; $url = "http://freegeoip.net/json/$ip"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); $data = curl_exec($ch); curl_close($ch); if ($data) { $location = json_decode($data); $lat = $location->latitude; $lon = $location->longitude; $sun_info = date_sun_info(time(), $lat, $lon); print_r($sun_info); } 

It will not always be very accurate though. In javascript, you will have access to the HTML5 geolocation API or Google Maps, but reverse geocoding with Google requires using a map according to TOS.

NOTE (2019): The service used in the example, FreeGeoIP, was closed, I will leave an answer for posterity, since there are probably other services offering the same service.

+16
source

Refer to the Google Maps API Geocoder: http://lab.abhinayrathore.com/ipmapper/

This will answer all your questions from AZ. If you have problems using it, check where this question was asked and answered: Find the latitude and longitude of a given IP address using IPMapper

This will be done using Javascript, not PHP

0
source

Since the exact location requires user consent, this cannot be done with PHP. Try using JS to get the user's location and use AJAX to send to the server.

If you want to use an approximate location, you can map by IP address. Web search will open for him several options, for example this one .

0
source

Source: https://habr.com/ru/post/944297/


All Articles