1

I'm using GeoServer, PostgreSQL and here OpenLayers. I'm trying to retrieve two lat-lon points (for point A and point B) on clicking of map. This code is working only to retrieve one lat-lon.

I want to use this for four (two lat-lon) points in JSP file, that I will use to find shortest path between that points.

So how can I perform this?

OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, {                
                defaultHandlerOptions: {
                    'single': true,
                    'double': false,
                    'pixelTolerance': 0,
                    'stopSingle': false,
                    'stopDouble': false
                },

                initialize: function(options) {
                    this.handlerOptions = OpenLayers.Util.extend(
                        {}, this.defaultHandlerOptions
                    );
                    OpenLayers.Control.prototype.initialize.apply(
                        this, arguments
                    ); 
                    this.handler = new OpenLayers.Handler.Click(
                        this, {
                            'click': this.trigger
                        }, this.handlerOptions
                    );
                }, 

                trigger: function(e) {
                    var lonlat = map.getLonLatFromPixel(e.xy);
                    var lat=lonlat.lat;
                    var lon=lonlat.lon;

                    //alert("You clicked near " + lonlat.lat + " N, " +
                      //                        + lonlat.lon + " E");
                    window.location.replace("atobNewForPath.jsp?lat="+lat+"&lon="+lon);

                }
            });

 var click = new OpenLayers.Control.Click();
            map.addControl(click);
            click.activate();

In body tag

<%
    double lat = Double.parseDouble(request.getParameter("lat"));
    double lon = Double.parseDouble(request.getParameter("lon"));

/*How to perform this one
    double lat2 = Double.parseDouble(request.getParameter("lat2"));
    double lon2 = Double.parseDouble(request.getParameter("lon2"));
    */

    out.println(lat);
    out.println(lon);


    }%>
8
  • Are you asking how to save the previously clicked point in the trigger function? Oct 13 '15 at 4:28
  • exactly and want to use that two point(previous and current) in java tag as variable.
    – Yash
    Oct 13 '15 at 15:54
  • So, you need to create two more variables inside your overriden Control.Click class, lat_last, lon_last, for example, and update those to previous values of lat/lon, if they exist. Assuming all 4 have values, and are different, then you can call your jsp function. Oct 13 '15 at 16:16
  • i had tried this yesterday <dropbox.com/s/gkqdfzu7p20v3mj/path.txt?dl=0>
    – Yash
    Oct 13 '15 at 16:38
  • Above code also not work. and my page stucture : javascript defer init() { click event code and map creation code } <body onload=init()>
    – Yash
    Oct 13 '15 at 16:56
0

Please try below code. it might be useful in this scenario...

var lat1='',lat2='',lon1='',lon2='';var index=0;trigger: function(e) {
                var lonlat = map.getLonLatFromPixel(e.xy);
if ( index==0)
{
                 lat1=lonlat.lat;
                 lon1=lonlat.lon;

}else(index==1){
                 lat2=lonlat.lat;
                 lon2=lonlat.lon;
window.location.replace("atobNewForPath.jsp?lat1="+lat+"&lon1="+lon1 + "&lat2="+lat+"&lon2="+lon1);}else{
    alert('Please press reset button');
}
index=index+1;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.