Google maps api

Google maps api description

General overview
Google maps API is a technology, that allows developers to work with Google Maps content and functionality. Interaction goes by *xml-data* exchange with Google servers. In this article you will see how to get and set user marks.

Access to google aps through the browser
Before starting work with Google maps you need to create Google account by this link https://www.google.com/accounts/NewAccount. You will be asked to write your email, password, choose country and confirm that you aren’t bot (write letters from picture). If there will be some problems you also will be asked to write your phone number. After (if you had problems) you will receive sms from Google with confirmation code that allow you to continue registration. When you complete registration you can start creating your maps. You can add many types of content to your maps - marks, paths, polygons and also add additional content (maplets).

Data structures
Maps API allows getting user maps content in KML files format. *KML(Keyhole Markup Language)* is a markup language based on XML and used mostly in Google Earth. In kml documents user`s information, such as marks, paths, polygons and its graphics data, is stored using special tags. There are sample of small kml-file of map with one mark, path and polygon below.    1  <![CDATA[]]>

   http://maps.gstatic.com/intl/ru_ALL/mapfiles/ms/micons/blue-dot.png      7300CC33 5        40000000      3      <PolyStyle> 730000FF 1      1     </PolyStyle> </Style> <Placemark> 2    <![CDATA[]]> <styleUrl>#style2</styleUrl> <Point> -103.007812,38.891033,0.000000    </Point> </Placemark> <Placemark> 3    <![CDATA[]]> <styleUrl>#style1</styleUrl> <LineString> 1        -103.008514,38.891293,0.000000        -103.007767,38.891361,0.000000        -103.007263,38.891045,0.000000        -103.007858,38.890747,0.000000        -103.008186,38.891022,0.000000        -103.008514,38.891293,0.000000    </LineString> </Placemark> <Placemark> 4    <![CDATA[]]> <styleUrl>#style3</styleUrl> <Polygon> <outerBoundaryIs> <LinearRing> 1            -103.008125,38.891457,0.000000            -103.008034,38.891335,0.000000            -103.007835,38.891476,0.000000            -103.007462,38.891350,0.000000            -103.008049,38.891167,0.000000            -103.008125,38.891457,0.000000        </LinearRing> </outerBoundaryIs> </Polygon> </Placemark> </Document>

Another sample - map with three marks:

<?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://earth.google.com/kml/2.2"> <Document> 1  <![CDATA[]]> <Style id="style3">   http://maps.gstatic.com/intl/ru_ALL/mapfiles/ms/micons/blue-dot.png </Icon> </IconStyle> </Style> <Style id="style2">   http://maps.gstatic.com/intl/ru_ru/mapfiles/ms/micons/blue-dot.png </Icon> </IconStyle> </Style> <Style id="style1">   http://maps.gstatic.com/intl/ru_ru/mapfiles/ms/micons/blue-dot.png </Icon> </IconStyle> </Style> <Placemark> 2    <![CDATA[]]> <styleUrl>#style3</styleUrl> <Point> -103.007812,38.891033,0.000000    </Point> </Placemark> <Placemark> 3    <![CDATA[]]> <styleUrl>#style2</styleUrl> <Point> -103.007576,38.891438,0.000000    </Point> </Placemark> <Placemark> 4    <![CDATA[]]> <styleUrl>#style1</styleUrl> <Point> -103.007645,38.891445,0.000000    </Point> </Placemark> </Document>

Authentication method
Google use https authentication using POST http method. Below you can see simple request for authentication:

POST /accounts/LoginAuth HTTP/1.1 Host: www.google.com content-type: application/x-www-form-urlencoded GALX=Erpd_C6PB_8&Email=user_email&Passwd=password&PersistentCookie=yes&rmShown=1&signIn=%D0%92%D0%BE%D0%B9%D1%82%D0%B8&asts=

If authentication is successful, server returns such document:

<HTML> <HEAD> <TITLE>Moved Temporarily</TITLE> </HEAD> <BODY BGCOLOR="#FFFFFF" TEXT="#000000"> <H1>Moved Temporarily</H1> The document has moved <A HREF="https://www.google.com/accounts/CheckCookie?chtml=LoginDoneHtml">here</A>. </BODY> </HTML>

And such cookie-string(they will have another values in your case):

SID=DQAAAG0AAABar1L724cF1ITPeprvO4kCuVjVvNrGtN4GStjYG8Ed_tmmIX4gIMOoP6aAQyfch33y3BMr0IYm2t9ut9FFJLGApfN4gLr_P9FlbuftdvnGgRNqEjkSFnS1kbqtDJ8Z23TEb-UM_avamq55pWhH8DzR LSID=DQAAAG4AAAAwvDJoILE10c4gRuO7xJGrnklfvqhJU9oeMAxeMsDJzbhDj43SKECs7zVRPn0Z0tccvrvOPoDikbsuSwobCDHYSFEnbUirBpKMNGZp8aSjEGKnj1Avh38mED0UwxbMWpZJYhbAXSgJeLhcLjoEuCie GAUSR=api.example@gmail.com HSID=AdfATLK2fDrPkJqkt SSID=AuDXi4G29QxAFxnL-

They are necessary in further work. Next sample is a code snippet sending authentication request and getting back cookies (created in QtCreator, library version 4.5.2, user_email and password is your email and password):

//.. void MainWnd::copyCookies(const QHttpResponseHeader& h){ cookies=h.allValues("Set-Cookie"); //Deleting domain, expire date and such other information from cookie string for (QStringList::iterator i=cookies.begin;i!=cookies.end;i++)i->remove(i->indexOf(";"),i->length+1-i->indexOf(";")); disconnect(client,SIGNAL(responseHeaderReceived(const QHttpResponseHeader&)),0,0);// Disconnecting slot until next authentication } //.. QHttp client; QString buffer="GALX=Erpd_C6PB_8&Email="+user_email+"&Passwd="+password+"&PersistentCookie=yes&rmShown=1&signIn=%D0%92%D0%BE%D0%B9%D1%82%D0%B8&asts="; //Making POST-data string QHttpRequestHeader header("POST", "/accounts/LoginAuth");//Request header connect(client,SIGNAL(responseHeaderReceived(const QHttpResponseHeader&)),this,SLOT(copyCookies(const QHttpResponseHeader&))); //Connecting slot for making received cookie copy client.setHost("www.google.com",QHttp::ConnectionModeHttps,443); //Setting connection type header.setValue("Host", "www.google.com");//Setting host field in request header.setContentType("application/x-www-form-urlencoded"); client.request(header,buffer.toUtf8,0);//Sending request and POST-data //..

Getting marks from google
How I said before, Google allows to get user maps in kml format by adding “_&output=kml_” to map URL and make a GET request. The answer for this request will be map file. Below you can see code snippet that finds all marks on map with specified coordinaties(mapUrl is URL to your map, coords - coordinates that we find):

//..

QStringList cookie;

void MainWnd::done{ QDomDocument xml("kml"); QString labelCoords=coords;//Coordinates that we find (“latitude, longitude”) QDomNodeList coordinates; QDomNode y; int i,count; labelCoords=labelCoords.right(labelCoords.length-labelCoords.indexOf(",")-1) +","+labelCoords.left(labelCoords.indexOf(","))+",0.000000";//Write coordinates in other format “ longitude,latitude” and adding altitude above sea level // file->close; file->open(QIODevice::ReadOnly);//File opening for reading xml.setContent(file);//Load data from file to QDomDocument object file->close; coordinates=xml.elementsByTagName("coordinates");//Get list of all elements with tag “coordinates” count=coordinates.count; for (i=0;i<count;i++){ y=coordinates.item(i); if (y.firstChild.toText.data==labelCoords){//If specified coordinaties found QDomNodeList names,descriptions; names=xml.elementsByTagName("name");// Get list of all elements with tag “name” descriptions=xml.elementsByTagName("description");// Get list of all elements with tag “description” //Below I add one to i because first elements with “name” and “description” tag is name and description of map str+="Name: "+names.item(i+1).firstChild.toText.data+ +"\nCoordinates: "+labelCoords +"\nDescriptions: "+descriptions.item(i+1).firstChild.toText.data+"\n"; //In string str will saved all marks with specified coordinates } } disconnect(client,SIGNAL(done(bool)),0,0); } //.. QHttpRequestHeader header("GET",mapUrl+ "&output=kml");//Creating request header and adding to URL string"&output=kml" QString cookieStr;//cookie string connect(client,SIGNAL(done(bool)),this,SLOT(done));//Connecting slot for working with data file=new QFile;//Creating file for map file->setFileName("map.xml"); file->open(QIODevice::WriteOnly);//Set file open mode and open it client->setHost("maps.google.com"); header.setValue("Host","maps.google.com"); for (QStringList::const_iterator i=cookies.begin;i!=cookies.end;i++) cookieStr+=*i+"; ";//Adding data to cookie string header.setValue("Cookie",cookieStr);//Adding it to header header.setContentType("application/x-www-form-urlencoded");//Set ContentType field in request client->request(header,0,file);//Sending request //..

Setting marks to google
To set your own mark on your Google map, you need send POST-request with such data:

<ms auth="a53f7031:zOKS-bgigNIS7vNQoTjhiMseYmc" retry="0"> <Request action="3" mapid="MAPID" type="0"> <EditInfo member="3" value="icon_url"/> <EditInfo member="15" value="32" /> <EditInfo member="16" value="32" /> <EditInfo member="14" value="shadow_url" /> <EditInfo member="2" value="COORDS" /> <EditInfo member="0" value="NAME" /> <EditInfo member="1" value="DESCRIPTION" /> </Request> </ms>

and auth identifier mast be new. To get it we need send this request with old identifier. Server will return this web-page, that contains identifier that we need(“auth_token”):

{responses:[{rc:0,auth_token:"dd72e75a:89MC_gcEPp7--zOxs94d6LOb-0U"}]}

This code snippet shows how set mark on your maps:

//.. hasAuth=0;//This flag shows is there new auth identifier or not

//.. void MainWnd::doneS{ file->close; if (hasAuth==0) {//If there isn’t new auth QTextStream tmp(file);//Load tmp file content to text stream tmp.setCodec("UTF-8"); file->open(QIODevice::ReadOnly); auth=QString(tmp.readAll);//Adding tmp content to string auth file->close; auth.remove(0,auth.indexOf("\"")+1);//Extracting identifier auth.remove(auth.indexOf("\""),auth.length-auth.indexOf("\"")+1); hasAuth=1;//Set flag to true(1) on_setMark_pressed;//Calling for on_setMark_pressed to set mark } } //.. QFile f("template.xml");//Opening file with request template QTextStream templat(&f);//Set its content to text stream QHttpRequestHeader header("POST", "/maps/ms?ie=UTF8&hl=ru&vps=1&jsv=172d"); QString cookieStr; templat.setCodec("UTF-8"); f.open(QIODevice::ReadOnly); QString requestData(templat.readAll); f.close; if (hasAuth==1){//If auth identifier is new disconnect(setM,SIGNAL(done(bool)),0,0); hasAuth=0; QString mapid= mapUrl;//URL карты mapid=mapid.remove(0,mapid.indexOf("msid")+5);//Extracting mapid identifier from map URL mapid=mapid.remove(mapid.indexOf("&"),mapid.length-mapid.indexOf("&")+1); requestData.replace("MAPID",mapid);//Replace all field by user data requestData.replace("NAME",markName); requestData.replace("DESCRIPTION", markDescr);/ requestData.replace("COORDS",markCoords); requestData.replace("a53f7031:zOKS-bgigNIS7vNQoTjhiMseYmc",auth);//Replace old auth for new } else{ connect(setM,SIGNAL(done(bool)),this,SLOT(doneS(bool))); //Connect slot for auth identifier extracting file=new QFile; file->setFileName("tmp");//Creating file for server response file->open(QIODevice::WriteOnly); } header.setValue("Host", "maps.google.com"); setM->setHost("maps.google.com",QHttp::ConnectionModeHttp,80); for (QStringList::const_iterator i=cookies.begin;i!=cookies.end;i++) cookieStr+=*i+"; "; header.setValue("Cookie",cookieStr);//Set cookie recivied after authentication header.setContentType("application/x-www-form-urlencoded"); setM->request(header,requestData.toUtf8,file); } //…