Shouse
Shouse

Reputation: 252

Can I reduce my amount of requests in Google Maps JavaScript API v3?

I call 2 locations. From an xml file I get the longtitude and the langtitude of a location. First the closest cafe, then the closest school.

$.get('https://maps.googleapis.com/maps/api/place/nearbysearch/xml?
location='+home_latitude+','+home_longtitude+'&rankby=distance&types=cafe&sensor=false&key=X',function(xml)
{
verander($(xml).find("result:first").find("geometry:first").find("location:first").find("lat").text(),$(xml).find("result:first").find("geometry:first").find("location:first").find("lng").text());
}
);

$.get('https://maps.googleapis.com/maps/api/place/nearbysearch/xml?
location='+home_latitude+','+home_longtitude+'&rankby=distance&types=school&sensor=false&key=X',function(xml)
{
verander($(xml).find("result:first").find("geometry:first").find("location:first").find("lat").text(),$(xml).find("result:first").find("geometry:first").find("location:first").find("lng").text());
}
);

But as you can see, I do the function verander(latitude,longtitude) twice.

function verander(google_lat, google_lng)
  {
        var bryantPark = new google.maps.LatLng(google_lat, google_lng);
        var panoramaOptions = 
        {
            position:bryantPark,
            pov:
            {
                heading: 185,
                pitch:0,
                zoom:1,
            },
            panControl : false,
            streetViewControl : false,
            mapTypeControl: false,
            overviewMapControl: false   ,
            linksControl: false,
            addressControl:false,
            zoomControl : false,
        }
        map = new google.maps.StreetViewPanorama(document.getElementById("map_canvas"), panoramaOptions);
        map.setVisible(true);
  }

Would it be possible to push these 2 locations in only one request(perhaps via an array)? I know it sounds silly but I really want to know if their isn't a backdoor to reduce these google maps requests.

FTR: This is what a request is for Google:

What constitutes a 'map load' in the context of the usage limits that apply to the Maps API? A single map load occurs when:

So I'm afraid it isn't possible, but hey, suggestions are always welcome!

Upvotes: 2

Views: 5735

Answers (1)

Anthony Hatzopoulos
Anthony Hatzopoulos

Reputation: 10537

Your calling places api twice and loading streetview twice. So that's four calls but I think they only count those two streetviews as once if your loading it on one page. And also your places calls will be client side so they won't count towards your limits.

But to answer your question there's no loop hole to get around the double load since you want to show the users two streetviews.

What I would do is not load anything until the client asks. Instead have a couple of call to action type buttons like <button onclick="loadStreetView('cafe')">Click here to see Nearby Cafe</button> and when clicked they will call the nearby search and load the streetview. And since it is only on client request your page loads will never increment the usage counts like when your site get's crawled by search engines.

More on those usage limits

The Google Places API has different usages then the maps. https://developers.google.com/places/policies#usage_limits

  • Users with an API key are allowed 1 000 requests per 24 hour period
  • Users who have verified their identity through the APIs console are allowed 100 000 requests per 24 hour period. A credit card is required for verification, by enabling billing in the console. We ask for your credit card purely to validate your identity. Your card will not be charged for use of the Places API.

100,000 requests a day if you verify yourself. That's pretty decent.

As for Google Maps, https://developers.google.com/maps/faq#usagelimits

You get 25,000 map loads per day and it says.

In order to accommodate sites that experience short term spikes in usage, the usage limits will only take effect for a given site once that site has exceeded the limits for more than 90 consecutive days.

So if you go over a bit not and then it seems like they won't mind.


p.s. you have an extra comma after zoom:1 and zoomControl : false and they shouldn't be there. Will cause errors in some browsers like IE. You also are missing a semicolon after var panoramaOptions = { ... } and before map = new

Upvotes: 5

Related Questions