Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Using Mark McClure's GPXToGoogleMap results in a HTML and Javascript code which can be viewed by any browser. Is it possible to load this map into the notebook, which obviously means for example that

<script src="http://maps.google.com/maps?file=api&amp;v=2.s&amp;key="
    type="text/javascript">
</script>
<script src="/Users/meinmacbookpro/Desktop/Messprojekte/gps.js" 
    type="text/javascript">
</script>

should be implemented somehow into the Mathematica code?

share|improve this question
    
Ironic: "GPXToGoogleMap produces HTML compliant with the XHTML version 1.0 standard." yet the documentation facstaff.unca.edu/mcmcclur/GoogleMaps/GPXToGoogleMap/… fails validator.w3.org –  alancalvitti Oct 4 '12 at 18:50
add comment

1 Answer 1

up vote 36 down vote accepted

All we need to create an interactive Google Map in the notebook is access to the individual tiles - and there is a relatively simple naming scheme for those tiles. I actually typed up a description of this naming scheme a few years ago and posted it here: http://facstaff.unca.edu/mcmcclur/GoogleMaps/Projections/GoogleCoords.html

The examples on that page show the full blown URLs used by the GMap API, but you don't need the full URL to access the tiles. For example, I live in the tile shown here: http://mt0.google.com/vt/x=277&y=403&z=10

The webpage above describes the basics of translating from lat/lng values to tile indices. We can implement this in Mathematica as follows.

{alng,blng} = First[{a,b} /. Solve[
  {a*(-180)+b==0,a(180)+b==1},{a,b}]];
lngToIndex[lng_, zoom_] := Floor[(alng*lng+blng)2^zoom];
mercator[lat_] = Log[Abs[Sec[lat*Degree]+Tan[lat*Degree]]];
{alat,blat} = First[{a,b} /. Solve[{a*mercator[85.0511287798066]+b==0,
  a*mercator[-85.0511287798066]+b==1},{a,b}]];
latToIndex[lat_, zoom_] := Floor[(alat*mercator[lat]+blat)2^zoom];
indicesToTileURL[x_Integer,y_Integer, zoom_Integer] := 
    "http://mt0.google.com/vt/x="<>ToString[x]<>"&y="<>
      ToString[y]<>"&z="<>ToString[zoom];

Now, suppose I'd like to compute the URL of a tile in my neighborhood.

{lat0,lng0} = CityData["Asheville", "Coordinates"];
x0=lngToIndex[lng0,10];
y0=latToIndex[lat0,10];
indicesToTileURL[x0,y0,10]

"http://mt0.google.com/vt/x=277&y=403&z=10"

We can put this all together to set up an interactive zoomer.

Manipulate[
  With[{x0=lngToIndex[lng0,zoom],y0=latToIndex[lat0,zoom]},
    Grid[{
      {Import[indicesToTileURL[x0-1,y0-1,zoom]],
       Import[indicesToTileURL[x0,y0-1,zoom]],
       Import[indicesToTileURL[x0+1,y0-1,zoom]]},
       {Import[indicesToTileURL[x0-1,y0,zoom]],
       Import[indicesToTileURL[x0,y0,zoom]],
       Import[indicesToTileURL[x0+1,y0,zoom]]},
      {Import[indicesToTileURL[x0-1,y0+1,zoom]],
       Import[indicesToTileURL[x0,y0+1,zoom]],
       Import[indicesToTileURL[x0+1,y0+1,zoom]]}
    }, Spacings -> {0,0}]
  ],{{zoom,11},Range[5,15]}]

enter image description here

This is really just proof of concept at this point. There's quite a lot more that could be done. You could use an EventHandler to allow dragging and panning and add other controls as well. You'd need some error handling to deal with scrolling out of range. You could also interface other map servers. WolframAlpha evidently interfaces Open Street Map: http://www.wolframalpha.com/input/?i=map+of+asheville

Also, I checked the terms of use of the Google Maps API available here: http://www.google.com/intl/en_us/help/terms_maps.html

I don't see anything that violates their terms of use but, then, I'm not a lawyer.

Looking a little closer at your question, I see that you are wanting specifically to include map generated by the GPXToGoogleMap package into a Mathematica notebook. The first step would be to grab the map settings, which could be done like so:

mapsrc = Import[
  "http://facstaff.unca.edu/mcmcclur/GoogleMaps/index.js",
  "Text"
];
info =StringSplit[mapsrc, 
  {"map.setCenter(new GLatLng(", "G_NORMAL_MAP"}][[2]];
info = StringReplace[info, ")" -> ""];
{lat0,lng0,initialZoom} = ToExpression[StringSplit[info, ", "]]

Of course, that applies quite specifically to maps generated by this package. Other maps are likely different. Furthermore, the main purpose of that package is to display paths, markers, and other data contained in a GPX file. Doable, but a fair amount of work.

share|improve this answer
    
wow, thanks a lot. I will deeply go into your answer tomorrow on Sunday. Thanks a lot, Harald –  Harald Mar 24 '12 at 17:25
    
@Harald No problem - it's great fun! I'd be curious to hear more about the specific application you have in mind. –  Mark McClure Mar 24 '12 at 17:36
    
The application calculates points for some grids (usually 40 m by 40 m) for geophysical mappings of archaeological sites, as an input for the Leica GPS900. And the combination with a (for example) Google Map should be good for the documentation of the field campaign. –  Harald Mar 25 '12 at 7:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.