[OpenLayers-Users] problem with geolocation

Nicholas Efremov-Kendall n.e.kendall at gmail.com
Sat Mar 12 14:56:57 EST 2011


Hi Everyone,

I apologize in advance for what I assume is a pretty trivial error. I've
been trying to implement the geolocation example on my local system, but I
get an error "ol.control.geolocate is not a constructor" in Firebug, and
uncaught typeerror: undefined is not a function in chrome. I've tried this
both with linking the OL script to the live api url, as well as to a local
copy of OL 2.10. I repost the code below, but I just copied it from the live
example and got this error.
Thanks in advance!

var style = {
    fillColor: '#000',
    fillOpacity: 0.1,
    strokeWidth: 0
};

var map = new OpenLayers.Map('map');
var layer = new OpenLayers.Layer.OSM( "Simple OSM Map");
var vector = new OpenLayers.Layer.Vector('vector');
map.addLayers([layer, vector]);

map.setCenter(
    new OpenLayers.LonLat(-71.147, 42.472).transform(
        new OpenLayers.Projection("EPSG:4326"),
        map.getProjectionObject()
    ), 12
);

var pulsate = function(feature) {
    var point = feature.geometry.getCentroid(),
        bounds = feature.geometry.getBounds(),
        radius = Math.abs((bounds.right - bounds.left)/2),
        count = 0,
        grow = 'up';

    var resize = function(){
        if (count>16) {
            clearInterval(window.resizeInterval);
        }
        var interval = radius * 0.03;
        var ratio = interval/radius;
        switch(count) {
            case 4:
            case 12:
                grow = 'down'; break;
            case 8:
                grow = 'up'; break;
        }
        if (grow!=='up') {
            ratio = - Math.abs(ratio);
        }
        feature.geometry.resize(1+ratio, point);
        vector.drawFeature(feature);
        count++;
    };
    window.resizeInterval = window.setInterval(resize, 50, point, radius);
};

var geolocate = new OpenLayers.Control.Geolocate({
    geolocationOptions: {
        enableHighAccuracy: false,
        maximumAge: 0,
        timeout: 7000
    }
});
map.addControl(geolocate);
geolocate.events.register("locationupdated",this,function(e) {
    vector.removeAllFeatures();
    var circle = new OpenLayers.Feature.Vector(
        OpenLayers.Geometry.Polygon.createRegularPolygon(
            new OpenLayers.Geometry.Point(e.point.x, e.point.y),
            e.position.coords.accuracy/2,
            40,
            0
        ),
        {},
        style
    );
    vector.addFeatures([
        new OpenLayers.Feature.Vector(
            e.point,
            {},
            {
                graphicName: 'cross',
                strokeColor: '#f00',
                strokeWidth: 2,
                fillOpacity: 0,
                pointRadius: 10
            }
        ),
        circle
    ]);
    map.zoomToExtent(vector.getDataExtent());
    pulsate(circle);
});
geolocate.events.register("locationfailed",this,function() {
    OpenLayers.Console.log('Location detection failed');
});

$('locate').onclick = function() {
    vector.removeAllFeatures();
    geolocate.deactivate();
    $('track').checked = false;
    geolocate.watch = false;
    geolocate.activate();
};
$('track').onclick = function() {
    vector.removeAllFeatures();
    geolocate.deactivate();
    if (this.checked) {
        geolocate.watch = true;
        geolocate.activate();
    }
};
$('track').checked = false;
-- 

Dept. of Anthropology
Washington University in St. Louis
Campus Box 1114
St. Louis, MO, 63130
(917) 370-3489
nefremov at wustl.edu <nefremov at artsci.wustl.edu>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20110312/718275e6/attachment.html


More information about the Users mailing list