[GIS] Openlayers toggle Marker controls and update information in external form fields

javascriptopenlayers-2web-mapping

I am setting a form (for multiple addresses) where users can click on map by creating a marker for each address they want to input. Users can only create one marker per address and once the marker has been created I need to get nearest address and update the address field (form input) with that address.

I am able to activate the marker drawing control but how can I make sure that user only creates one marker (not more) per address and then take that calculated address and update original text field (which fired the event) with new information.

This is what I have so far:

<form name="xxxx">

POI 1: <input type="text">
<a href="#" onClick="edrawMarker(event)"> click here to select address on map</a>
POI 2: <input type="text">
<a href="#" onClick="edrawMarker(event)"> click here to select address on map</a>


</form>
<a href="#"> Add more poi's </a>

Here's the javascript code

poi_markers = new OpenLayers.Layer.Vector('markers',{styleMap:markerStyleMap});
poi_marker_control = new openLayers.Control.DrawFeature(poi_markers,OpenLayers.Handler.Point);  


function e_drawPOIMarker(evt){

        route_markers.events.register('featureadded',poi_markers,function(evt){
         {
         //get JSON response
                     var place_name = getGeoName(evt.feature.geometry)  
                     poi_marker_control.deactivate();
                            //Somehow find the id of the text field which fired original event and update its contents with data returned in place name.
        }
    });
        poi_marker_control.activate();  
    }

Basically I have to capture co-ods of locations clicked by users. Each field (in form) should only allow 1 marker. Suppose if I have unknown numbers of poi address fields (which users can add dynamically) whats the best way of capturing coods and then updating original fields with that information.

At the moment I can activate and deactivate controls and allow users to create marker(s). but how can I know which marker belongs to which text field ?

Thanks.

Best Answer

I would probably add a token to the *e_drawPOIMarker()* function. When called, just test if a marker having the passed token already exists or not.