3

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 ?

1

this is my code

<script src='http://maps.google.com/maps?file=api&amp;v=2&amp;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);
            }
        }
    );
}
2

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);                            
        }
    });

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.