Thursday, February 21, 2013

Marker GWT Maps V3 Single and Double ClickHandler

        GWT Maps V2 supports both single click and double click handler for a single marker in the same window. Whereas, in V3, both methods are available but only single click handler triggers if used for the same marker. In my scenario, I need single click handler for showing InfoWindow and double click handler to navigate to other page(another tab) within application. Normally if u want to implement both in maps V3, single click handler needs a delay or need some asynchronous calls. Here, I have used Geocoder in single click for getting address of particular marker(getting latlng) and showing in InfoWindow.

Sample Code for using both Handlers:

 VerticalPanel panel = new VerticalPanel();
  panel.setSize("1200", "800");
  LatLng myLatLng = LatLng.create(13.029603, 80.271800);
  MapOptions myOptions = MapOptions.create();
  GoogleMap mapWidget = GoogleMap.create(panel.getElement(), myOptions);
  MarkerOptions newMarkerOpts = MarkerOptions.create();
  newMarkerOpts.setTitle("Hello World!");
  final Marker marker = Marker.create(newMarkerOpts);
 marker.addClickListener(new ClickHandler() {
   public void handle(MouseEvent event) {
    Geocoder geocoder = Geocoder.create();
    GeocoderRequest request = GeocoderRequest.create();
    geocoder.geocode(request, new Callback() {
     public void handle(JsArray<GeocoderResult> a,
       GeocoderStatus b) {
      if (b == GeocoderStatus.OK) {
       if (a.length() > 0) {
        GeocoderResult geocoderResult = a.get(0);
        StringBuffer sb = new StringBuffer();
        JsArray<GeocoderAddressComponent> addressComponents = geocoderResult
        for (int i = 0; i < addressComponents.length(); i++) {
         if (i > 0) {
          sb.append(", ");
        String location = sb.toString();
        InfoWindowOptions infoWindowOptions = InfoWindowOptions.create();
        InfoWindow infoWindow = InfoWindow.create(infoWindowOptions);, marker);
  marker.addDblClickListener(new DblClickHandler() {
   public void handle(MouseEvent event) {
    Window.alert("Double Click Demo");