openlayers and google geocoder
i'm trying to use the google geocoding service with an openlayer map. this shoudl work without problem...should it not? the code i'm using is just like the example on googles geocode api doc:
function geoCode(){
var adresse = $("observation_location_text").getValue();
var geoCoder = new google.maps.Geocoder();
alert(adresse);
geoCoder.geocode( { 'address': address}, function(results, status) { 开发者_JAVA技巧
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
marker.moveTo(results[0].geometry.location);
marker.display(true);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
}); }
for some reason i do not get the Geocoder object. the alert with address is never executed.
looking forward to some hints jan
Try to remove any dependency from OpenLayers and see if it works. You should be able to create geocoder object with plain Google Maps API.
On little thing I noticed is that you read value from input into variable called "adresse" but pass to geocoder variable callled "address". If it's just a typo and second variable is not declared you get script error and code stops executing. Probably that's why you geocoder object is never created.
Reading the comments on the first answer - are you sure the Google Maps files have loaded when you run "new google.maps.Geocoder()"?
You say that the code gets called on load, in what way do you do it? Since you are using jQuery, it should be done in $(document).ready().
i' m trying to do the exact same thing as you. Here is my problem, it may help you link
Also , if you try to pass the geocoding's point on an openlayers' layer be aware that the points on openlayers vector layers are like (lon, lat) BUT the points came from Google geocoding are like (lat,lon). I'm copying from Googles page : "location contains the geocoded latitude,longitude value. Note that we return this location as a LatLng object, not as a formatted string" ( Googles page)
But I think the problem is that "google geocoder unfortunately dictate that you use the coordinates to map onto a google map.", as El Duderino says...
Oh, and also, if you use Drupal, check this out...
精彩评论