6

I use openlayers 2.12

I use HTML5's geolocation API to get the coords and then I insert them in a layer as feature (point). After that, I zoom in , since the layer has no other feature.

map.zoomToExtent(geoloc.getDataExtent());

geoloc is the name of the layer...

My problem is, how do I zoom-out automatically after the zoomToExtent?

Because the map zooms in too much and I can see just the point and very little details. It would be more user-friendly to zoom out a bit

Thanks

  • Do you want to zoom out one zoomlevel? Or to a particular ZoomLevel? – Devdatta Tengshe Oct 16 '13 at 2:36
4

If you want to ZoomTo a specific ZoomLevel , you can use Map.zoomTo like this:

map.zoomTo(level); \\where level is an integer, indicating the zoom level

If you just want to Zoom out one level, you can use Map.zoomOut like this:

map.zoomOut()
1

map.setCenter is useful, as it moves the map to the location as well as to a specified zoom level.

My code

function geolocate() {
    navigator.geolocation.getCurrentPosition(function (position) {
        var lonLat = new OpenLayers.LonLat(position.coords.longitude,
                position.coords.latitude)
            .transform(
                new OpenLayers.Projection("EPSG:4326"), //transform from WGS 1984
                map.getProjectionObject() //to Spherical Mercator Projection
            );
        markers.addMarker(new OpenLayers.Marker(lonLat)); //markers is a marker layer
        map.setCenter(lonLat, zoom); //lotlat is geolocation and zoom is the zoom level you want
        alert("your position is located");
    });
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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