Click here to Skip to main content
15,889,861 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok I'm utterly confused. Google Maps API works FINE on local and live server, but not on my client's server. It throws the OVER_QUERY_LIMIT status, but it's NOT over the limit because it works fine on the other two servers when requesting from the url.

Here is my code to see what's going on:

JavaScript
// Load PHP File
function downloadUrl(url, callback) {
  var request = window.ActiveXObject ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest;

  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      request.onreadystatechange = doNothing;
      callback(request.responseText, request.status);
    }
  };

  request.open('GET', url, true);
  request.send(null);
}

/*
 * Search locations near an address with a given radius and category
 * @param address    Address user types in (full or zip code)
 * @param radius     Radius drop down (25, 50, 75, etc)
 * @param category   Category drop down
*/
function searchLocationsNear() {
  // clear current locations
  clearLocations();

  var radius = document.getElementById('radius').value;
  var category = document.getElementById('category').value;
  var address = document.getElementById('address').value;
  var searchURL = 'ajax/getPlaces.php?addr=' + address + '&rad=' + radius + '&cat=' + category;

  // generate xml and markup map
  downloadUrl(searchURL, function(data) {
    var xml = parseXml(data);
    var markerNodes = xml.documentElement.getElementsByTagName('marker');
    var bounds = new google.maps.LatLngBounds();
    for (var i = 0; i < markerNodes.length; i++) {
      // set keys with marker nodes to pass to view
      var markerNum = i;
      var id = markerNodes[i].getAttribute('id');
      var logo = markerNodes[i].getAttribute('logo');
      var city = markerNodes[i].getAttribute('city');
      var state = markerNodes[i].getAttribute('state');
      var zip = markerNodes[i].getAttribute('zip');
      var phone = markerNodes[i].getAttribute('phone');
      var email = markerNodes[i].getAttribute('email');
      var web_link = markerNodes[i].getAttribute('web_link');
      var name = markerNodes[i].getAttribute('name');
      var address = markerNodes[i].getAttribute('address');
      var distance = parseFloat(markerNodes[i].getAttribute('distance'));
      var latlng = new google.maps.LatLng(parseFloat(markerNodes[i].getAttribute("lat")),parseFloat(markerNodes[i].getAttribute("lng")));

      // load view file
      $( '#listings' ).append($('<div>').load( 'ajax/listingFind.php', {
          id: id, 
          name: name,
          logo: encodeURIComponent(logo),
          address: address,
          city: city,
          state: state,
          zip: zip,
          phone: phone,
          email: email,
          web_link: encodeURIComponent(web_link),
          distance: distance,
          marker: markerNum
        } ));
      createMarker(latlng, name, address + '<br />' + city + ', ' + state + ' ' + zip);
      bounds.extend(latlng);
      fitToMarkers(bounds);
    }

    map.fitBounds(bounds);
    $('.pag-borders').show();
  });
}


getPlaces.php:

PHP
include_once('../../../app/scripts/config.php');
$googleMaps = new GoogleMaps();
$listingObject = new Listing();

// search places
$results = $googleMaps->get_places(trim($_GET['addr']), trim($_GET['rad']), array('category' => trim($_GET['cat']), 'status' => $listingObject->get_approved()));

// get xml
$xml = $googleMaps->generate_xml($results);

echo $xml;


get_places method:

PHP
/*
	 * Sets geocode lat and lng
	 * @param address     Street Address
	 * @param city        City
	 * @param state       State
	 * @param zip         Zip
	*/
	public function get_geocode($address) {

		$request_url = 'http://maps.googleapis.com/maps/api/geocode/json' . '?address=' . urlencode($address) . '&sensor=' . $this->sensor;

		$request = file_get_contents($request_url);
		$response = json_decode($request, true);

		if($response['status'] == 'OK') {
			// success
			$geometry = $response['results'][0]['geometry'];

			// Format:: Longitude, Latitude, Altitude
			$this->lat = $geometry['location']['lat'];
			$this->lng = $geometry['location']['lng'];

			return true;
		} else {
			// fail
			return false;
		}

	}

/*
	 * Get stores within mile radius of zip code
	 * @param zip      Zip Code
	 * @param radius   Mile Radius
	 * @param args     Other arguments such as category drop down
	*/
	public function get_places($zip, $radius, array $args) {

		// generate lat and lng for zip
		$this->get_geocode($zip);

		// start building query
		$this->select(array('id', 'name', 'address', 'city', 'state', 'zip', 'phone', 'email', 'web_link', 'user_id',
			'logo', 'lng', 'lat', '( 3959 * acos( cos( radians('.$this->get_lat().') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('.$this->get_lng().') ) + sin( radians('.$this->get_lat().') ) * sin( radians( lat ) ) ) ) AS distance'));
		$this->table($this->table);

		// see if any additional args exist - no for loop since defined indexes in array reference database columns
		if(count($args) > 0) {

			// set your counter
			$count = 0;

			// loops through args
			foreach($args as $key => $value) {
				if($count == 0) {
					$this->where($key, '=', $value);
				} elseif($count > 0) {
					$this->and_where($key, '=', $value);
				}
				$count++;
			}
		}
		
		// finish query build
		$this->having('distance', '<', $radius);
		$this->order_by('distance');

		$results = $this->all();

		return $results;

	}
Posted
Updated 16-Dec-13 3:34am
v3

1 solution

Ok the answer is USE CLIENT SIDE geocoding! Do not use server side. That's a way you can avoid the query limit.

Took me forever to figure this out, but just a few changes to request it client side and it works perfectly =)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900