2017-11-09 1 views
-3

Ich habe eine Seite, wo ich eine Karte nach einer Adresse laden muss, um die Eigenschaft an diesem Punkt zu sehen. Um das zu tun habe ich die Google Maps API gefunden, aber ich kann das noch nicht funktionieren. Ich versuche ein Beispiel, das ich in der Dokumentation sah, aber es funktioniert immer noch nicht und löst keine Ausnahme aus.Wie kann ich die Karte laden, wenn die Seite bereits geladen ist?

Here de Dokumentation.

Wie könnte ich das tun?

Loading Google Maps API

<script src="https://maps.google.com/maps/api/js?key=AIzaSyA8JZPv2N9bE0OQABj6hKO9QZb0kH32l"></script> 

Script Karte zu laden, Adresse

$(document).ready(function() { 
    initialize(); 
    codeAddress(); 
}); 

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('gmap'), mapOptions); 
} 

function codeAddress() { 
    var address = document.getElementById('myAddress').value;  
    geocoder.geocode({ 'address': address }, function (results, status) { 
     if (status == '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); 
     } 
    }); 
} 

HTML

<div id="gmap"></div> 

<!--my address--> 
@Html.HiddenFor(model => model.myAddress) 
+0

Wird der Warnfehler angezeigt? –

+1

Ihr Code sieht gut aus. Was passiert gerade ? Hast du Skriptfehler? – Shyju

Antwort

-1

Ich habe das Problem endlich gelöst.

function loadByAddress() { 
    var address = $('#myAddress').val(); 
    var geocoder = new google.maps.Geocoder(); 
    geocoder.geocode({ 'address': address }, function (results, status) { 
     if (status == 'OK') { 
      var latlng = new google.maps.LatLng(results[0].geometry.location.lat, results[0].geometry.location.lng); 
      var myOptions = { 
       zoom: 20, 
       center: latlng, 
       mapTypeId: google.maps.MapTypeId.SATELLITE 
      }; 
      var map = new google.maps.Map(document.getElementById("gmap"), myOptions); 
      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", loadByAddress); 
+0

Was war das Problem? – Shyju

Verwandte Themen