Viewed   62 times

i am using this api for getting current location but when this site goes down i am not able to get current location.

can google provide any api to find current location using ip.



If you don't want to use the HTML5 Geolocoation API, you can detect a bit from IP.

Or try other other solution

The Google Geocoding API




Tuesday, December 27, 2022

the solution of this question is

                    $url = str_replace(' ','-',$sport['title']);
                    $url = str_replace(":",'',$url);
                    $url = str_replace("'",'',$url);    

<a href="<?php echo base_url().'news/readmore/'.$sport['id']."/".urlencode($url);?>"><?php echo $sport_top['title'];?></a>
Sunday, August 28, 2022

There is no way to get the user location using PHP since it's running on the server side. You can get the user location using javascript through the browser.

Here is an example. In this example I separated the code in two files. One for processing and storing the information using PHP (geocoordinates.php) and another one (HTML) for collecting the geocoding informantion (index.html), index.html.

You could combine both files into index.php but I'll keep them separated for simplicity.



if(isset($_POST['lat'], $_POST['lng'])) {
    $lat = $_POST['lat'];
    $lng = $_POST['lng'];

    $url = sprintf(",%s", $lat, $lng);

    $content = file_get_contents($url); // get json content

    $metadata = json_decode($content, true); //json decoder

    if(count($metadata['results']) > 0) {
        // for format example look at url
        $result = $metadata['results'][0];

        // save it in db for further use
        echo $result['formatted_address'];

    else {
        // no results returned



<!DOCTYPE html>
    <meta charset="utf-8">
    <title>Geocoding Page</title>
    <script src=""></script>
  function getLocation() {
      if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(savePosition, positionError, {timeout:10000});
      } else {
          //Geolocation is not supported by this browser

  // handle the error here
  function positionError(error) {
      var errorCode = error.code;
      var message = error.message;


  function savePosition(position) {
            $.post("geocoordinates.php", {lat: position.coords.latitude, lng: position.coords.longitude});
    <button onclick="getLocation();">Get My Location</button>

Keep in mind that in this example the once the user clicks "Get My Location" the browser will prompt the user to allow the geolocation. You could also call the getLocation function once the page loads, but the browser will always ask for the user's permission

You can learn more about geolocation at

Monday, November 14, 2022

You should be using a service account for this. Accessing the Google APIs using a service account is quite useful. Some times you just want to access your own data and not data owned by other users. In this instance there is no reason to use OAuth2 and prompt a user to give you access to there information, its your information you already have access. This is why we use a service account.

Google Service Account Example with in the PHP client lib can be found here: Example

You don't say exactly which API you are trying to access, and I dislike link only answers. So here is a basic example using the Google Analytics API.

require_once 'Google/Client.php';
require_once 'Google/Service/Analytics.php';

  The following 3 values an befound in the setting
  for the application you created on  Google 
  Developers console.
  The Key file should be placed in a location
  that is not accessable from the web. outside of 
  web root.

  In order to access your GA account you must
  Add the Email address as a user at the 
  ACCOUNT Level in the GA admin. 
$client_id = '';
$Email_address = '';
$key_file_location = '629751513db09cd21a941399389f33e5abd633c9-privatekey.p12';

$client = new Google_Client();

$key = file_get_contents($key_file_location);

// seproate additional scopes with a comma
$scopes ="";  

$cred = new Google_Auth_AssertionCredentials(

if($client->getAuth()->isAccessTokenExpired()) {

$service = new Google_Service_Analytics($client);  

I have a tutorial that goes along with that code it can be found here. Google Service Account PHP

Thursday, November 10, 2022

What you are looking to do is get the position using the LocationManager.NETWORK_PROVIDER instead of LocationManager.GPS_PROVIDER. The NETWORK_PROVIDER will resolve on the GSM or wifi, which ever available. Obviously with wifi off, GSM will be used. Keep in mind that using the cell network is accurate to basically 500m. has some really great information and sample code.

After you get done with most of the code in OnCreate(), add this:

// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
      // Called when a new location is found by the network location provider.

    public void onStatusChanged(String provider, int status, Bundle extras) {}

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}

// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

You could also have your activity implement the LocationListener class and thus implement onLocationChanged() in your activity.

Tuesday, August 2, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :