Isochrones API-Antwort verarbeiten und visualisieren

Die Isochrones API gibt Geometrie im Standard-GeoJSON-Format zurück. Die Antwort enthält ein MultiPolygon-Geometrieobjekt.

Erreichbare Isochronenbereiche sind nicht immer zusammenhängende einzelne Formen. Über eine Brücke kann man beispielsweise oft eine Insel erreichen, wodurch ein erreichbarer Bereich entsteht, der vom Festland getrennt ist. Außerdem können unzugängliche Bereiche wie eine private Wohnanlage mit Toren oder ein See Löcher innerhalb des Hauptpolygons erzeugen.

Das folgende Beispiel zeigt ein typisches Antwortformat. Beachten Sie, dass GeoJSON die Koordinatenreihenfolge [longitude, latitude] einhält.

{
  "isochrone": {
    "geoJson": {
      "type": "MultiPolygon",
      "coordinates": [
        [
          [
            [-122.332100, 47.606200],
            [-122.332200, 47.606300],
            ...
          ]
        ]
      ]
    }
  }
}

Isochrone auf einer Karte visualisieren

Da die API Standard-GeoJSON zurückgibt, können Sie die Antwort mit der Maps JavaScript API visualisieren. Die Klasse google.maps.Data unterstützt GeoJSON nativ. Das bedeutet, dass Sie die Koordinatenreihenfolge [longitude, latitude] nicht manuell umkehren müssen.

Das folgende JavaScript-Snippet zeigt, wie Sie die API-Antwort direkt einer Karte hinzufügen:

// Assume 'map' is an initialized google.maps.Map object
// and 'response' is the JSON payload returned by the Isochrones API.

const isochroneGeoJson = response.isochrone.geoJson;

// Add the GeoJSON directly to the map's data layer
map.data.addGeoJson(isochroneGeoJson);

// Optional: Apply custom styling to the isochrone polygon
map.data.setStyle({
  fillColor: '#4285F4',
  fillOpacity: 0.3,
  strokeColor: '#4285F4',
  strokeWeight: 2
});