2017-12-18 6 views
-1

Ich versuche herauszufinden, warum das Polygon nicht auf meinen Google Maps gezeichnet wird. Ich habe es auf das Array geschlossen, kann aber nicht sehen, was ich falsch mache, um ehrlich zu sein. Ich habe den Google API KEY aus meinem Code gelöscht, um es kurz zu machen.Polygon wird nicht auf der Karte von Koordinaten (von Array) gezeichnet

Irgendwelche Tipps/Feedback?

<!DOCTYPE HTML> 
<html> 
<head> 
</head> 
<body> 
    <div id="map" style="width:100%;height:400px;"></div> 
</body> 
<script> 
    function initialize() 
    { 
     //fill array with coordinates 
     var path = [ 
      [51.14920179999362, 3.706512451171875], 
      [50.99042122689005, 3.475799560546875], 
      [50.93852713736125, 3.73809814453125], 
      [50.95929172950454, 4.003143310546875], 
      [51.108695514831865, 3.972930908203125] 
     ]; 

     //Options for the map 
     var mapOptions = { 
      zoom: 10, 
      center: new google.maps.LatLng(51.0108706, 3.7264613), 
     } 

     //generate map 
     var map = new google.maps.Map(document.getElementById('map'), mapOptions); 

     //options for the polygon 
     var polyOptions = { 
      paths: path, 
      strokeColor: '#FF0000', 
      strokeWeight: 2, 
      strokeOpacity: 0.8, 
      fillColor: '#FF0000', 
      fillOpacity: 0.35, 
      editable: false, //editeren van de polygon 
      draggable: false //verplaatsen van de polygon 
     }; 

     //create the polygon 
     var polygon = new google.maps.Polygon(polyOptions); 
     polygon.setMap(map);  
    } 
    google.maps.event.addDomListener(window, 'load', initialize); 
</script> 

</html> 
+0

Was passiert, wenn Sie die 'ersetzen;' gerade achtern er 'Pfade: Pfad' mit einem', '? –

+0

Gibt es Ausgaben in der Browserkonsole, die das Verhalten erklären können? – tatmanblue

+0

Ich erhalte ein Javascript Error: 'Nachricht : " bei Index 0: bei Index 0: kein LatLng oder LatLngLiteral: kein Objekt "' (weil Sie ein Array von Arrays übergeben, nicht LatLng oder LatLngLiteral Objekte) – geocodezip

Antwort

1

Übersetzen Sie Ihre Array von Arrays in ein Array von LatLngLiteral Objekte (oder LatLng Objekte).

var fixedPath = []; 
for (var i=0; i<path.length; i++) { 
    fixedPath.push({lat:path[i][0],lng:path[i][1]}); 
} 
//options for the polygon 
var polyOptions = { 
    paths: fixedPath, 

proof of concept fiddle

enter image description here

Code-Schnipsel:

function initialize() { 
 
    //fill array with coordinates 
 
    var path = [ 
 
    [51.14920179999362, 3.706512451171875], 
 
    [50.99042122689005, 3.475799560546875], 
 
    [50.93852713736125, 3.73809814453125], 
 
    [50.95929172950454, 4.003143310546875], 
 
    [51.108695514831865, 3.972930908203125] 
 
    ]; 
 

 
    //Options for the map 
 
    var mapOptions = { 
 
    zoom: 10, 
 
    center: new google.maps.LatLng(51.0108706, 3.7264613), 
 
    } 
 

 
    //generate map 
 
    var map = new google.maps.Map(document.getElementById('map'), mapOptions); 
 
    var fixedPath = []; 
 
    for (var i = 0; i < path.length; i++) { 
 
    fixedPath.push({ 
 
     lat: path[i][0], 
 
     lng: path[i][1] 
 
    }); 
 
    } 
 
    //options for the polygon 
 
    var polyOptions = { 
 
    paths: fixedPath, 
 
    strokeColor: '#FF0000', 
 
    strokeWeight: 2, 
 
    strokeOpacity: 0.8, 
 
    fillColor: '#FF0000', 
 
    fillOpacity: 0.35, 
 
    editable: false, //editeren van de polygon 
 
    draggable: false //verplaatsen van de polygon 
 
    }; 
 

 
    //create the polygon 
 
    var polygon = new google.maps.Polygon(polyOptions); 
 
    polygon.setMap(map); 
 
} 
 
google.maps.event.addDomListener(window, 'load', initialize);
html, 
 
body, 
 
#map { 
 
    height: 100%; 
 
    width: 100%; 
 
    a margin: 0px; 
 
    padding: 0px 
 
}
<script src="https://maps.googleapis.com/maps/api/js"></script> 
 
<div id="map"></div>

+0

Ich sehe was du da gemacht hast ... einfach unglaublich! Danke für die Hilfe! Jetzt kann ich weitermachen. – TDB

Verwandte Themen