how to get a lat long from address using google api in php

How to get a latitude and longitude using google map api, create account to console  and enable google map api and geocode api, and get api  and put in this url




https://maps.googleapis.com/maps/api/geocode/json?address=$address&key=Your API key

Here is php function to pass address and get lat long in a return value

// function to get  the address
 public function GetLatLong($address){
  $address = str_replace(" ", "+", $address);
 $json = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address=$address&key=Your API key");
     $json = json_decode($json);
     $lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
     $long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
     return $lat.','.$long;
 }

No comments:

Post a Comment

how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...