Search
 
SCRIPT & CODE EXAMPLE
 

PHP

get city name from latitude and longitude in php using geocode api

var x=document.getElementById("demo");
function getLocation(){
    if (navigator.geolocation){
        navigator.geolocation.getCurrentPosition(showPosition,showError);
    }
    else{
        x.innerHTML="Geolocation is not supported by this browser.";
    }
}

function showPosition(position){
    lat=position.coords.latitude;
    lon=position.coords.longitude;
    displayLocation(lat,lon);
}

function showError(error){
    switch(error.code){
        case error.PERMISSION_DENIED:
            x.innerHTML="User denied the request for Geolocation."
        break;
        case error.POSITION_UNAVAILABLE:
            x.innerHTML="Location information is unavailable."
        break;
        case error.TIMEOUT:
            x.innerHTML="The request to get user location timed out."
        break;
        case error.UNKNOWN_ERROR:
            x.innerHTML="An unknown error occurred."
        break;
    }
}

function displayLocation(latitude,longitude){
    var geocoder;
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(latitude, longitude);

    geocoder.geocode(
        {'latLng': latlng}, 
        function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                if (results[0]) {
                    var add= results[0].formatted_address ;
                    var  value=add.split(",");

                    count=value.length;
                    country=value[count-1];
                    state=value[count-2];
                    city=value[count-3];
                    x.innerHTML = "city name is: " + city;
                }
                else  {
                    x.innerHTML = "address not found";
                }
            }
            else {
                x.innerHTML = "Geocoder failed due to: " + status;
            }
        }
    );
}
Comment

get city name from latitude and longitude in php using geocode api

<p><button onclick="getLocation()">Get My Location</button></p>
<p id="demo"></p>
<script src="http://maps.google.com/maps/api/js?key=YOUR_API_KEY"></script>
Comment

PREVIOUS NEXT
Code Example
Php :: laravel where in query builder 
Php :: php email sender 
Php :: php array 
Php :: Generate Laravel Migrations from an existing database 
Php :: array_search function in php 
Php :: create seed file from db laravel 
Php :: pagination javascript php 
Php :: dont insert duplicate data in laravel 
Php :: template engine php 
Php :: create an email addresses php 
Php :: laravel relationship 
Php :: multiple value match in array php 
Php :: merge pdf php fpdf 
Php :: default time of session in php 
Php :: Code for finding Prime Numbers 
Php :: php code generator 
Php :: password_verify 
Php :: phpspreadsheet 
Php :: laravel Call to a member function validate() on array 
Php :: merge multiple exceptions php 
Php :: IgasterLaravel ThemeExceptions heme Already Exists 
Php :: $age = 20; print ($age = 18) ? "Adult" : "Not Adult"; 
Php :: remove dashboard mya ccount 
Php :: cake php 2.x sql dump 
Php :: how to use pg_dropcluster 
Php :: tina4 save file to database in orm 
Php :: array with key value pair php 
Php :: How to hide tax details from woocommerce order emails 
Php :: vault deployment in production 
Php :: laravel 8 template favicon 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =