Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am trying to create some code in GeoExt that will allow me to centre that map layer I am viewing to coordinates that have been submitted through a formPanel. I would have thought that this would have been fairly straightforward but this is proving to be quite troublesome and I am wondering if any of you guys could help?

So far I have created the the panel with 2 boxes to accept the eastings and northings but can't work out how to get the values that have been submitted to actually move the image.

The code I have cobbled together this far is:

formPanelCoord = new GeoExt.form.FormPanel({
    border: true,
    layout: 'anchor',
    bodyStyle:'padding: 20px',
        items: [{
        xtype: "textfield",
        width: 100,
        fieldLabel: "Easting",
        name: "search_lc",
        value: "",
        emptyText:'Easting',
        allowblank: false
    },

{
        xtype: "textfield",
        width: 100,
        fieldLabel: "Northing",
        name: "search_lc",
        value: "",
        emptyText:'Northing',
        allowblank: false
    }],



    listeners: {
       actioncomplete: function(form, action) {

        features = action.response.features;
        console.log(features.length)
     }



  }


});


formPanelCoord.addButton({
        text:"Go to Coord",
        handler:function() {
        console.log(formPanelCoord);
            //alert(formPanelCoord.items.items[0].getValue());
            //alert(formPanelCoord.items.items[1].getValue());
            formPanelCoord.items.items[0].getValue();
            formPanelCoord.items.items[1].getValue();
                       //function recentreMap (form) {
                       var easting = from.Easting.value;
                       var northing = form.Northing.value;
                       //var easting = formPanelCoord.items.items[0].value;
                       //var northing = formPanelCoord.items.items[1].value;
                       map.setCenter(new OpenLayers.LonLat(easting,northing));


formPanelCoord.addButton({
        text:"Go to Coord",
        handler:function() {
            this.search();
     },
     scope:formPanelCoord
});


            //this.search();
     },
     scope:formPanelCoord
});

Any help gratefully received!

share|improve this question
add comment

1 Answer 1

Assuming your points are latitude and longitude you can use the OpenLayers Map panTo function.

//assuming map is the map object
//and x and y are in map projection
map.panTo(new OpenLayers.LonLat(x, y));

If your input points aren't in the map projection you'll have to project them. If Eastings and Northings aren't in one of the default OpenLayers projections you'll have to include the proj4js library to build projection objects.

//define a prjection, proj4 string from http://spatialreference.org
Proj4js.defs["EPSG:28355"] = "+proj=utm +zone=55 +south +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs";

in_projection = new OpenLayers.Projection("EPSG:28355");
map_projection = new OpenLayers.Projection("EPSG:4326"); //default OpenLayers map projection

zoom_point = new OpenLayers.LonLat(easting, northing);
zoom_point.transform(in_projection, map_projection); //in place transform
share|improve this answer
    
Many thanks for your reply Chad. If the projection has already been declared earlier in the code when an OpenLayers Map is instantiated, can the pan function be used in this instance or would I still need to include the proj4js library? –  Red Jan 22 '13 at 9:23
    
Happy to help. And you should be ok - as long as the points are in the same projection as the map then you should be fine to do the pan. –  om_henners Jan 22 '13 at 10:18
    
Yep, it did work! I had to alter the code to fetch the values from the input boxes to this: var easting = formPanelCoord.items.items[0].getValue(); var northing = formPanelCoord.items.items[1].getValue(); And then it was simply a case of re-centering the map using: map.setCenter(new OpenLayers.LonLat(easting,northing)); Once again, very many thanks for your help with this Om, no doubt I'll be back when I confuse myself again! –  Red Jan 23 '13 at 10:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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