I'm making a Openlayers map with some shapefile and I have Google Maps base layer. I want to create a text area and a button. When I input a address and click the button, the map should show me the google maps address in the map. Can you help me please ?
2 Answers
Try the below code.
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log(results[0].geometry.location);
}
else {
console.log("Geocoding failed: " + status);
}
});
this is my code
<script src='http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAjpkAC9ePGem0lIq5XcMiuhR_wWLPFku8Ix9i2SXYRVK3e45q1BQUd_beF8dtzKET_EteAjPdGDwqpQ'></script>
function search(){
//alert('search start');
var address = document.getElementById('address').value+",Kathmandu, Nepal";
//alert(address);
var geocode_url="http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address="
//needs google api
var geocoder = new GClientGeocoder();
//geocoder.setBaseCountryCode('Nepal');
var location = geocoder.getLatLng(address,
function(point) {
if (!point) {
alert(address + " not found");
} else {
//alert("going to "+address);
var loc = new OpenLayers.LonLat(point.x,point.y);
map.setCenter(loc,3);
}
}
);
}