Google geocoder limits - php

Is there a way of sending multiple addresses to through one geocode request:
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
addMarker(results[0].geometry.location);
var the_string = generate_string(elements, a, address);
infowindow[a] = new google.maps.InfoWindow({
content: the_string
});
google.maps.event.addListener(markersArray[a], "click", function() {
infowindow[a].open(map, markersArray[a]);
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
where address contains an array of addresses. Reason is I am coming across "OVER_QUERY_LIMIT" addressed here OVER_QUERY_LIMIT while using google maps and here Google Map Javascript API v3 - OVER_QUERY_LIMIT Error
I was wondering if i should start using the server side version of the client or continue with the javascript api and get help with sending an array of addresses through the geocoder call?
In your opinion or experience, if i have an application that you can type endless amounts of addresses into fields and then press a "plot to map" button, how would you go about doing this in terms of api calls, geocoding etc?
EDIT:////////////////////////
I'm now using this code:
class geocoder{
static private $url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=";
static public function getLocation($address){
$url = self::$url.urlencode($address);
$resp_json = self::curl_file_get_contents($url);
$resp = json_decode($resp_json, true);
if($resp['status']='OK'){
return $resp['results'][0]['geometry']['location'];
}else{
return false;
}
}
static private function curl_file_get_contents($URL){
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_URL, $URL);
$contents = curl_exec($c);
curl_close($c);
if ($contents) return $contents;
else return FALSE;
}
}
and calling it like so:
$address = urlencode($field_set["value"]);
$loc = geocoder::getLocation($address);
in a loop of every address being placed in (in this case 20 times).
It only places 10 markers on the map regardless of the loop correctly having 2 different lngs and lats.
It returns null for the last 10 lat lngs :s
EDIT:///////////////////////////
I have placed a sleep in the loop that calls this:
$loc = geocoder::getLocation($address);
every 8th geocoding, it pauses for a second then continues.. this seems to allow all geocoder locations to be rendered correctly... but it seems like a fluff.
Any ideas?

The best way to to this with geocode, is to store the longitude and latitude value on your server, so you won't have to send a bunch geocode requests every time your script runs

No, the address have to be an address as specified in the documentation.
The geocoder will return an array of results, but you can't input an array of addresses to geocode/reverse-geocode, isn't too logic anyway and people would use it to exploit the limits.
You shouldn't worry too much on the query_limit, because it's per IP/day as long as it's javascript api and no backend. So if you have multiple users using this feature the limit is set to them individually and it doesn't sum up.
If you are going over the limit by a lot because you have a local environment, then you might need to hire the google maps for business services that have up to 100k requests daily I think.

Related

My code for downloading longitude and latitude is now returning null value for existing areas

I have a code for downloading longitude and latitude coordinates from google maps, it was working before but now it's returning null value. Below is my code;
<?php
//Optaining Latitude and Longitude
$address = $title;
$url = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=".urlencode($address)."&sensor=false");
$response = json_decode($url);
$latitudee = "";
$longitudee = "";
if ($response->status == 'OK') {
$latitudee = $response->results[0]->geometry->location->lat;
$longitudee = $response->results[0]->geometry->location->lng;
}
echo $latitudee;
?>
Google now requires you to include API key for calling geocode API. Change the 'url' variable as follows-
$url = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=".urlencode($address)."&key=YOUR_API_KEY");
The sensor parameter is no longer required to pass as per google developer guide
https://developers.google.com/maps/documentation/geocoding/intro
If you don't have an API key, you can get it here-
https://developers.google.com/maps/documentation/javascript/get-api-key
Also don't forget to restrict your key once you have one.
Returned error message states what is the problem here:
Keyless access to Google Maps Platform is deprecated.
Please use an API key with all your API calls to avoid service interruption.
For further details please refer to http://g.co/dev/maps-no-account
After recent changes to google maps, you will need to create an account to use google maps API, this will generate an API key and you have to append it to URL that you use
"http://maps.google.com/maps/api/geocode/json?address=".urlencode($address)."&key=YOUR_API_KEY"
you must Formatted your 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:
if ($output->status == 'OK') {
$data['latitude'] = $output->results[0]->geometry->location->lat;
$data['longitude'] = $output->results[0]->geometry->location->lng;
}
I was able to find where the problem was, Google changed the API url to the one below;
$url = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($add`enter code here`ress)."&key=YOUR_API_KEY");

i am using a rest API i need to parse a forever changing json result

Okay so here goes i am using a rest api called strichliste
i am creating a user credit payment system
i am trying to grab a users balance by username problems is
my restapi i can only get the blanace via its userid
I have created a bit of php that grabs all the current users and the corresponding id and balance using this below
function getbal(){
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://example.io:8081/user/'
)
);
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
print_r($resp);
}
this is the resulting respinse i get after using this in my main php script
<? getbal(); ?>
result --- #
{
"overallCount":3,
"limit":null,
"offset":null,"entries":[
{"id":1,
"name":"admin",
"balance":0,
"lastTransaction":null
},
{"id":2,
"name":"pghost",
"balance":0,
"lastTransaction":null
},
{"id":3,
"name":"sanctum",
"balance":0,
"lastTransaction":null
}
]
}
as you can see there are only currently 3 users but this will grow everyday so the script needs to adapt to growing numbers of users
inside my php script i have a var with the currently logged in use so example
$user = "sanctum";
i want a php script that will use the output fro gatbal(); and only output the line for the given user in this case sanctum
i want it to output the line in jsondecode for the specific user
{"id":3,"name":"sanctum","balance":0,"lastTransaction":null}
can anyone help
$user = "sanctum";
$userlist = getbal();
function findUser($u, $l){
if(!empty($l['entries'])){
foreach($l['entries'] as $key=>$val){
if($val['name']==$user){
return $val;
}
}
}
}
This way, once you have the list, and the user, you can just invoke findUser() by plugging in the userlist, and the user.
$userData = findUser($user, $userlist);
However, I would suggest finding a way to get the server to return only the user you are looking for, instead of the whole list, and then finding based on username. But thats another discussion for another time.

get the category of an android app - at server side

I have this case where in our in-house app is capturing all the applications used by the user, and uploads the list to our cloud server, where I need to categorize them by the "category" at play store.
It would have been better if the android code could also find the category of an application by its package name, but looks like my developers were having problem doing this at the android end.
How do we find out the category of an application by using just its package name?
Lets just pick one of the package name say "com.facebook.katana", and lets google it.
the first result (if its a valid android app) would be the play url like - https://play.google.com/store/apps/details?id=com.facebook.katana, lets hit this url and look at the source of it.
a span like this has what we need -
<span itemprop="genre">Social</span>
Now lets write a dirty index game in php to get the genre or category of our package, i hope y'all find it easy to understand, coz it ain't rocket science
the code is available here https://gist.github.com/brijrajsingh/6915711
and is given below as well.
Note - I would also suggest that once you have got a category for a package name you better store it in some db or memcache, so you don't need to hit it again and again for diff packages, also the code might stop running if you bombard the google servers, so go easy on it.
<?php
class PackageCategory
{
protected static $playurl = 'https://play.google.com/store/apps/details?id=';
public function PackageCategory($packageName) {
$this->packageName = $packageName;
}
public function getPackageCategory()
{
$result = $this->makeRequest(self::$playurl.$this->packageName);
//starting index of category tag
$indexstart = strpos($result,"<span itemprop=\"genre\">");
//ending index of category tag
$indexend = strpos($result,"</span>",$indexstart);
$category = substr($result,$indexstart+23,$indexend - ($indexstart+23));
echo $category;
}
//curl to the play store
/**
* Makes request to AWIS
* #param String $url URL to make request to
* #return String Result of request
*/
protected function makeRequest($url) {
//echo "\nMaking request to:\n$url\n";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}
if (count($argv) < 2) {
echo "Usage: $argv[0] PackageName\n";
exit(-1);
}
else {
$packageName = $argv[1];
}
$packageCategory = new PackageCategory($packageName);
$packageCategory->getPackageCategory();
?>
Yes, there seems to be a way:
Since you already found the names of the application packages.
You can use this unofficial googleplay api to get the info you need about the app.

How to properly store and lookup areacode from a phone number

How do I store areacodes (npa-nxx) in a database for fast lookup?
Here's the deal: I have a variable that contains a phone number and I need to look in a database for the city attached to that phone number.
The problem is, different countries have different formats.
Canada/USA: +19055551234 (+1 > Country, 905 > Area Code, 555 > City Code)
France: +33512345678 (+33 > Country, 5 > Areacode, 1 > City, Other numbers > Subscriber number)
and so on (infos based on wikipedia)
I created a table called 'npanxx' that contain the list of area codes, city code and city attached to each one (with the id of the country and the province/state id):
CountryId, RegionId, PrimaryCity, npa, nxx, fnpanxx
1 11 Acton Vale 450 236 +1450236
I am thinking about the following procedure:
Get all country codes from sql to php array
Go through each entry and check if there's a match from the beginning of the phone number
When (If there's) a match is found
Remove the beginning of the phone number
Get all npa-nxx that belong to that contry and put them in a php array
Go through each value of the array to find a matching beginning
When (If there's) a match is found
Remove the beginning of the phone number
Store data in different variables like: $country = 'Canada'; $city = 'Acton Vale'...
etc, etc.
First mistake (I think): To much database requests (the npanxx table contain 3000 records for only one province in Canada)
Second mistake: I'm pretty sure there's no need to go through each and every npa-nxx code
another problem: It's not sure that if the phone number is a France one that this procedure will work.
And... If there's an entry for, let's say 336 and another for 3364, it might give the wrong result.
Do you have any idea how I can solve this problem ? (I don't ask for any code, I don't want to to do the work for me, I would like some clues though)
This is for a personnel project to make donation for Multiple Sclerosis Society of Canada and would really like to finish that project :)
I would think maybe some kind of set of reg-exes or other pattern matches to whiddle down your options in terms of search. Just some basic way or "guessing" at the possibilities instead of searching all of them.
Here's a small script I wrote in PHP to return the NPA/NXX as a JSON object in real time from area-codes.com.
It returns some very useful data. It's only for the NANP, so it doesn't do so well trying to discern international calls. For that, I would suggest making a table of all international country codes and the appropriate methods to dial them, internationally.
Additionally, network exchange operators demand an international dial code (like 011 for the USA, or + for cell phones, in general) to figure out if the number is international, and then take the steps, above, to figure out where you're trying to go. You could add this constraint into the input field and be done with it.
If you're trying to just get NPA/NXX information in the North American Numbering Plan, though, this script should be very helpful.
Just an aside, the area-codes.com counts online lookups among their free services, and I have found nothing on the site to suggest that this code violates that policy. But this code can be retooled to gather data from other providers, none-the-less.
<?php
// Small script to return and format all data from the NPA/NXX info site www.area-codes.com
// Returns a JSON object.
error_reporting(E_NONE);
$npa = $_GET['npa'];
$nxx = $_GET['nxx'];
function parseInput($input) {
$v = new DOMDocument();
$v->formatOutput = true;
$v->preserveWhiteSpace = false;
$v->loadHTML($input);
$list = $v->getElementsByTagName("td");
$e = false;
$dataOut = array();
$p = "";
foreach($list as $objNode) {
if (!$e) {
$p = $objNode->nodeValue;
$p = strtolower($p);
$p = preg_replace("%[+ .:()\/_-]%", "", $p);
$p = str_replace("\xc2\xa0", "", $p);
$p = trim($p);
}
else {
if ($p != "") {
$d = trim($objNode->nodeValue);
if ($d != "") $dataOut[$p] = $d;
}
$p = "";
}
$e = !$e;
}
return $dataOut;
}
function getNPANXX($npa, $nxx) {
$url = "www.area-codes.com/exchange/exchange.asp?npa=$npa&nxx=$nxx";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
curl_close($ch);
$i = strpos($response, "<h3>AreaCode/Prefix $npa-$nxx Details</h3>");
$i = strpos($response, "<table width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"0\">", $i);
$e = strpos($response, "</table>", $i);
$scan = substr($response, $i, ($e-$i) + 8);
return parseInput($scan);
}
$result = getNPANXX($npa, $nxx);
if (!isset($result['npaareacode'])) {
$result = array("error" => "invalid");
}
echo json_encode($result);
die;
?>
For the query npanxx.php?npa=202&nxx=520 the JSON outputs as follows:
{
"npaareacode":"202",
"nxxusetype":"WIRELESS",
"nxxprefix":"520",
"nxxintroversion":"11\/16\/2007",
"city":"WASHINGTON",
"state":"DC",
"latitude":"38.901",
"county":"DISTRICT OF COLUMBIA",
"longitude":"-77.0315",
"countypopulation":"0",
"lata":"236",
"zipcode":"20005",
"zipcodecount":"0",
"ratecenter":"WSHNGTNZN1",
"zipcodefreq":"0",
"fips":"11001",
"ocn":"6664",
"observesdst":"Unknown",
"cbsacode":"47900",
"timezone":"Eastern (GMT -05:00)",
"cbsaname":"Washington-Arlington-Alexandria, DC-VA-MD-WV",
"carriercompany":"SPRINT SPECTRUM L.P."
}
For your example npanxx.php?npa=450&nxx=236 the data returned is a little bit limited because it's Canada and Canada doesn't provide all the FIPS and carrier data like the United States does, but the returned data still quite useful:
{
"npaareacode":"450",
"nxxusetype":"WIRELESS",
"nxxprefix":"236",
"nxxintroversion":"2002-08-04",
"city":"ACTON VALE",
"state":"QC",
"latitude":"45.6523",
"longitude":"-72.5671",
"countypopulation":"51400",
"lata":"850",
"zipcodecount":"0",
"zipcodefreq":"-1",
"observesdst":"Unknown",
"timezone":"Eastern (GMT -05:00)"
}

Geographical coordinates to street names

is any way (with a rest API will be awesome) to get the Street name corresponding to a Geographical coordinate? I think the name is geocoding, do google have this API? Im PHP developer.
Ex.
<?php
cor="38.115583,13.37579";
echo(geoname(cor)); // this prints: Foro Umberto I - 90133 Palermo
?>
So the output of the function is the street name, the postal code and the city. Thanks for any help and scripts examples!
Yes, just use the "Reverse Geocoding" function in the Google Maps API: http://code.google.com/apis/maps/documentation/geocoding/#ReverseGeocoding
Here's some example code:
$lat="38.115583";
$long = "13.37579";
$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$long&sensor=false";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_ENCODING, "");
$curlData = curl_exec($curl);
curl_close($curl);
$address = json_decode($curlData);
print_r($address);
Here's my PHP function I used for doing a Reverse Geocode lookup for a street address using the Google MAP API. Note, this example gets the output from Google in JSON, but I am doing a simple parse in PHP.
/*
* Use Google Geocoding API to do a reverse address lookup from GPS coordinates
*/
function GetAddress( $lat, $lng )
{
// Construct the Google Geocode API call
//
$URL = "http://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lng}&sensor=false";
// Extract the location lat and lng values
//
$data = file( $URL );
foreach ($data as $line_num => $line)
{
if ( false != strstr( $line, "\"formatted_address\"" ) )
{
$addr = substr( trim( $line ), 22, -2 );
break;
}
}
return $addr;
}
Andrew
Team at OpenGeoCode.Org
btw> Google does have restrictions on using their APIs for commercial purposes. Basically, you need to display the result on a google map.
You could also take a look at using Yahoo!'s PlaceFinder API, which offers reverse geocoding. A minimal example of a call to the API (asking it to return the lightweight data-interchange format du jour, JSON) might look like:
$url = 'http://where.yahooapis.com/geocode?location=55.948496,-3.198909&gflags=R&flags=J';
$response = json_decode(file_get_contents($url));
$location = $response->ResultSet->Results[0];
print_r($location);
Which outputs the first result (hopefully there is one!) which contains properties like street, postal and city.
Another way of using the PlaceFinder API is through Yahoo!'s YQL API, which allows you to make use of SQL-like queries against "data tables" (often, other APIs).
Such a query might look like:
SELECT * FROM geo.placefinder WHERE text="55.948496,-3.198909" AND gflags="R"
(Try this in the YQL console interface)
To make a call to YQL with that query, from PHP, is very similar to the earlier example and should print the same information.
$query = 'SELECT * FROM geo.placefinder WHERE text="55.948496,-3.198909" AND gflags="R"';
$url = 'http://query.yahooapis.com/v1/public/yql?q='.urlencode($query).'&format=json';
$response = json_decode(file_get_contents($url));
$location = $response->query->results->Result;
print_r($location);
As #Elijah mentions, there's a lot of restrictions on the Maps API I gave in my last answer. You're only actually supposed to use it for Google Maps Applications. If that's a problem then you can also try the AdWords API. It also has a similar service but Google charges for it so there's less restriction.

Categories