how to get current location latitude and longitude in php

Solutions on MaxInterview for how to get current location latitude and longitude in php by the best coders in the world

showing results for - "how to get current location latitude and longitude in php"
Giuseppe
20 Apr 2019
1var x=document.getElementById("demo");
2function getLocation(){
3    if (navigator.geolocation){
4        navigator.geolocation.getCurrentPosition(showPosition,showError);
5    }
6    else{
7        x.innerHTML="Geolocation is not supported by this browser.";
8    }
9}
10
11function showPosition(position){
12    lat=position.coords.latitude;
13    lon=position.coords.longitude;
14    displayLocation(lat,lon);
15}
16
17function showError(error){
18    switch(error.code){
19        case error.PERMISSION_DENIED:
20            x.innerHTML="User denied the request for Geolocation."
21        break;
22        case error.POSITION_UNAVAILABLE:
23            x.innerHTML="Location information is unavailable."
24        break;
25        case error.TIMEOUT:
26            x.innerHTML="The request to get user location timed out."
27        break;
28        case error.UNKNOWN_ERROR:
29            x.innerHTML="An unknown error occurred."
30        break;
31    }
32}
33
34function displayLocation(latitude,longitude){
35    var geocoder;
36    geocoder = new google.maps.Geocoder();
37    var latlng = new google.maps.LatLng(latitude, longitude);
38
39    geocoder.geocode(
40        {'latLng': latlng}, 
41        function(results, status) {
42            if (status == google.maps.GeocoderStatus.OK) {
43                if (results[0]) {
44                    var add= results[0].formatted_address ;
45                    var  value=add.split(",");
46
47                    count=value.length;
48                    country=value[count-1];
49                    state=value[count-2];
50                    city=value[count-3];
51                    x.innerHTML = "city name is: " + city;
52                }
53                else  {
54                    x.innerHTML = "address not found";
55                }
56            }
57            else {
58                x.innerHTML = "Geocoder failed due to: " + status;
59            }
60        }
61    );
62}
Lennart
24 Sep 2016
1function geoLocate($address)
2{
3    try {
4        $lat = 0;
5        $lng = 0;
6
7        $data_location = "https://maps.google.com/maps/api/geocode/json?key=".$GOOGLE_API_KEY_HERE."&address=".str_replace(" ", "+", $address)."&sensor=false";
8        $data = file_get_contents($data_location);
9        usleep(200000);
10        // turn this on to see if we are being blocked
11        // echo $data;
12        $data = json_decode($data);
13        if ($data->status=="OK") {
14            $lat = $data->results[0]->geometry->location->lat;
15            $lng = $data->results[0]->geometry->location->lng;
16
17            if($lat && $lng) {
18                return array(
19                    'status' => true,
20                    'lat' => $lat, 
21                    'long' => $lng, 
22                    'google_place_id' => $data->results[0]->place_id
23                );
24            }
25        }
26        if($data->status == 'OVER_QUERY_LIMIT') {
27            return array(
28                'status' => false, 
29                'message' => 'Google Amp API OVER_QUERY_LIMIT, Please update your google map api key or try tomorrow'
30            );
31        }
32
33    } catch (Exception $e) {
34
35    }
36
37    return array('lat' => null, 'long' => null, 'status' => false);
38}
39
Mía
20 Jun 2018
1
2
3<!DOCTYPE html>
4
5<html>
6
7<form method="post">
8
9<input type="text" name="address">
10
11<input type="submit" name="submit" value="submit">
12
13</form>
14
15</html>
16
17<?php
18
19if(isset($_POST['submit']))
20
21{
22
23
24function getLatLong($address){
25
26    if(!empty($address)){
27
28        //Formatted address
29
30        $formattedAddr = str_replace(' ','+',$address);
31
32        //Send request and receive json data by address
33
34        $geocodeFromAddr = file_get_contents
35('http://maps.googleapis.com/maps/api/geocode/json?address='.$formattedAddr.'&sensor=false'); 
36
37        $output = json_decode($geocodeFromAddr);
38
39        //Get latitude and longitute from json data
40
41        $data['latitude']  = $output->results[0]->geometry->location->lat; 
42
43        $data['longitude'] = $output->results[0]->geometry->location->lng;
44
45        //Return latitude and longitude of the given address
46
47        if(!empty($data)){
48
49            return $data;
50
51        }else{
52
53            return false;
54
55        }
56
57    }else{
58
59        return false;  
60 
61    }
62
63}
64
65$address = $_POST['address'];
66
67$latLong = getLatLong($address);
68
69$latitude = $latLong['latitude']?$latLong['latitude']:'Not found';
70
71$longitude = $latLong['longitude']?$latLong['longitude']:'Not found';
72
73echo "Latitude:".$latitude."<br>";
74
75echo "longitude:".$longitude."";
76
77}
78
79?>
80
queries leading to this page
how to get complete address from latitude and longitude in phpgeocoding apigoogle map get city name by latitude and longitudeapi response to show lat long for multiple locationsget state from latitude and longitude phpget city by latitude and longitude apihow to get location name from latitude and longitude in javascriptget country city name from latitude and longitude laravelget city name from latitude and longitude javascriptget city name from latitude and longitude in node jsget the latitude and longitude from phpget lat long from address phpget lat long from address google api phpphp get latitude and longitude from locationget the geolocation latitude value from phphow to get latitude and longitude from address in phphow to get latitude and longitude from form in php google api key for getting lat long from addresshow to get city name from latitude and longitude in javascriptjs city to lang long apihow to get city from latitude and longitudeget longitude and latitude phpphp get latitude longitudeget latitude and longitude from city name in phpgeocode from cityfree api that returns lat lon from city nameget place name from latitude and longitude google mapshow to get longitude and latitude in phphow to get location name from latitude and longitude in angularget city name from latitude and longitude in php using geocode apiaddress to latitude and longitude phphow to get city name using google api in reactget city name from latitude and longitude google apiconvert from lat and lon to city kotlinget state or city from lat lngconvert address to lat long using phphow do i check longitude and latitude using apihow to determine city using longitude and latitudejavascript navigator get city from latitude longitudejs city to coordinatesget longitude and latitude in phpfree lng and latitude to city apiget the city name based on longtitude and latitude google mapsget latitude and longitude phpconvert city name to latitude longitude javascripthow to get lat long from address phphow to convert city into latitude and longitude javascripthow to get gps coordinates from latitude and longitude in phphow to get latitude and longitute of current location in phpreturn latitude and longitude using phphow to get latitude and longitude in phpget coordinates of city apican you convert latitude and longitutle to city name javascriptfrom city to coordinate jslatitude longitude php geo apiget longitude and latitude using phpwith latitude and longitude data find address using phpapi get city from lat longhow to get current location latitude and longitude in phpapi latitude and longitude of city by namejavascript get city with latitudeapi get city location with lat longapi to find latitude and longitude using city nam 2ceget city name from latitude and longitude in phphow to get city name from geocoding in angularwhen invoked 2c this function should use geocode xyz 27s api retrieve the city data 2c which uses a url formatted as follows 3a https 3a 2f 2fgeocode xyz 2fseattle 3fjson 3d1js coordinates to cityget city from lat long javascripthow to get place name from latitude and longitudephp get current location latitude longitudephp get longitude and latitudeget city from lat lon javascriptget city name from latitude and longitude in jsapps script map get city name from coordinatesapi to get country from coordinatesjavascript get city from latitude longitudeswitch case for lat long giving city nameget location from latitude and longitude phpget current latitude and longitude phpget latitude and longitude from address in php get latitude and longitude from address phpget longitude and latitude gps phpget city name from latitude and longitudeget city from latitude longitudecities found json api javascripthow to get the latitude and longitude of a place in javascript by cityget latitude and longitude from address using phphow to get city name by latitude and longitude in javascriptget city by longitude and latitude google maps apiapi get city with lat and loggeoreference google apiget city name by latitude and longitudeget city code from latitude longitudeapi geocode free lat longhow to get cordinate from city name jsget city using longitude and latitude apiget city from coordinates jsphp code to get address from latitude and longitudehow to get city name from latitude and longitude inhow to get latitude and longitude from city name in javascript without google mapsgoogle api to get latitude and longitude from city idgoogle api get lat long from addressfetching latitude and longitude in phplat long to country code api freehow to get lat and long from city name and state name javascriptjs city to lon lat apiget city name based on lat and logitudeget latitude from city name jsfind a city with latitude and longitude apihow to get city name from latitude longitudetype place and get latitude and longitude php functionget the city 2c state 2c and country names from latitude and longitude using phpget city name from latitude and longitude jsgrab latitude and longitude in phpget lat and long from city name javascriptlongitude and latitude phpusing the geocode apifree get boundaries coordinate apiget user details from lat long city 2c countget city state country from lat long javascriptget city name from latitude and longitude without apiget longitude and latitude from city name jsget city longitude latitudehow to turn lat and long into city javascriptget location name from latitude and longitude javascriptget location name from latitude and longitude angularapi to convert lat and long to city nameswitch case for lat long giving citz nameonline api get long and latidutdeget latitude and longitude with phpget latitude and longitude in phphow to get latitude and longitude using place name 3flatitude longitude phpgeocoder php get longitude and latituderetrieve lat lon from city namejs how to find a city by longitude and latitudeget city and country from lat long javascriptget city name from latitude and longitude without geolocation in phpget current latitude and longitude in php to input feildconvert latitude and longitude to city in javascriptapi city coordinatesget city name from latitude and longitude reactget city name by lat longgegt city name from google map using angularlatitude longitude validation in phpgeocode get city name from long latget place name from latitude and longitudeget city and town name from latitude longitudeapi coordinatescity name from latitude and longitudefree api to return lat and longget city from lat long and use ithow to get city name from latitude and longitudehow to get current location latitude and longitude in php