3

I have added a feature map to my OpenLayers 2 map, by using the following code. When I click a spot on the map, it correctly retrieves the feature name for that place according to the feature layer.

Code:

       featureControl = new OpenLayers.Control.WMSGetFeatureInfo({
           url: featureLayer.url,
           title: '',
           layers: [ featureLayer ],
           infoFormat: 'text/xml',
           queryVisible: true
       });
       featureControl.events.register('getfeatureinfo', this, function(event) {

           var xml = new window.DOMParser().parseFromString(event.text, 'application/xml'),
               path = featureLayer.$featureInfoXPath,
               featureName;
           try {
               featureName = xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null).iterateNext().value;

                console.log('got the feature:', featureName)
           } catch ( error ) {
               console.log('error', error);
           }
       }); 
       map.addControl(featureControl);
       featureControl.activate();

However, I would like to trigger this retrieval of the feature name on other events as well, such as when I type in an address in an input field and I select an address in an auto complete. So I am looking for a mechanism to trigger the GetFeatureInfo event by something other than "click".

Is this possible, and if so, how?

2
+100

Once you have latitude and longitude of where you would like to get info. You can make your getFeatureFnfo request directly.

For example if you have a search bar that geocodes addresses to lat/lon, you can make a getFeatureInfo request with that callback data.

geocoder.geocode( { 'address': address}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
    var loc=[];
    loc[0]=results[0].geometry.location.lat();
    loc[1]=results[0].geometry.location.lng();

    getFeatures(loc); 

  } else {
    alert("Geocode was not successful: " + status);
  }

 function getFeatures( long_lat ){
      bounds = new OpenLayers.Bounds();
      bounds.extend(new OpenLayers.LonLat(long_lat[1]-.0001,long_lat[0]-.0001));
      bounds.extend(new OpenLayers.LonLat(long_lat[1],long_lat[0]));
      bounds.toBBOX();
      $.ajax({
        type:'GET',
        url: "http://demo.opengeo.org/geoserver/wms?bbox="+bounds+"&styles=&format=jpeg&request=GetFeatureInfo&layers=topp%3astates&query_layers=topp%3astates&width=2&height=2&x=1&y=1",
        success: function (data) {
          console.log(data);
        }
      })

I've hacked together the bbox, but hopefully this gives you an idea of how to go about a custom getFeatureInfo request.

If you want to avoid using ajax, take a look at this thread for registering events. OpenLayers custom request for WMS GetFeatureInfo

I've created an example on codepen.

http://codepen.io/owenjam/pen/ZBaNdw

0

It seems that if you were to write a function which could query the information layer or google maps for example, you could pull back the GPS location of the item / address that you searched for in that function-box. When you have that location returned back you could call getfeatureinfo(location from searchbox) rather than getfeatureinfo(from mouse click location).

In this way you could write any kind of call from different sources which could trigger the getfeatureinfo call.

0

getfeatureinfo api requires coordinates as parameters. So you need to retrieve the location from :

when I type in an address in an input field and I select an address in an autocomplete

on the other hand, you can also run a attribute query using the text from search/address box if you have certain field in you data.

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.