Kanishka Panamaldeniya
Kanishka Panamaldeniya

Reputation: 17586

Is it possible to call a Google API and get the map image and save the image in somewhere

I am currently working on sending a google map image in an html email .

Currently I am using static-maps-api to achieve my task . i will call the static maps api and set the image URL as src In an image tag .

<img src="https://maps.googleapis.com/maps/api/staticmap?markers=icon:http://mailadapterdev.vanapi.com/static/start.png|13.82,100.54&markers=icon:http://mailadapterdev.vanapi.com/static/waypt1.png|13.80,100.54&markers=icon:http://mailadapterdev.vanapi.com/static/waypt2.png|13.81,100.52&markers=icon:http://mailadapterdev.vanapi.com/static/waypt3.png|13.79,100.54&markers=icon:http://mailadapterdev.vanapi.com/static/dest.png|13.79,100.53&path=weight:3|color:blue|enc:cfjsAqvsdRnAMd@MZpDLpATpB?HGLMX?VV`CTjBr@L|ATlDf@hBXGl@CPTDtAR~Et@jF`AnLbBvKdB~@JbAR`BTdDd@pDj@l@H`Cb@vALFOJIr@yCxAgHPYTOREV?N@ZHrIxD|HjD~CxAxAn@j@XHDZy@hAoCdCiG~B{Gj@gBXeALWNU@[A[OWi@m@_@_@i@w@O[o@SSKK?}A]sBy@wBeAvBdArBx@|A\J?JOIsCJgC?[Io@KQ^Nn@TLDUfDC`AFp@RfAj@dAl@p@n@n@DHDN?TC`@IFW^MVYdAKZMb@uBdGaDfIiAnC[x@B@RHpCnAnAh@jChAnVrK|@`@Uh@iChG{D~J}B~Fe@tAq@fCcBfG_ApDMd@AXO`@Q^GCu@S{AAyIByEAaABc@@}A@kACeDImKWcKSmAGgAEoCCqECI?LlCH~DI_EMmCH?|@BdA?lA?nCBfADlAFrDHnEH|ELtIRjAB|AAb@A`ACxE@xICzA@l@Oh@a@p@}BhDoMd@yA~AcE`AiCtDsJhCeGHSHs@b@_Ej@cGbAuJ\mC|@sGb@gENoBF]XkAZuBX}CLoA@CbAJbDXl@Fm@GcDYcAKABMnAOzAI`AKr@_@pBQx@OnBUdCYbCk@tDCR~@VTL~@t@xBz@|GrB`I`CYnA_BpH{A`H&key=MY_API_KEY=400x400" >

before calling the static maps api , i will call the directions-api with the lat long and get the polyline , with polyline I get from the call directions API i will call the static maps API.

My Problem Is

When the number of way points increase ( more than 5 normally ) the polyline become very large and the URL will become long , if it is larger than 2048 characters , i cannot view my map . google does not identify it as a valid URL. so it is obvious .

My Questions Are

  1. Is there anyway to reduce the size of the ploy line ?
  2. Is there anyway to call a Google API and get the image and save it in my server.
  3. If I can do the second point, is it illegal to save google maps in the my server, instead of call google server to get the image every time I want.
  4. Is there any other ways I can achieve my task.

I am stuck in this problem for more than 2 weeks now. I almost tried everything. please help me to solve this issue .

Thank you in advance.

Upvotes: 2

Views: 3513

Answers (4)

Codemon72
Codemon72

Reputation: 23

Addressing question 3: no (as previously stated by xomena)

Here an update to where the relevant terms of service seem to be now:

3.2.3 Restrictions Against Misusing the Services.

(a) No Scraping. Customer will not export, extract, or otherwise scrape Google Maps Content for use outside the Services. For example, Customer will not: (i) pre-fetch, index, store, reshare, or rehost Google Maps Content outside the services; (ii) bulk download Google Maps tiles, Street View images, geocodes, directions, distance matrix results, roads information, places information, elevation values, and time zone details; (iii) copy and save business names, addresses, or user reviews; or (iv) use Google Maps Content with text-to-speech services.

(b) No Caching. Customer will not cache Google Maps Content except as expressly permitted under the Maps Service Specific Terms.

Upvotes: 1

Kanishka Panamaldeniya
Kanishka Panamaldeniya

Reputation: 17586

I will answer my own question - only for part 1 ,

A way to reduce the size of the polyline is to use polyline encoding. To see how it works you can use this interactive sample

For node.js I used simplify-path and polyline modules to implement this.

STEPS

  1. cal the Directions API with your lat long data and get the overview polyline
  2. decode the ploy line using polyline module
  3. simplify the path using simplify-path module.
  4. finally encode the details you got from 3 rd step using encode in polyline module

Example

var simplify_path = require("simplify-path");
var polyline      = require("polyline");

var poly_line = "overview polyline from directions API";

var path = polyline.decode(poly_line);
var tolerance = 10;

path = simplify_path(path, tolerance)
var new_polyline = polyline.encode(path);

console.log("old_plyline "+JSON.stringify(poly_line));
console.log("new_polyline "+JSON.stringify(new_polyline));

So you will get a simplified relatively short ploy line .

Seems pretty simple :D

Upvotes: 4

Soldeplata Saketos
Soldeplata Saketos

Reputation: 3461

I will answer you inline:

  1. Is there anyway to reduce the size of the poly line ?
    Yes, Google uses encoded polylines, and here you can find the process how they are created. You can find also an Interactive Polyline Encoder Utility, and you could also consider using the Google's library called Geometry and its method encodePath(). Find reference here.
  2. Is there anyway to call a Google API and get the image and save it in my server.
    No, as stated by xomena

  3. If I can do the second point, is it illegal to save google maps in the my server, instead of call google server to get the image every time I want?
    No, as stated by xomena

  4. Is there any other ways I can achieve my task.
    Yes, as stated at my answer to your first question, you have the possibility to use Google's Geometry library

Upvotes: 3

xomena
xomena

Reputation: 32178

Addressing questions 2 and 3. You cannot save an image on your server. It is prohibited by Terms of Service.

Look at paragraph 10.1 (a) of ToS

No access to APIs or Content except through the Service. You will not access the Maps API(s) or the Content except through the Service. For example, you must not access map tiles or imagery through interfaces or channels (including undocumented Google interfaces) other than the Maps API(s).

https://developers.google.com/maps/terms#10-license-restrictions

Upvotes: 2

Related Questions