Google Maps and DCOP

Ideas on how to use KDE DCOP interprocess communication with web pages built with Google Maps API or even with Google Maps Web Site.

Introduction

First of all you need to be familiar with the following geeky subjects:

  • DCOP: Desktop COmmunication Protocol. This is KDE's interprocess communication (IPC)/remote procedure call (RPC) technology. It allows a given application to use another KDE's running application functions.
  • Google Maps Web Site (GMWS): this is Google's web site providing online map service
  • Google Maps API (GMAPI): this is a JavaScript API that lets you embed GMWS-like service in your own web pages.
  • Konqueror: KDE's web browser (and much more) 

 

At the moment, Google are not using their public API (GMAPI) to  build their own Google Maps service (GMWS). See here for some info. This is why this howto is divided into two parts: the first one focuses on how to use DCOP with GMWS (this is rather limited though), whereas the other shows how you can use/control a GMAPI mashup to build your own desktop applications.


DCOP is very useful when used used with scripts: there is a command line tool dcop that is able to communicate with all DCOP enabled applications and to return he result on the standard output.

If you want to explore what DCOP interfaces are available on the runnning applications, you can launch the graphical DCOP browser: kdcop.

 

For example if you launch konqueror web browser (with the pid 1234), you will notice a "konqueror-1234" tree in kdcop. You can see that is has the "html-widget2" interface with a list of available functions. One of them is very interesting: "evalJS"; it allows you to execute javascript calls on the current page.


To test the DCOP communication, try this command:

$ dcop konqueror-1234 html-widget2 evalJS "alert('hello world');"

There should be a message box poping into konqueror, just as if you had included the code "alert('hello world');" in the page source. This shows that you can access/trigger a javascript event on any web page displayed in your konqueror. Isn't that cool?


In order to use dcop you have to know the pid of the konqueror session you want to control. A simple way to do this is to include this code in a shell script:

nohup /usr/bin/konqueror http://maps.google.com &
echo $!

The "$!" variable contains the pid of the last launched background job. The "nohup" command is not mandatory, it just allows the konqueror session to remain open after the shell script termination.


Now that we know how to launch konqueror with a specific web page, and how to send it javascript calls, let's see how we can play with GMWS and GMAPI. All you need to try it is: KDE, dcop, kdcop, konqueror, your own web site running GMAPI. KDE runs on Linux, but there is also a Windows port (based on Cygwin I think): the Windows version has not been tested, but as long as dcop and konqueror work, it should be exactly the same.



DCOP and GMWS

As stated above, GMWS does not use GMAPI. So you cannot just read the API documentation and try to execute javascript calls. You have to search for info on how GMWS site works, and to figure how to use it by yourself.

 This discussion shows some methods of the GMapsApplication class.

GMapsApplication.clear()
GMapsApplication.getMap()
GMapsApplication.getMarker()
GMapsApplication.getPageUrl()
GMapsApplication.getPolyline()
GMapsApplication.getTabUrl()
GMapsApplication.infoWindowProcess()
GMapsApplication.loadVPage()
GMapsApplication.openInfoWindow()
GMapsApplication.showPolylineBlowup()


GMapsApplication is used on GMWS. If you read the source of GMWS, you can find this:

gApplication = new GMapsApplication(e("map"),e("panel"),{pwm:"2"});

So you can assume that gApplication.getPageUrl() should return the url of the location you are looking at. Let's try it:

$ dcop konqueror-1234 html-widget2 evalJS "gApplication.getPageUrl();"
http://maps.google.com/?ie=UTF8&ll=21.276018,-157.827229&spn=0.011677,0.022659&t=h&z=16&om=1


Bingo! You can then parse the url to get latitude, longitude for example with:

$ dcop konqueror-1234 html-widget2 evalJS "gApplication.getPageUrl();" | sed -e "s#.*&ll=\([^&]*\).*#\1#"
21.276018,-157.827229


This is just a basic example. You might want to dive into GMWS source (good luck!) to find out more useful calls.  If you do so, please let me know, I can add/link them here.



DCOP and GMAPI

A more powerful way of using javascript with Google Maps is to build your own pages with GMAPI. You have to sign up for a key (as a matter of fact, I noticed that if you host your page on Google Page Creator you don't need this key).

For example see here: gmaps.html

You can see a very basic GMAPI page that shows the map center coordinates just under the map, into the "<div id="message"></div>" element.

So all you have to do is just to read through a javascript call the content of the div element:

$ dcop konqueror-1234 html-widget2 evalJS "document.getElementById('message').innerHTML"
(21.276018, -157.827229)


Now you just have to read the full GMAPI documentation, and to build your own great pages that will communicate with desktop applications ;)



Example: kommander application for photos geotagging

I use this DCOP/GMAPI communication to geotag my pictures. This example section shows how I do it. To understand it you have to know what kommander is : kommander is a sort "graphical shell scripting" environment that enables you to easily build small applications.


In order to add geotagging info to my pictures, I built a kommander application that follows these steps:

  • The first thing is to launch a konqueror session and to store the pid in a string. The following code is contained in the "population" section of a Label named "pidkonq". You can trigger this population with an ExecButton: this will store the pid in the Label
    @execBegin#!/bin/bash
    nohup /usr/bin/konqueror http://92.calvin.googlepages.com/gmaps.html &
    echo $!
    kdialog --title "Konqueror" --passivepopup "You can now GPS puis les importer" 3
    @execEnd
  • Then you select the right coordinates using the GMAPI web page
  • After that you want to import the coordinates in the kommander application. You just have to create a new Label with the following "population" code (it will also be trigerred by an ExecButton)
    @execBegin#!/bin/bash
    Coords=`dcop konqueror-@pidkonq.text html-widget2 evalJS "document.getElementById('message').innerHTML"`
    Lat=`echo $Coords| sed 's/(\(-*[0-9.]*\), \(-*[0-9.]*\))/\1/'` #-48.756747537903
    Lon=`echo $Coords| sed 's/(\(-*[0-9.]*\), \(-*[0-9.]*\))/\2/'`
    LatRef=`echo $Lat | sed 's/\(-*\)[0-9]*.[0-9]*/\1/'`
    if [[ "$LatRef" == "-" ]]; then
        LatRef="S"
        Lat=`echo $Lat | tr -d "-"`
    else
        LatRef="N"
    fi
    LonRef=`echo $Lon | sed 's/\(-*\)[0-9]*.[0-9]*/\1/'`
    if [[ "$LonRef" == "-" ]]; then
        LonRef="W"
        Lon=`echo $Lon | tr -d "-"`
    else
        LonRef="E"
    fi
    echo "$LatRef:$Lat|$LonRef:$Lon"
    @execEnd
  • You can now call an external application (such as Exiftool) to add EXIF GPS data to a selected image