I made it! Found something useful here:
http://stackoverflow.com/questions/4002981/openlayers-how-do-i-draw-a-polygon-from-existing-lonlat-points
Thanks for the help. Here's my Solution:
var pointsB = []; //for Geometry.Point Objects
var lonlatsB = []; //for LonLat Objects
// Define polygon area, using LonLat-Objects to create Polygon
var lonlatObject1 = new OpenLayers.LonLat(0.5751265,-0.03368972);
var lonlatObject2 = new OpenLayers.LonLat(0.5751265,-0.032040477);
var lonlatObject3 = new OpenLayers.LonLat(0.57347727,-0.032040477);
var lonlatObject4 = new OpenLayers.LonLat(0.57347727,-0.03368972);
//add LonLats to Array
lonlatsB.push(lonlatObject1);
lonlatsB.push(lonlatObject2);
lonlatsB.push(lonlatObject3);
lonlatsB.push(lonlatObject4);
//projecting LonLats to Points
for (var i=0; i<lonlatsB.length; i++) {
point = new OpenLayers.Geometry.Point(lonlatsB[i].lon, lonlatsB[i].lat);
point.transform(
new OpenLayers.Projection("EPSG:4326"), //from
map.getProjectionObject() //to
);
pointsB.push(point);
}
pointsB.push(pointsB[0]);
// create a polygon feature from a list of points
var linearRingB = new OpenLayers.Geometry.LinearRing(pointsB);
var polygonFeatureB = new OpenLayers.Feature.Vector(linearRingB, null,
style_green);
vectorLayer.addFeatures([polygonFeatureB]);
From: Xurxo Mendez
Sent: Tuesday, January 25, 2011 12:17 PM
To: [email protected] ; OpenLayers
Subject: Re: [OpenLayers-Users] How to deal with Geometry.Point and lonlat
Hi,
You should already know the current projection of the vertices, and you maybe
have to reproject them to the map's projection using
OpenLayers.Projection.transform() if they are not in the same projection
Best regards,
Xurxo Méndez Pérez
http://www.sonxurxo.com
2011/1/25 <[email protected]>
Hello,
Ok, the code works now. But unfortunately it is not quite exactly what I
wanted to have.
Because now the Geometry.Point refers to another place in the map than the
original LonLat-Object, since
it doesn't get projected but just "copied".
Yes and this the right vocabulary I was searching for: "to project"
How can I project a LonLat-Object to a Geometry-Point, so that both refer to
the same place in the map?
From: Xurxo Mendez
Sent: Tuesday, January 25, 2011 11:19 AM
To: [email protected]
Subject: Re: [OpenLayers-Users] How to deal with Geometry.Point and lonlat
Hi,
Sorry, my bad. (I didn't actually test the code).
OpenLayers.LonLat objects hold two properties, 'lon' and 'lat', so you should
try
var lonlatObject1 = new OpenLayers.LonLat(0.5751265, -0.03368972);
var point1 = new OpenLayers.Geometry.Point(lonlatObject1.lon,
lonlatObject1.lat);
And also my bad, I click "reply" instead of "reply to all" and the messages
weren't send to the list, so if this works for you please reply yourself to the
list with the solution, so if anyone searches the list and finds your question
can also find the solution.
Best regards,
Xurxo Méndez Pérez
http://www.sonxurxo.com
2011/1/25 <[email protected]>
Hello,
Thanks for the help! I tried following code:
var lonlatObject1 = new OpenLayers.LonLat(0.5751265, -0.03368972);
var point1 = new OpenLayers.Geometry.Point(lonlatObject1.x,
lonlatObject1.y);
alert(point1.x);
alert gives me the message "NaN". I guess that means that result is not a
number.
Any ideas why this occurs? Actually the x should be a normal float,
shouldn't it?
From: Xurxo Mendez
Sent: Tuesday, January 25, 2011 8:51 AM
To: rikner
Subject: Re: [OpenLayers-Users] How to deal with Geometry.Point and lonlat
Hi,
OpenLayers.LonLat and OpenLayers.Geometry.Point are not directly related to
each other (someone please correct me if I'm wrong). LonLat objects represent
simply a longitude and latitude pair, while Point instances represent
geometries (they are a subclass of OpenLayers.Geometry, and as such they can be
used wherever a Geometry is expected, as in OpenLayers.Feature.Vector
instances).
If you want to create a Point from a LonLat object (I'm not sure if what
your webapp gives you are OpenLayers.LonLat objects or "latitude and longitude
info"), you could do something like
var lonlatObject = //obtained from the list-entry
var point = new OpenLayers.Geometry.Point(lonlatObject.x, lonlatObject.y);
and then set the point as the geometry of an OpenLayers.Feature.Vector
instance, or add it to an array to pass it to the LinearRing constructor.
I hope this helps you!
Best regards,
Xurxo Méndez Pérez
http://www.sonxurxo.com
2011/1/24 rikner <[email protected]>
Hello everyone,
this my very first post and I'm new to the whole OpenLayers thing.
Let me get to the point quickly...
I want to highlight buildings in an OpenLayers Map.
I already figured out a way how to do this by using a vectorLayer.
I create a linearRing which gets an array of Geometry.Points (representing
the coordinates of the building). Then I create a Feature.Vector (here
"polygonFeature") with this linearRing and do:
vectorLayer.addFeatures([polygonFeature]);
So far so good. But I asked myself why I have to use Geometry.Point and
not
just simply LonLat coordinates?
My web-application on which I'm working is showing me a list of buildings.
When I click on an list-entry I get a LonLat-Array containing the
'vertices'
of the building. So now when I want to highlight my building by doing the
above mentioned I need to convert these LonLats into Geometry.Points. How
am
I supposed to do that?
Maybe you could give me short overview how LonLat and Geometry.Points are
related to each other.
I just can't make a sense of it right now. Thanks already for your help. I
would appreciate a quick answer.
Best regards - Erik
--
View this message in context:
http://osgeo-org.1803224.n2.nabble.com/How-to-deal-with-Geometry-Point-and-lonlat-tp5956361p5956361.html
Sent from the OpenLayers Users mailing list archive at Nabble.com.
_______________________________________________
Users mailing list
[email protected]
http://lists.osgeo.org/mailman/listinfo/openlayers-users
_______________________________________________
Users mailing list
[email protected]
http://lists.osgeo.org/mailman/listinfo/openlayers-users
_______________________________________________
Users mailing list
[email protected]
http://lists.osgeo.org/mailman/listinfo/openlayers-users