I have a problem when I try to convert an address into latitude and longitude via google service. My address is 1456 sok. no: 10/1 kat:8 Alsancak. The problem is when I write this address to url, correct result is returned, however when I use the php code below, I get zero results.
No problem with result :
http://maps.google.com/maps/api/geocode/json?address=1456%20sok.%20no:%2010/1%20kat:8%20Alsancak&sensor=true
Problem with php :
<?php
header('Content-Type: text/html; charset=utf-8');
getGoogleAddressCoordinates("1456 sok. no: 10/1 kat:8 Alsancak");
function getGoogleAddressCoordinates($address)
{
//$address = urlencode($address);
$address = str_replace(" ", "%20", $address);
$request = file_get_contents('http://maps.google.com/maps/api/geocode/json?address=' . $address . '&sensor=true');
$json = json_decode($request, true);
print_r ($json);
}
?>
The address 1456 sok. no: 10/1 kat:8 Alsancak is not valid. If you try it in google maps you get zero results, too.
Try it with: 1456 sok. 10/1 Alsancak and uncomment the line $address = urlencode($address); and clear the line $address = str_replace(" ", "%20", $address);.
The url should look like:
http://maps.google.com/maps/api/geocode/json?address=1456%20sok.%2010%2F1%20Alsancak&sensor=true
Related
I'm using this function to get the coordinates of any address:
function getCoordinates($address) {
$address = str_replace(" ", "+", $address); // replace all the white space with "+" sign to match with google search pattern
$address = str_replace("-", "+", $address); // replace all the "-" with "+" sign to match with google search pattern
$url = "http://maps.google.com/maps/api/geocode/json?address=$address";
$response = file_get_contents($url);
$json = json_decode($response,TRUE); //generate array object from the response from the web
return ($json['results'][0]['geometry']['location']['lat'].",".$json['results'][0]['geometry']['location']['lng']);
}
Sometimes it works and sometimes it only gets "," for the same address.
Do I need to use another function?
try this
$url = "http://maps.google.com/maps/api/geocode/json?address=".$address;
I have a pho script that takes an address and returns the lat/lon. Works fine until I put the google api key at the end of the url. I tested the url in the browser and it works fine there.
This works fine:
$geo = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&sensor=false');
This causes an error:
$geo = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'+&sensor=false+CA&key=[MyAPIKeyHere]');
It causes the error: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request
I've seen consulted similar posts such as,
Warning while using file_get_contents for google api , but they weren't much help.
Here is the complete code:
<?php
// Address
$address = '1451 Broadway New York, NY 10036';
// Address from command line
// $address = readline("Enter an address: ");
// $address = str_replace(' ','+',$address);
// Get JSON results from this request
// This url works
$geo = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&sensor=false');
// This url creates an error
//$geo = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'+&sensor=false+CA&key=[MyAPIKeyHere]');
// Convert the JSON to an array
$geo = json_decode($geo, true);
if ($geo['status'] == 'OK') {
// Get Lat & Long
$latitude = $geo['results'][0]['geometry']['location']['lat'];
$longitude = $geo['results'][0]['geometry']['location']['lng'];
}
// $address = str_replace('+',' ',$address);
echo "The address ". $address. " has the following coordinates:\n" ;
echo "Lat: ". $latitude. " Lon: ". $longitude. "\n";
?>
Try using this API. It worked for me.
https://maps.googleapis.com/maps/api/geocode/json?address='.$address.'&key=YOUR_API_KEY'
I'm uploading a csv file and getting address field in $address variable, but when I pass $address to google maps, its showing me error,
file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?address=9340+Middle+River+Street%A0%2COxford%2CMS%2C38655): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request.
I searched for its solution, I found one that to encode only address but its also not working for me...
CODE
if (!empty($address)) {
$geo = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($address));
$geo = json_decode($geo, true);
if ($geo['status'] = 'OK') {
if (!empty($geo['results'][0])) {
$latitude = $geo['results'][0]['geometry']['location']['lat'];
$longitude = $geo['results'][0]['geometry']['location']['lng'];
}
$mapdata['latitude'] = $latitude;
$mapdata['longitude'] = $longitude;
return $mapdata;
} else {
$mapdata['latitude'] = "";
$mapdata['longitude'] = "";
return $mapdata;
}
}
Error is at line
$geo = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($address));
Have I missed anything.!
Any help is much appreciated.. Thanks
you need to use google api key
function getLatLong($address){
if(!empty($address)){
//Formatted address
$formattedAddr = str_replace(' ','+',$address);
//Send request and receive json data by address
$geocodeFromAddr = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.$formattedAddr.'&sensor=false');
$output = json_decode($geocodeFromAddr);
//Get latitude and longitute from json data
$data['latitude'] = $output->results[0]->geometry->location->lat;
$data['longitude'] = $output->results[0]->geometry->location->lng;
//Return latitude and longitude of the given address
if(!empty($data)){
return $data;
}else{
return false;
}
}else{
return false;
}
}
Use getLatLong() function like the following.
$address = 'White House, Pennsylvania Avenue Northwest, Washington, DC, United States';
$latLong = getLatLong($address);
$latitude = $latLong['latitude']?$latLong['latitude']:'Not found';
$longitude = $latLong['longitude']?$latLong['longitude']:'Not found';
To specify a Google API key in your request, include it as the value of a key parameter.
$geocodeFromAddr = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.$formattedAddr.'&sensor=true_or_false&key=GoogleAPIKey');
i hope this will help you.
It looks like the problem is with your data set. The part of the url that is being encoded as %A0 by urlencode($address) is a special non-breaking space character, rather than a regular space.
See here for more information on the difference:
Difference between "+" and "%A0" - urlencoding?
The %A0 character isn't accepted in this context, but you can do a quick str_replace() on the result of urlencode() to replace all these special space characters with the + symbols that regular spaces would have resulted in.
$geo = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=' . str_replace('%A0', '+', urlencode($address)));
I'm doing some tests with Google Maps API and for some reason there is valid addresses that return status INVALID_REQUEST for no apparent reason. Here is my php code
$request_url = "https://maps.googleapis.com/maps/api/geocode/xml?address=".urlencode($address);
$xml = simplexml_load_file($request_url) or die("url not loading");
$status = $xml->status;
return $status;
$address example:
$address = "R. Espírito Santo, Viana, ES, Brasil" // status INVALID_REQUEST
but if I try to access the URL directly it return status OK
https://maps.googleapis.com/maps/api/geocode/xml?address=R.%20Espírito%20Santo,%20Viana,%20ES,%20Brasil
For some addresses it returns OK, but for others INVALID_REQUEST using exactly the same code changing only the address. Accessing the URL directly always returns OK.
Any clues?
thank you
It was an character encoding problem. utf8_encode() (info) solved it.
$address = utf8_encode($address);
Use urlencode() (info)
$address = "R. Espírito Santo, Viana, ES, Brasil" // this can't be passed as query string
$request_url = "https://maps.googleapis.com/maps/api/geocode/xml?address=".urlencode($address); // urlencode() solves this
$xml = simplexml_load_file($request_url) or die("url not loading");
$status = $xml->status;
return $status;
I have read a few Q&As on here and additional articles, but cannot get this script to display the relevant values from the JSON return (which are long and lat from this Google Maps call).
<?php
// Address for Google to search
$address = 'London,UK';
// Get the map json data from Google Maps using the $address variable
$googleCall = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . $address .'&sensor=false';
$json = file_get_contents($googleCall);
//header("Content-type: application/json");
//echo $json;
echo $json->results->geometry->location->lat;
echo $json->results->geometry->location->lng;
?>
I am sure I am 99% there, just cannot see where the error is.
You can decode your json as an associative array and the access to all data with scopes
$address = 'London,UK';
$googleCall = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . $address .'&sensor=false';
$json = file_get_contents($googleCall);
$array = json_decode($json, true); //note second parameter on true as we need associative array
echo $array['results'][0]['geometry']['location']['lat'] . '<br>';
echo $array['results'][0]['geometry']['location']['lng'];
This will output
51.5112139
-0.1198244
try this out:
<?php
// Address for Google to search
$address = 'London,UK';
// Get the map json data from Google Maps using the $address variable
$googleCall = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . $address .'&sensor=false';
$json = file_get_contents($googleCall);
$json = json_decode($json);
//header("Content-type: application/json");
//echo $json;
echo $json->results->geometry->location->lat;
echo $json->results->geometry->location->lng;
?>
use json_decode here documentation
http://php.net/manual/en/function.json-decode.php
<?php
// Address for Google to search
$address = 'London,UK';
// Get the map json data from Google Maps using the $address variable
$googleCall = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . $address .'&sensor=false';
$json = file_get_contents($googleCall);
$json = json_decode($json);
echo $json->results[0]->geometry->location->lat;
echo $json->results[0]->geometry->location->lng;
?>