0

I have been looking at an example of geocoding on the Google maps dev website. I found this example and would like to as an experiment hard code a postal code instead of using the input box to get the address (the overall aim is to have an xml place the postal address inside here). Is it just a case of hard coding the postal address inside of the 'address' tags? I did try this but to no joy. Hope that makes sense?

<script>
    var geocoder;
    var map;

    function initialize() {

        geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(-34.397, 150.644);

        var mapOptions = {
            zoom: 8,
            center: latlng
        }

        map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
    }

    function codeAddress() {

        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);
                var marker = new google.maps.Marker({
                  map: map,
                  position: results[0].geometry.location
                });
            } else {
                alert('Geocode was not successful for the following reason: ' + status);
            }

        });
    }

    google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
    <div id="panel">
      <input id="address" type="textbox" value="Sydney, NSW">
      <input type="button" value="Geocode" onclick="codeAddress()">
    </div>
    <div id="map-canvas"></div>
</body>

https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple

Thiago C. S Ventura
  • 2,448
  • 1
  • 29
  • 43
efc84
  • 35
  • 5

1 Answers1

0

If I understood well, you can try like this:

http://jsfiddle.net/7g7qG/

var geocoder;
var map;
function initialize() {
  geocoder = new google.maps.Geocoder();
  var latlng = new google.maps.LatLng(-34.397, 150.644);
  var mapOptions = {
    zoom: 8,
    center: latlng
  }
  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
  codeAddress('London')
}

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

google.maps.event.addDomListener(window, 'load', initialize);
Thiago C. S Ventura
  • 2,448
  • 1
  • 29
  • 43
  • Exactly what i needed! I'm not the best at explaining problems but you understood, thank you! – efc84 Jan 08 '14 at 12:52
  • additional question, would it be possible to use a for loop instead of hard coding an address in? I have got an xml file with postcodes and would like to print each point on the map using the same geocoding, is this a suitable way to go about it? – efc84 Jan 08 '14 at 16:05
  • take a look at this: https://developers.google.com/maps/documentation/javascript/examples/map-projection-simple Might help – Thiago C. S Ventura Jan 08 '14 at 16:14
  • Can a variable be placed inside of the 'codeAdress' brackets? The link you provided was not much help sorry. @Ventura – efc84 Jan 09 '14 at 12:47
  • http://stackoverflow.com/questions/5413122/showing-many-markers-in-google-maps Try searching for 'google maps markers many' – Thiago C. S Ventura Jan 09 '14 at 13:55
  • actually I think my problem is displaying/loading a url which contains the xml data that can then be incorporated into a for loop @Ventura – efc84 Jan 09 '14 at 14:16