[GIS] add a feature in openlayers using lonlat from geolocation

geolocationopenlayers-2

I'm trying to add a feature in a openlayer's vector layer using the lonlat that came from the HTML5's geolocation API.

Combining tutorials for the geolocation and this demo I created the following code

<input type="button" value="Where Am I" onClick="loadDemo();" />


//this is outside the init() openlayers function. If i put it inside init(), will not work at all
function loadDemo() {
if(navigator.geolocation) {
document.getElementById("support").innerHTML = "I'll find you!";
navigator.geolocation.getCurrentPosition(updateLocation, handleLocationError, {timeout:100000});
} else {
document.getElementById("support").innerHTML = "Browser is so old";
}}

function updateLocation(position) {
var latitudeg = position.coords.latitude;
var longitudeg = position.coords.longitude;
var accuracy = position.coords.accuracy;
document.getElementById("latitude").innerHTML = latitudeg;//works
document.getElementById("longitude").innerHTML = longitudeg;//works
document.getElementById("accuracy").innerHTML = "This location is accurate within " +accuracy + " meters."//works
var lonLat = new OpenLayers.LonLat(longitudeg,latitudeg).transform(new OpenLayers.Projection("EPSG:4326"),new OpenLayers.Projection("EPSG:900913"));//works
alert(lonLat);//works
var pointg = new OpenLayers.Geometry.Point(lonLat);
var feature_point_g = new OpenLayers.Feature.Vector(pointg);
alert(pointg.lat);//not working, returns undifined
alert(feature_point_g);//not working, returns [object Object]
pins.addFeatures([feature_point_g]);//not working
pins.refresh({force:true});//not working
}

As you can see, problem is , there is something wrong with creating the point and the feature. Honestly, I searched and checked and can't solve this. Can you see something I don't?

Thanks

Best Answer

You are passing an incorrect argument to the OpenLayers.Geometry.Point constructor. You don't need the lonLat. Try the following:

var pointg = new OpenLayers.Geometry.Point(longitudeg, latitudeg);
pointg.transform(
    new OpenLayers.Projection("EPSG:4326"),
    new OpenLayers.Projection("EPSG:900913")
);