How can I get Longitudes and Latitudes (Waypoints) from Google Maps' Direction API?

Viewed 29

I followed the tutorial for Google Maps Go Client https://github.com/googlemaps/google-maps-services-go and eventually got it work.

package main

import (
    "context"
    "log"

    "github.com/kr/pretty"
    "googlemaps.github.io/maps"
)

const GoogleMapsApiKey string = "<my apikey>"

func main() {
    c, err := maps.NewClient(maps.WithAPIKey(GoogleMapsApiKey))
    if err != nil {
        log.Fatalf("fatal error: %s", err)
    }
    r := &maps.DirectionsRequest{
        Origin:      "Sydney",
        Destination: "Perth",
        Waypoints:   make([]string, 0),
    }
    route, waypoints, err := c.Directions(context.Background(), r)
    if err != nil {
        log.Fatalf("fatal error: %s", err)
    }

    pretty.Println(waypoints)
    pretty.Println(route)

}

I browsed through all the return values and viewed it in debugger:

auspicious:

// GeocodedWaypoint represents the geocoded point for origin, supplied waypoints, or
// destination for a requested direction request.
type GeocodedWaypoint struct {
    // GeocoderStatus indicates the status code resulting from the geocoding operation.
    // This field may contain the following values.
    GeocoderStatus string `json:"geocoder_status"`
    // PartialMatch indicates that the geocoder did not return an exact match for the
    // original request, though it was able to match part of the requested address.
    PartialMatch bool `json:"partial_match"`
    // PlaceID is a unique identifier that can be used with other Google APIs.
    PlaceID string `json:"place_id"`
    // Types indicates the address type of the geocoding result used for calculating
    // directions.
    Types []string `json:"types"`
}

(https://github.com/googlemaps/google-maps-services-go/blob/master/directions.go#L181) or

// Route represents a single route between an origin and a destination.
type Route struct {
    // Summary contains a short textual description for the route, suitable for
    // naming and disambiguating the route from alternatives.
    Summary string `json:"summary"`

    // Legs contains information about a leg of the route, between two locations within
    // the given route. A separate leg will be present for each waypoint or destination
    // specified. A route with no waypoints will contain exactly one leg within the legs
    // array.
    Legs []*Leg `json:"legs"`

    // WaypointOrder contains an array indicating the order of any waypoints in the
    // calculated route.
    WaypointOrder []int `json:"waypoint_order"`

    // OverviewPolyline contains an approximate (smoothed) path of the resulting
    // directions.
    OverviewPolyline Polyline `json:"overview_polyline"`

    // Bounds contains the viewport bounding box of the overview polyline.
    Bounds LatLngBounds `json:"bounds"`

    // Copyrights contains the copyrights text to be displayed for this route. You
    // must handle and display this information yourself.
    Copyrights string `json:"copyrights"`

    // Warnings contains an array of warnings to be displayed when showing these
    // directions. You must handle and display these warnings yourself.
    Warnings []string `json:"warnings"`

    // Fare contains the total fare (that is, the total ticket costs) on this route.
    // This property is only returned for transit requests and only for routes where
    // fare information is available for all transit legs.
    *Fare `json:"fare"`
}

(https://github.com/googlemaps/google-maps-services-go/blob/master/directions.go#L196)

However I couldn't find any array of my complete track (Latitudes & Longitudes in a decent resolution). Is it possible to do so with the APIs or does Google not allow this?

I would also appreciate a workaround with receiving KMZ or KML files. This can be easiliy parsed with Golang's json library I think.

0 Answers
Related