i've read that you can use either javascript or php with google maps api. so what are the pros and cons for each of them?
and if i got the geocodes stored in a database. should i get them with ajax and process them with javascript or should i use php?
it says in the FAQ that 15000 requests are allowed per day per ip. does this mean that EACH user has to run 15000 requests a day if im using javascript? sounds a lot. but if im using php instead, is it from the server's ip only, and thus 15000 for ALL users?
would be great if someone could shed a light on this topic.
The Google Maps API is a JavaScript library. However Google offers its Geocoding Services through a client-side API in JavaScript and though a server-side web service.
This is an example on how to use the JavaScript geocoder:
function initialize() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map_canvas"));
geocoder = new GClientGeocoder();
geocoder.getLatLng(
"London, UK",
function(point) {
if (point) {
map.setCenter(point, 13);
var marker = new GMarker(point);
map.addOverlay(marker);
}
}
);
}
}
The following is an example showing how to get the latitude and longitude of an address on the server-side using php:
$url = 'http://maps.google.com/maps/geo?q=London,+UK&output=csv&sensor=false';
$data = #file_get_contents($url);
$result = explode(",", $data);
echo $result[0]; // status code
echo $result[1]; // accuracy
echo $result[2]; // latitude
echo $result[3]; // longitude
You understood the free geocoding quota correctly. Server-side geocoding is limited to 15k requests per day per server, while client-side geocoding is limited to 15k requests per day per client. You would need the Google Maps API Premier to increase these limits.
I think you are confused. The only part of the API that you can use PHP with is the geocoding api - http://code.google.com/apis/maps/documentation/geocoding/index.html
Assuming you are asking whether you should do your geocoding with javascript or a server side language like PHP, best practice is to cache whatever geocoding you can into some sort of persistance layaer (xml/db/whatever) and minimise the number of client side geocode requests (because of the delay it will introduce)
Related
Geocode API allowed 2500 requests per day, but my site is not sending such requests in a single day. is my code sending automated requests?
function getmap($addrss,$location,$city){
$address = $addrss.' '.$city.' '.$location;
$prepAddr = urlencode($address);
$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
$output= json_decode($geocode);
$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng;
$retval = $lat.'*'.$long;
return $retval;
}
Response came
[error_message] => You have exceeded your daily request quota for this API.....[status] => OVER_QUERY_LIMIT )
Please provide any suggestion.
From the API Example I've looked up it specifically requests appending the API Key to the request URL, might that be it? Otherwise my guess would be accidental looped requests.
It would be best if the API Key was set outside this function in case other functions in this class / object want to access the API as well.
$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false&key='.$API_KEY);
Try to use google browser key as below in order to get more number of requests per day
http://maps.google.com/maps/api/geocode/json?key=YOUR_GOOGLE_MAP_API_KEY&address=los+Angels
Follow these steps to get GOOGLE API KEY,
Go to the Google Developers Console.
Create or select a project.
Click Continue to enable the API and any related services.
On the Credentials page, get a Browser key (and set the API
Credentials).
(Optional) Enable billing.
Note: If you have an existing Browser key, you may use that key.
To prevent quota theft, secure your API key following these best practices.
This function for Reveres Geocoding works well for me. I have to display the address in the information window of a marker, and there are more then 200 in one map. I built the code below and it works perfectly. However, Google's API request limit of 2500/day gets exceeded easily.
Is there any way in I can bulk request Google's API?
Our project is currently a small one, so buying more requests/day isn't an option for us.
<?php
function getaddress($lat,$lng)
{
$url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($lat).','.trim($lng).'&sensor=false';
$json = #file_get_contents($url);
$data=json_decode($json);
$status = $data->status;
if($status=="OK")
return $data->results[0]->formatted_address;
else
return false;
}
?>
No, you can't "bulk query" a bunch of addresses and have it only count as one request when using the standard API. Each lookup counts as a request.
There are other APIs that don't have limits, or allow more lookups, like MapQuest. If you're hitting the daily limit with Google, try using another API.
I cant get Google API to give the same answer for driving distance as when using Google maps. I use this data to test the accuracy of the API vs Google Maps. I am using cakephp but php code is also acceptable.
Test data for lat/long (not people I know)
(point1)
Address Lat: -38.041968
Address Long: 145.26416599999993
(point 2)
Address Lat: -37.988981
Address Long: 145.53447499999993
The distance between these 2 points on Google maps says 41.3km but on the Google api below for the same lat/long points I get 33.7km which is too inaccurate to rely on. What is going on with Google API as it doesnt seem to work?
$stlat = $student['Student']['address_lat'];
$stlong = $student['Student']['address_long'];
foreach ($tutors as $key => $item):
$tutlat = $item['Tutor']['address_lat']; //this is the lat/long above
$tutlong = $item['Tutor']['address_long'];
$testUrl = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=".$stlat.",".$stlong."&destinations=".$tutlat.",".$tutlong."&mode=driving&language=en&sensor=false";
$data1 = file_get_contents($testUrl);
$data = utf8_decode($data1);
$obj = json_decode($data1);
$dist2=$obj->rows[0]->elements[0]->distance->text;
debug($obj->rows[0]->elements[0]->distance->text); //km
endforeach;
The answer is that on google maps I am not getting the optimum distance, but on the API I am. So the problem is solved
I have 3 events:
https://www.facebook.com/491594114271958 (Mad Haus)
https://www.facebook.com/569226999799343 (Deuglish)
539504962802119/ (Piffle)
All are being fetched via the PHP
$config = array();
$config['appId'] = $appId;
$config['secret'] = $secret;
$config['fileUpload'] = false; // optional
$facebook = new Facebook($config);
$ev = $facebook->api('/'.$id."?fields=cover,description,location,name,owner,venue",'GET');
print_r($ev);
For some reason Mad Haus and Piffle do not return venue data but Deuglish does. All events return basic data such as title, description and start time. When I run them through the Graph API explorer the venue data is returned as expected but not through the PHP API, any ideas? I can not for the life of me see the difference with these 3 events.
Thanks,
Caroline
Well, if you don't want to wait for the Facebook developers to fix the bug, you can try the same by making a simple GET request (of course along with an Access Token) to Graph API using PHP cURL. Since you are able to retrieve the venues while using the Graph API explorer, I'm pretty sure you'll be able to get them by making a simple GET request using PHP cURL.
This however involves an overhead of parsing the received JSON object and dealing with errors will be a little difficult here. But you can give it a shot.
I need a function to get an nearest address or city from coordinates(lat,long) using google map api reverse geocoding and php... Please give some sample code
You need to use the getLocations method on the GClientGeocoder object in the Google Maps API
var point = new GLatLng (43,-75);
var geocoder = new GClientGeocoder();
geocoder.getLocations (point, function(result) {
// access the address from the placemarks object
alert (result.address);
});
EDIT: Ok. You are doing this stuff server side. This means you need to use the HTTP Geocoding service. To do this you will need to make an HTTP request using the URL format described in the linked article. You can parse the HTTP response and pull out the address:
// set your API key here
$api_key = "";
// format this string with the appropriate latitude longitude
$url = 'http://maps.google.com/maps/geo?q=40.714224,-73.961452&output=json&sensor=true_or_false&key=' . $api_key;
// make the HTTP request
$data = #file_get_contents($url);
// parse the json response
$jsondata = json_decode($data,true);
// if we get a placemark array and the status was good, get the addres
if(is_array($jsondata )&& $jsondata ['Status']['code']==200)
{
$addr = $jsondata ['Placemark'][0]['address'];
}
N.B. The Google Maps terms of service explicitly states that geocoding data without putting the results on a Google Map is prohibited.
You can display Geocoding API results on a Google Map, or without a
map. If you want to display Geocoding API results on a map, then these
results must be displayed on a Google Map. It is prohibited to use
Geocoding API data on a map that is not a Google map.