Example usage for com.google.gwt.geolocation.client Geolocation getIfSupported

List of usage examples for com.google.gwt.geolocation.client Geolocation getIfSupported

Introduction

In this page you can find the example usage for com.google.gwt.geolocation.client Geolocation getIfSupported.

Prototype

public static Geolocation getIfSupported() 

Source Link

Document

Returns a Geolocation if the browser supports this feature, and null otherwise.

Usage

From source file:$.ZoomToLocationController.java

License:Open Source License

public void onMouseUp(MouseUpEvent event) {
        //      StyleElement se = (StyleElement) Document.get().getElementById("inlineStyle");
        //      se.setInnerText(".msRibbon { background: green; }");

        final Geolocation geo = Geolocation.getIfSupported();
        if (geo != null) {
            geo.getCurrentPosition(new Callback<Position, PositionError>() {

                @Override//from   ww  w  .j  a  va2 s  .  c o  m
                public void onSuccess(final Position result) {
                    Coordinates coord = result.getCoordinates();
                    TransformGeometryRequest req = new TransformGeometryRequest();
                    GeometryFactory gf = new GeometryFactory(4326, 1);
                    Point point = gf.createPoint(new Coordinate(coord.getLongitude(), coord.getLatitude()));

                    req.setGeometry(GeometryConverter.toDto(point));
                    req.setSourceCrs("EPSG:4326");
                    req.setTargetCrs(mapWidget.getMapModel().getCrs());

                    GwtCommand command = new GwtCommand(TransformGeometryRequest.COMMAND);
                    command.setCommandRequest(req);

                    GwtCommandDispatcher.getInstance().execute(command, new CommandCallback<CommandResponse>() {

                        @Override
                        public void execute(CommandResponse response) {
                            if (response.getErrors().isEmpty()) {
                                org.geomajas.geometry.Geometry geom = ((TransformGeometryResponse) response)
                                        .getGeometry();
                                double accuracy = result.getCoordinates().getAccuracy();

                                Bbox box = new Bbox(geom.getCoordinates()[0].getX() - (accuracy / 2),
                                        geom.getCoordinates()[0].getY() - (accuracy / 2), accuracy, accuracy);
                                mapWidget.getMapModel().getMapView().applyBounds(box, ZoomOption.LEVEL_FIT);
                            }
                        }
                    });

                }

                @Override
                public void onFailure(PositionError reason) {
                    // TODO Auto-generated method stub

                }
            });
        }
        event.stopPropagation();
    }

From source file:com.google.maps.gwt.samples.basics.client.MapGeolocation.java

License:Apache License

@Override
public void onModuleLoad() {
    final MapOptions myOptions = MapOptions.create();
    myOptions.setZoom(14.0);//from w  ww .j ava2  s .  c o m
    myOptions.setMapTypeId(MapTypeId.ROADMAP);

    map = GoogleMap.create(Document.get().getElementById("map_canvas"), myOptions);

    // Try W3C Geolocation (Preferred)
    if (Geolocation.isSupported()) {
        browserSupportFlag = true;
        Geolocation.getIfSupported().getCurrentPosition(new Callback<Position, PositionError>() {

            @Override
            public void onSuccess(Position result) {
                Coordinates coords = result.getCoordinates();
                initialLocation = LatLng.create(coords.getLatitude(), coords.getLongitude());
                map.setCenter(initialLocation);
            }

            @Override
            public void onFailure(PositionError reason) {
                MapGeolocation.this.handleNoGeolocation(browserSupportFlag);
            }
        });
    } else {
        browserSupportFlag = false;
        handleNoGeolocation(browserSupportFlag);
    }
}

From source file:it.bz.tis.sasabus.html5.shared.ui.map.SASAbusMapAttachHandler.java

License:Open Source License

@Override
public void onAttachOrDetach(AttachEvent event) {
    if (event.isAttached()) {
        this.map.leafletMap = new Map(this.map.mapDiv.getElement());
        this.map.leafletMap.addLayer(new OSMLayer());

        this.map.fitAllAreas();

        this.map.refreshBaseLayerAfterZoomLevelAndPosition();

        this.map.leafletMap.addZoomEndEventListener(new EventListener() {
            @Override//from  www. j av  a  2s  .c  om
            public void onEvent() {
                SASAbusMapAttachHandler.this.map.refreshBaseLayerAfterZoomLevelAndPosition();
            }
        });
        this.map.leafletMap.addDragEndEventListener(new EventListener() {
            @Override
            public void onEvent() {
                SASAbusMapAttachHandler.this.map.refreshBaseLayerAfterZoomLevelAndPosition();
            }
        });

        this.map.gpsIcon.addClickHandler(new DMClickHandler() {

            @Override
            public void onClick(DMClickEvent event) {
                PositionOptions positionOptions = new PositionOptions();
                positionOptions.setHighAccuracyEnabled(true);
                positionOptions.setTimeout(10000);

                // http://openlayers.org/dev/examples/geolocation.html

                Geolocation geoloc = Geolocation.getIfSupported();
                if (geoloc != null) {
                    geoloc.getCurrentPosition(new Callback<Position, PositionError>() {
                        @Override
                        public void onSuccess(Position result) {

                            double lon = result.getCoordinates().getLongitude();
                            double lat = result.getCoordinates().getLatitude();
                            double accuracy = result.getCoordinates().getAccuracy();
                            Window.alert("lat: " + lat + " lon: " + lon + " acc (meter): " + accuracy);

                            LatLng position = new LatLng(lat, lon);
                            SASAbusMapAttachHandler.this.map.leafletMap.setView(position, 16);
                        }

                        @Override
                        public void onFailure(PositionError reason) {
                            Window.alert("Failure: " + reason.getMessage());
                        }
                    }, positionOptions);
                } else {
                    Window.alert("Your browser does not support localization");
                }
            }
        });

    }
}

From source file:org.geomajas.quickstart.gwt.client.ZoomToLocationController.java

License:Open Source License

public void onMouseUp(MouseUpEvent event) {
    //      StyleElement se = (StyleElement) Document.get().getElementById("inlineStyle");
    //      se.setInnerText(".msRibbon { background: green; }");

    final Geolocation geo = Geolocation.getIfSupported();
    if (geo != null) {
        geo.getCurrentPosition(new Callback<Position, PositionError>() {

            @Override// w  ww.  ja v a 2s.c o m
            public void onSuccess(final Position result) {
                Coordinates coord = result.getCoordinates();
                TransformGeometryRequest req = new TransformGeometryRequest();
                GeometryFactory gf = new GeometryFactory(4326, 1);
                Point point = gf.createPoint(new Coordinate(coord.getLongitude(), coord.getLatitude()));

                req.setGeometry(GeometryConverter.toDto(point));
                req.setSourceCrs("EPSG:4326");
                req.setTargetCrs(mapWidget.getMapModel().getCrs());

                GwtCommand command = new GwtCommand(TransformGeometryRequest.COMMAND);
                command.setCommandRequest(req);

                GwtCommandDispatcher.getInstance().execute(command, new CommandCallback<CommandResponse>() {

                    @Override
                    public void execute(CommandResponse response) {
                        if (response.getErrors().isEmpty()) {
                            org.geomajas.geometry.Geometry geom = ((TransformGeometryResponse) response)
                                    .getGeometry();
                            double accuracy = result.getCoordinates().getAccuracy();

                            Bbox box = new Bbox(geom.getCoordinates()[0].getX() - (accuracy / 2),
                                    geom.getCoordinates()[0].getY() - (accuracy / 2), accuracy, accuracy);
                            mapWidget.getMapModel().getMapView().applyBounds(box, ZoomOption.LEVEL_FIT);
                        }
                    }
                });

            }

            @Override
            public void onFailure(PositionError reason) {
                // TODO Auto-generated method stub

            }
        });
    }
    event.stopPropagation();
}

From source file:org.geomajas.quickstart.mobile.client.activity.MobileMapActivity.java

License:Open Source License

private void getGeoLocation() {

    final Geolocation geo = Geolocation.getIfSupported();
    if (geo != null) {
        geo.getCurrentPosition(new Callback<Position, PositionError>() {

            @Override//from w ww .ja v a  2  s . co  m
            public void onSuccess(final Position result) {
                Position.Coordinates coord = result.getCoordinates();
                TransformGeometryRequest req = new TransformGeometryRequest();
                Geometry point = new Geometry(Geometry.POINT, 4326, -1);
                point.setCoordinates(
                        new Coordinate[] { new Coordinate(coord.getLongitude(), coord.getLatitude()) });
                GWT.log("Browser geolocation coords "
                        + new Coordinate(coord.getLongitude(), coord.getLatitude()));
                req.setGeometry(point);

                req.setSourceCrs("EPSG:4326");

                req.setTargetCrs(mapView.getMap().getMapPresenter().getViewPort().getCrs());

                GwtCommand command = new GwtCommand(TransformGeometryRequest.COMMAND);
                command.setCommandRequest(req);

                GwtCommandDispatcher.getInstance().execute(command, new CommandCallback<CommandResponse>() {

                    @Override
                    public void execute(CommandResponse response) {
                        if (response.getErrors().isEmpty()) {
                            org.geomajas.geometry.Geometry geom = ((TransformGeometryResponse) response)
                                    .getGeometry();

                            GWT.log("Geomajas after transform coords " + geom.getCoordinates()[0]);
                            /*
                                                    double accuracy = result.getCoordinates().getAccuracy();
                                                    Bbox box = new Bbox(geom.getCoordinates()[0].getX() - (accuracy / 2), geom
                                                          .getCoordinates()[0].getY() - (accuracy / 2), accuracy, accuracy);
                                                    double res = mapView.getMap().getMapPresenter().getViewPort().getResolution();
                                    
                                                    double crr = mapView.getMap().getMapPresenter().getViewPort().getResolution(co + 3);
                                                    mapView.getMap().getMapPresenter().getViewPort().
                                                    applyView(new View(geom.getCoordinates()[0], crr));
                            */

                            //Coordinate c = geom.getCoordinates()[0];

                            /*   Coordinate c1 = new Coordinate(c.getX(), c.getY());
                               Coordinate c2 = new Coordinate(c.getX() + buffer, c.getY() + buffer);
                               Geometry geometry = new Geometry(Geometry.LINE_STRING, 0, 0);
                               geometry.setCoordinates(new Coordinate[] { c1, c2 });*/

                            //int co = mapView.getMap().getMapPresenter().getViewPort().getResolutionIndex(19);

                            /*   double minRes = mapView.getMap().getMapPresenter().getViewPort().getResolution(5);
                                    
                               mapView.getMap().getMapPresenter().getViewPort().
                               applyView(new View(geom.getCoordinates()[0],
                                     minRes));
                            */

                            Bbox bbox = GeometryService.getBounds(geom);
                            mapView.getMap().getMapPresenter().getViewPort().applyBounds(bbox);
                            /*   double resApply = mapView.getMap().getMapPresenter().getViewPort().getResolution(19);
                               mapView.getMap().getMapPresenter().getViewPort().applyResolution(resApply);*/

                            /*   mapView.getMap().getMapPresenter().getViewPort().
                            applyView(new View(geom.getCoordinates()[0],
                                     resApply));*/

                        }
                    }
                });

            }

            @Override
            public void onFailure(PositionError reason) {

            }
        });
    }
}

From source file:org.yocto.sample.client.WorldMap.java

License:Open Source License

public synchronized static void getLatLng(final AsyncCallback<LatLng> callback) {

    if (latLng == null) {
        Geolocation location = Geolocation.getIfSupported();
        location.getCurrentPosition(new Callback<Position, PositionError>() {
            public void onFailure(PositionError reason) {
                callback.onFailure(reason);
            }/*from   w ww.j  a v  a 2  s  .co m*/

            public void onSuccess(Position result) {
                logger.info("Location found at: " + result);
                latLng = LatLng.newInstance(result.getCoordinates().getLatitude(),
                        result.getCoordinates().getLongitude());
                callback.onSuccess(latLng);
            }
        });
    } else {
        callback.onSuccess(latLng);
    }
}

From source file:pl.itrack.client.local.services.maps.ClassicMapService.java

License:Apache License

public void setCurrentLocationIfSupported() {
    Geolocation.getIfSupported().getCurrentPosition(new Callback<Position, PositionError>() {

        @Override/*from  ww  w. j  av a2  s .co  m*/
        public void onSuccess(Position result) {
            Position.Coordinates coordinates = result.getCoordinates();
            LatLng center = LatLng.newInstance(coordinates.getLatitude(), coordinates.getLongitude());
            getMapWidget().setCenter(center);
        }

        @Override
        public void onFailure(PositionError reason) {
            Window.alert(Texts.MSG_LOCATION_NOT_SUPPORTED);
        }
    });
}