Little Modification of GoogleMap Mod for AutoZoom

Post custom hacks and enhancements for phpwcms here only. Maybe some of these things will be included in official release later.
breitsch
Posts: 473
Joined: Sun 6. Mar 2005, 23:12
Location: bern switzerland

Re: Little Modification of GoogleMap Mod for AutoZoom

Post by breitsch »

Ok for those of you who wants to provide the routeplanner-service on their own sites and not send the user to the google maps site in a new window/tab, there is a way to do so (but don't tell anyone!!!)

Open a HTML contentpart in your article.
Copy the code below and change the 'HereYourKey' in the first line with your actual Google Maps API Key
enter your desired start and end locations here: setDirections("San Francisco", "Mountain View", "en_US"); (for germans it'd be de_DE)
a bit further down the start and end points appear again within the input fields, you should change it there as well: value="San Francisco"/> and value="Mountain View" />
Save the whole thing and enjoy it!
The rest is only modifications in the html part of the code and translation if necessary.

Code: Select all

<script src="http://www.google.com/jsapi?key=HereYourKey"
      type="text/javascript"></script>
<script type="text/javascript">
  google.load("maps", "2.x");
</script>
    <style type="text/css">
      body {
        font-family: Verdana, Arial, sans serif;
        font-size: 11px;
        margin: 2px;
      }
      table.directions th {
	background-color:#EEEEEE;
      }
	  
      img {
        color: #000000;
      }
    </style>
 <script type="text/javascript">
 
    var map;
    var gdir;
    var geocoder = null;
    var addressMarker;

    function initialize() {
      if (GBrowserIsCompatible()) {      
        map = new GMap2(document.getElementById("map_canvas"));
        gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);

        setDirections("San Francisco", "Mountain View", "en_US");
      }
    }
    
    function setDirections(fromAddress, toAddress, locale) {
      gdir.load("from: " + fromAddress + " to: " + toAddress,
                { "locale": locale });
    }

    function handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	   
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

	//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
	//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
	     
	   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	    
	   else alert("An unknown error occurred.");
	   
	}

	function onGDirectionsLoad(){ 
      // Use this function to access information about the latest load()
      // results.

      // e.g.
      // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
	  // and yada yada yada...
	}
	google.setOnLoadCallback(initialize);
    </script>
  
  <h2>Maps API Directions Illustrated</h2>
  <form action="#" onsubmit="setDirections(this.from.value, this.to.value, this.locale.value); return false">

  <table>

   <tr><th align="right">From: </th>

  <td><input type="text" size="25" id="fromAddress" name="from"
     value="San Francisco"/></td>
   <th align="right">&nbsp;&nbsp;To:&nbsp;</th>
   <td align="right"><input type="text" size="25" id="toAddress" name="to"
     value="Mountain View" /></td></tr>

   <tr><th>Language: </th>
   <td colspan="3"><select id="locale" name="locale">

    <option value="en" selected>English</option>

    <option value="fr">French</option>

    <option value="de">German</option>
    <option value="ja">Japanese</option>
    <option value="es">Spanish</option>
    </select>

    <input name="submit" type="submit" value="Get Directions!" />

   </td></tr>
   </table>

    
  </form>

    <br/>
    <table class="directions">
    <tr><th>Formatted Directions</th><th>Map</th></tr>

    <tr>
    <td valign="top"><div id="directions" style="width: 275px"></div></td>
    <td valign="top"><div id="map_canvas" style="width: 310px; height: 400px"></div></td>

    </tr>
    </table>
Cool, not?
Working example here: http://www.napfwalking.ch/index.php?anfahrt
(start and end point is set to same location and first input is empty)
You can link to this article from your original map in the module, but you must do it with an ID replacement tag in the info window, don't use the routeplanner-link in the contentpart, this one will still send you to Google Maps.
http://www.youtube.com/watch?v=jqxENMKaeCU
because it's important!
breitsch
User avatar
update
Moderator
Posts: 6455
Joined: Mon 10. Jan 2005, 17:29
Location: germany / outdoor

Re: Little Modification of GoogleMap Mod for AutoZoom

Post by update »

Cool, not?
8) :!:
I just couldn't resist :D
Cool, yes!
It's mostly all about maintaining two or three customer's sites Still supporter for the band Mykket Morton. Visit Mykket Morton on FB. Listen Mykket Morton and live videos on youtube.
Now building a venue for young artists to get wet on stage, rehearsal rooms, a studio, a guitar shop - yes I'm going to build some guitars.
breitsch
Posts: 473
Joined: Sun 6. Mar 2005, 23:12
Location: bern switzerland

Re: Little Modification of GoogleMap Mod for AutoZoom

Post by breitsch »

The code above even a bit better:

change the embedded styles to:

Code: Select all

   <style type="text/css">
      body {
        font-family: Verdana, Arial, sans serif;
        font-size: 11px;
        margin: 2px;
      }
     table.directions th {
        background-color:#EEEEEE;
      }
     table.directions div div div div div table tr {
	background-color:#FFFFFF;
      }
     table.directions div div div div div table tr:hover {
	background-color:#cdcdcd;
      }
     img {
        color: #000000;
      }
    </style>
and limit the height of the directions div to the same height as the map and give it an overflow:auto, e.g :

Code: Select all

     <td valign="top"><div id="directions" style="width: 345px;height:400px;overflow:auto;"></div></td>
      <td valign="top"><div id="map_canvas" style="width: 325px; height: 400px;margin-top:12px;"></div></td>
you get scrollbars then and the rows with mouseover are highlighted.
Of course you must enable the hover effect for IE in the config.php: $phpwcms['IE_htc_hover'] = 1; // enables HTC Hover for IE < 7 - has no effect in other browsers
See again: http://www.napfwalking.ch/index.php?anfahrt
http://www.youtube.com/watch?v=jqxENMKaeCU
because it's important!
breitsch
User avatar
update
Moderator
Posts: 6455
Joined: Mon 10. Jan 2005, 17:29
Location: germany / outdoor

Re: Little Modification of GoogleMap Mod for AutoZoom

Post by update »

You can link to this article from your original map in the module, but you must do it with an ID replacement tag in the info window
Very nice! Is there a way to get the coordinates passed over to the internal routing map (like this is happening with the external google routing map?
(I'm a bit late with testing at the moment but I'll catch up immediately!) ;)
It's mostly all about maintaining two or three customer's sites Still supporter for the band Mykket Morton. Visit Mykket Morton on FB. Listen Mykket Morton and live videos on youtube.
Now building a venue for young artists to get wet on stage, rehearsal rooms, a studio, a guitar shop - yes I'm going to build some guitars.
User avatar
Jensensen
Posts: 3000
Joined: Tue 17. Oct 2006, 21:11
Location: auf der mlauer

Re: Little Modification of GoogleMap Mod for AutoZoom

Post by Jensensen »

breitsch wrote:...See again: http://www.napfwalking.ch/index.php?anfahrt
sehr very, very well done, pretty cool, perfect :D
Thanks a lot...
Greetz
{so_much} | Knick-Knack. | GitHub
Umlaute im URL sind meistens immer Kacke.
selbaer
Posts: 93
Joined: Sun 22. Jan 2006, 02:19
Location: Florida West Coast

Re: Little Modification of GoogleMap Mod for AutoZoom

Post by selbaer »

breitsch:

you are scaring me. this is soo incredible, really impressing

on he other side I didn't get it to work right...yet
Post Reply