[OpenLayers-Users] Trying to draw a box on a map, and get the coordinates back.

Arnd Wippermann arnd.wippermann at web.de
Thu Jan 7 16:55:06 EST 2010


Hi John,

To get geoJSON in return use OpenLayers.Format.GeoJSON():

var formats = {
    wkt:     new OpenLayers.Format.WKT(),
    geojson: new OpenLayers.Format.GeoJSON(),
    georss:  new OpenLayers.Format.GeoRSS(),
    gml:     new OpenLayers.Format.GML(),
    kml:     new OpenLayers.Format.KML()
};

var type = "geojson";
//var type = "gml";
var theParser = formats[type];

function findcoords (event) {
    var data = theParser.write(event.feature);
    alert('ID:' + data);

    //only relevant for geoJSON, and only one feature
    var objData = eval ( '(' + data + ')');
    alert(objData["geometry"]["coordinates"]);
}

vectors.events.register('featureadded', vectors, findcoords);
 
Arnd

-----Ursprüngliche Nachricht-----
Von: users-bounces at openlayers.org [mailto:users-bounces at openlayers.org] Im
Auftrag von Jtrsmith
Gesendet: Donnerstag, 7. Januar 2010 19:36
An: users at openlayers.org
Betreff: Re: [OpenLayers-Users] Trying to draw a box on a map, and get the
coordinates back.


thank you that works.
is it possible to get the vertices in a geojson coordinate format? 

i.e.
[-277, 359], [-277, 407], [-238, 407], [-238, 359], [-277, 359]

instead of
POINT(-277 359),POINT(-277 407),POINT(-238 407),POINT(-238 359)
 
thanks again

John



Arnd Wippermann wrote:
> 
> Hi,
> 
> That should it be. 
> 
> function findcoords (event) {
>      var vertices = event.feature.geometry.getVertices();
>      alert('ID:' + vertices);
> }
> 
> Arnd
> 
> -----Ursprüngliche Nachricht-----
> Von: users-bounces at openlayers.org 
> [mailto:users-bounces at openlayers.org] Im Auftrag von Jtrsmith
> Gesendet: Donnerstag, 7. Januar 2010 17:27
> An: users at openlayers.org
> Betreff: Re: [OpenLayers-Users] Trying to draw a box on a map, and get 
> the coordinates back.
> 
> 
> i am not sure i explained what i was looking for correctly. here is an 
> example.
> 
> vectors.events.register('featureadded', vectors, findcoords); function 
> findcoords (feature) {
>      var vertices = feature.geometry.getVertices();
>      document.getElementById('output').value = 'ID:' + vertices; }
> 
> the event does actually trigger, however, 
> feature.geometry.getVertices() does not work.
> 
> i am using geojson to display other polygons on the map, and would 
> like the variable vertices to get the coordinates of the polygon 
> vertices so that i can add a new polygon to the map and save the 
> coordinates in a database.
> 
> John
> 
> 
> micklesh wrote:
>> 
>> Hi, John.
>> if i understood your requirements, there is an example in the docs 
>> http://dev.openlayers.org/docs/files/OpenLayers/Control-js.html
>> 
>> regards, michael.
>> 
>> 2010/1/4 John Smith <jtrsmith at gmail.com>
>> 
>>> i just saw this post and was wondering if the solution was ever 
>>> found. i am basically trying to do the same thing. I need to draw a 
>>> box around an area of my map and show the coordinates in a form 
>>> field, so i can post the coordinates and a description to my 
>>> database.
>>>
>>> _______________________________________________
>>> Users mailing list
>>> Users at openlayers.org
>>> http://openlayers.org/mailman/listinfo/users
>>>
>>>
>> 
>> _______________________________________________
>> Users mailing list
>> Users at openlayers.org
>> http://openlayers.org/mailman/listinfo/users
>> 
>> 
> 
> --
> View this message in context:
> http://n2.nabble.com/Trying-to-draw-a-box-on-a-map-and-get-the-coordin
> ates-b
> ack-tp4251287p4267311.html
> Sent from the OpenLayers Users mailing list archive at Nabble.com.
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users
> 
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users
> 
> 

--
View this message in context:
http://n2.nabble.com/Trying-to-draw-a-box-on-a-map-and-get-the-coordinates-b
ack-tp4251287p4268014.html
Sent from the OpenLayers Users mailing list archive at Nabble.com.
_______________________________________________
Users mailing list
Users at openlayers.org
http://openlayers.org/mailman/listinfo/users




More information about the Users mailing list