3

I'm trying to combine google's geocoding service with openlayers. So when a user types an address and hits enter, map zooms there and renders a simple point. I edited my code according to the previous link. Does not work. I'm using a vector layer as basic (comes from Geoserver/PostGIS), a google's layer as second basic. I also added a simple vector layer to render the adrress's point. Do I have to bind google's layer and geocoding service somehow?

Here is my code. Can you spot what's wrong?

var geocoder;
function codeAddress() 
{alert ('inside function');//works
var address = document.getElementById("address").value;    

//tell to user, he did not type anything
if (address==""){document.getElementById("geocodMsgG").innerHTML = "You gave no input";}
else{

alert(address);//works

geocoder.geocode( { 'address': address}, function(results, status) 
  {alert(status);//not work

   if (status == google.maps.GeocoderStatus.OK) {     
     alert('inside 2 function');//not work
     alert(results[0].geometry.location);//not work
     //create a geometry point
     pointgc = new OpenLayers.Geometry.Point(results[0].geometry.location);

     //create a feature point
     feature_point_gc = new OpenLayers.Feature.Vector(pointgc);

       //lfl contains bounds that i set earlier in my code
       if (lfl.containsLonLat(results[0].geometry.location)){
       //render the point on the layer named geoco
       geoco.addFeatures([feature_point_gc]);
       geoco.refresh({force:true});
       //zoom there
       map.setCenter(results[0].geometry.location);   
      }
      //if point is not inside the bounds (lfl) then dont add - print msg to user
      else{document.getElementById("geocodMsgG").innerHTML = "out of bounds";}
  } 

  //refers to the second if(geocoding's status)
  else {document.getElementById("geocodMsgG").innerHTML = "error";}    

});//geocode 
}//major else (if address empty or not)
}//function
7
  • This is not an OpenLayers question, suggest you remove tag. Also, try to simplify your code and/or create a separate test page that ONLY geocodes, nothing else. Have it geocode a static address or an input field and output to console w/o a map. Good luck.
    – Vadim
    Commented May 17, 2013 at 20:43
  • @ Vadim I thought it is an OpenLayers question, because I'm trying to render the output of geocoding API on an OpenLayers map. Anyway, create a test page is not a bad idea, thanks
    – slevin
    Commented May 17, 2013 at 20:57
  • 1
    This'll be an OL question when it becomes "hey, I can get the results back from google and output to console/div/alert but can't plot them on OL map". However, be aware that Google Maps TOS require you to plot google data on a google map.
    – Vadim
    Commented May 17, 2013 at 21:05
  • 1
    Given Google's TOS restriction, MapQuest's geocoding api might be an alternative.
    – Cyrus
    Commented May 17, 2013 at 22:46
  • Well if google require a google map, just add the google layers as base layer and set it active when the search runs OK. SO you can use it with OpenLayers too.
    – cavila
    Commented May 18, 2013 at 1:56

2 Answers 2

1

If you are able to use Openlayers 3 you can make use of an extension that I've written, some instructions at: https://github.com/jonataswalker/ol3-geocoder. A screenshot:

enter image description here

0

Looks like geocoder variable was not initialized. it is an object instance, not a static class, or function.

 geocoder = new google.maps.Geocoder();

There should be a javascript error on page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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