Frage

Ich bin neu bei Google Maps und versuche einfach, den Dreh raus zu bekommen.Ich möchte in der Lage sein, eine Stadt oder Adresse einzugeben und sie dann von der Karte laden zu lassen und eine Markierung zu platzieren.

Im Moment wird meine Karte anfangs gut geladen, aber wenn ich eine Stadt oder Adresse eingebe, passiert nichts.Irgendeine Idee, was hier los ist?

<script type="text/javascript">
var geocoder;
var map;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlang = new google.maps.LatLng(42, -84);
    var myOptions = {
        center: latlang, zoom: 5, mapTypeId: google.maps.MapTypeId.SATELLITE,
        navigationControlOptions: {
            style: google.maps.NavigationControlStyle.SMALL
        }
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);
  }

  function codeAddress() { 
    var sAddress = document.getElementById("newLocation").value;
    geocoder.geocode( { 'address': sAddress}, 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);
            }
        });
  }
</script>
</head>
  <body onload="initialize()">
    Address: <input type="text" id="newLocation" style=" width:200px" title="Address to Geocode" />     
    <input type="button" onclick="codeAddress()" id="inputButtonGeocode" style="width:150px" title="Click to Geocode" value="Geocode" />
    <div id="map_canvas" style="width:100%; height:100%"></div>
  </body>
</html>
War es hilfreich?

Lösung

Entsprechend

var map = new google.maps.Map(document.getElementById("map_canvas")...

entfernen Sie das Wort var.Weil es schafft map in einem funktional lokalen Bereich und die Geokodierungsfunktion sieht nur die vorherige (leere) map variabel.Durch Entfernen der globalen map variable wird auf diese neue Google Map aktualisiert.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top