Duke
Duke

Reputation: 36980

How do I save the google image map API picture to my server

Is it possible to save the google map image API as an image to my server?

Background: Google map image API is generating an image of the desired location. Every time I am calling for the external URL. Can I save the map image as an image(png, gif) on my server? This will improve my web page speed. I tried with curl but failed to copy the image

Requested url http://maps.googleapis.com/maps/api/staticmap?center=15719+OAKLEAF+RUN+DRIVE%2CLITHIA%2CFL%2C33547%2CUS&zoom=8&size=150x100&markers=color:blue|label:S|11211&sensor=false

Upvotes: 11

Views: 22518

Answers (4)

Sabbir
Sabbir

Reputation: 1374

This is really easy

just use

if(@copy('http://maps.googleapis.com/maps/api/staticmap?center=15719%20OAKLEAF%20RUN%20DRIVE,LITHIA,FL,33547,US&zoom=8&size=150x100&markers=color%3ablue%7Clabel%3aS%7C11211&sensor=false', '/where-u-want-to-save/your-given-name.png')){
echo "image-saved";
}else{
echo "failed"; }

image always returns as png format you may give image a unique name by lat lng value or using php time() function

Upvotes: 2

khizar ansari
khizar ansari

Reputation: 1556

you can easily read that url using file_get_contents

snippet for this will be

$url='http://maps.googleapis.com/maps/api/staticmap?center=15719+OAKLEAF+RUN+DRIVE%2CLITHIA%2CFL%2C33547%2CUS&zoom=8&size=150x100&markers=color:blue|label:S|11211&sensor=false';

file_put_contents('filename.png',file_get_contents($url));

Upvotes: 4

mishunika
mishunika

Reputation: 1916

you can try something like this:

<?php
$image = file_get_contents('http://maps.googleapis.com/maps/api/staticmap?center=15719%20OAKLEAF%20RUN%20DRIVE,LITHIA,FL,33547,US&zoom=8&size=150x100&markers=color%3ablue%7Clabel%3aS%7C11211&sensor=false'); 
$fp  = fopen('ae.png', 'w+'); 

fputs($fp, $image); 
fclose($fp); 
unset($image);
?>

Upvotes: 19

powtac
powtac

Reputation: 41050

Check the Maps API for mobile. There you will receive an Image instead of a HTML map.

Upvotes: -1

Related Questions