Link to home
Start Free TrialLog in
Avatar of lz7cjc
lz7cjc

asked on

Update google map center with value from input box

Hi
I have this code:
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>

   <script src="http://maps.googleapis.com/maps/api/js"></script>
    <script>
function initialize() {
  var mapProp = {
    center:new google.maps.LatLng(51.508742,-0.120850),
    zoom:12,
    mapTypeId:google.maps.MapTypeId.ROADMAP
  };
  var map=new google.maps.Map(document.getElementById("map"),mapProp);
}
google.maps.event.addDomListener(window, 'load', initialize);

    function codeAddress() {
        alert("yes");
                  var address = document.getElementById('address').value;
                  geocoder.geocode( { 'address': address}, function(results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                      map.setCenter(results[0].geometry.location);
                    }
                    else {
                      alert('Geocode was not successful for the following reason: ' + status);
                    }
                  });
                }

    </script>
</head>
<body>
      <input id="address" type="textbox">
      <input type="button" value="Geocode" onclick="codeAddress()">
 <style>

    #map {
        height: 500px;
        width: 800px;
        }

    </style>
          <div id="map"></div>

</body>
</html>

Open in new window


The map loads fine and centres on London but when I enter a place in the text field and hit enter it doesn't update the center of the map to the new location. What am I missing?

thanks
ASKER CERTIFIED SOLUTION
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lz7cjc
lz7cjc

ASKER

fantastic thanks
Avatar of lz7cjc

ASKER

No...same one! Do you know how ? Can close it? I do have another one open on responsive design and resizing map, if you want to help me out on that?
You can just delete the question if there are no posts to it.

I'll take a look at the responsive design question.