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'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?

Thank you...

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
share|improve this question
    
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 May 17 '13 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 May 17 '13 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 May 17 '13 at 21:05
1  
Given Google's TOS restriction, MapQuest's geocoding api might be an alternative. –  Cyrus May 17 '13 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 May 18 '13 at 1:56
show 2 more comments

1 Answer 1

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.

share|improve this answer
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.