hi again!
I just finished a rough draft of a GDirections class for GMap2. this is a standalone class, no modifications at the existing resources/classes are necessary.
it seems to work in my simple setting - this can also be updated with ajax.
the implementation has to override getJSadd() , since it is not constructed with a constructor. please have a look at the correctness, as this is my first custom component i've written for wicket.
open questions for me: is there a better way to access the core maps object?

       final String markupId = getParent().get("map").getMarkupId();
       String jsVar = "directions" + markupId;
sb.append("var ").append(jsVar).append(" = new GDirections(Wicket.maps['").append(markupId).append("'].map");

i have two concerns here:
1) accessing the map id via getParent().get("map").getMarkupId() - unfortunately the "map" variable in GMap2 is private without an accessor. otherwise I could have written getParent().getMap().getMarkupId(). 2) hardcoding the Wicket.maps[''].map javascript reference - is there a better way to obtain this from the parent?

possible features missing:
*) changing of markers via GEvent.addListener(jsVar,"load"...
*) somehow obtain the results (length) of routes. - any idea on this?
*) change the type of route (don't use highways, by foot, by public transport)

-----------------

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import wicket.contrib.gmap.api.GEvent;
import wicket.contrib.gmap.api.GLatLng;
import wicket.contrib.gmap.api.GOverlay;
import wicket.contrib.gmap.js.Array;

public class GDirections extends GOverlay {
   private Component writeOutPutTo;
   private final GLatLng[] waypoints;
   private final boolean writeOutput;

   public GDirections(GLatLng... gLatLngs) {
       this(null, gLatLngs);
   }

   public GDirections(Component writeOutPutTo, GLatLng... gLatLngs) {
       this.writeOutPutTo = writeOutPutTo;
       waypoints = gLatLngs;
       writeOutput = writeOutPutTo != null;
   }

   @Override
   protected String getJSconstructor() {
       return null;
   }

   @Override
   public String getJSadd() {
       Array array = new Array();
       for (GLatLng gLatLng : waypoints) {
           array.add(gLatLng.getJSconstructor());
       }
       final StringBuffer sb = new StringBuffer();
       final String markupId = getParent().get("map").getMarkupId();
       String jsVar = "directions" + markupId;
sb.append("var ").append(jsVar).append(" = new GDirections(Wicket.maps['").append(markupId).append("'].map");
       if (writeOutput) {
           sb.append(", document.getElementById('");
           sb.append(writeOutPutTo.getMarkupId());
           sb.append("')");
       }
       sb.append(");");
       sb.append(jsVar + ".loadFromWaypoints(");
       appendCoordArray(sb, waypoints);
       sb.append(");");
       sb.deleteCharAt(sb.length() - 1);
       return sb.toString();
   }

   /**
    * produces a string like
* ["37.600470,-122.384050","37.789010,-122.425420","38.029940,-122.255420","39.026450,-119.945700"]
    *
    * @param sb    StringBuffer to write into
    * @param array list of Waypoints
    */
   private void appendCoordArray(final StringBuffer sb, GLatLng[] array) {
       sb.append("[");
       for (GLatLng coord : array) {
sb.append("\"").append(coord.getLat()).append(",").append(coord.getLng()).append("\"");
           sb.append(",");
       }
       sb.deleteCharAt(sb.length() - 1);
       sb.append("]");
   }

   @Override
protected void updateOnAjaxCall(AjaxRequestTarget target, GEvent overlayEvent) {
       //no clue
   }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to