How to use Directions response in the Go client for Google Maps Services?

时间:2016-04-04 18:42:14

标签: javascript google-maps-api-3 go

When getting directions using "googlemaps.github.io/maps" in Go, the response is an array of maps.Route . Is there a way to turn this response into something readable by javascript's google.maps.DirectionsRenderer(); , the purpose of this is to get the directions with a go server and distribute that data into multiple platforms. Any suggestions about how to transform this data into a visual map are welcome.

Go code:

package main

import (
    "log"

    "googlemaps.github.io/maps"
    "github.com/kr/pretty"
    "golang.org/x/net/context"
)

func checkForError(err error) {
    if err != nil{
        log.Fatal(err)
    }
}

func main() {
    c, err := maps.NewClient(maps.WithAPIKey("myapikey"))
    checkForError(err)

    r := &maps.DirectionsRequest{
        Origin:      "Hollywood, FL",
        Destination: "Davie, FL",
    }

    resp, _, err := c.Directions(context.Background(), r)
    checkForError(err)

    // render := 

    pretty.Println(resp)
}

Output:

[]maps.Route{
    {
        Summary: "I-95 N and I-595 W",
        Legs:    {
            &maps.Leg{
                Steps: {
                    &maps.Step{
                        HTMLInstructions: "Head <b>east</b> on <b>Hollywood Blvd</b> toward <b>S Dixie Hwy</b>",
                        Distance:         maps.Distance{HumanReadable:"217 ft", Meters:66},
                        Duration:         23000000000,
                        StartLocation:    maps.LatLng{Lat:26.0112983, Lng:-80.1494921},
                        EndLocation:      maps.LatLng{Lat:26.0113264, Lng:-80.14883689999999},
                        Polyline:         maps.Polyline{Points:"siw}CheehN?e@Cq@Ai@"},
                        Steps:            nil,
                        TransitDetails:   (*maps.TransitDetails)(nil),
                        TravelMode:       "DRIVING",
                    },
                    &maps.Step{
                        HTMLInstructions: "Make a <b>U-turn</b> at <b>S 21st Ave</b>",
                        Distance:         maps.Distance{HumanReadable:"0.6 mi", Meters:886},
                        Duration:         90000000000,
                        StartLocation:    maps.LatLng{Lat:26.0113264, Lng:-80.14883689999999},
                        EndLocation:      maps.LatLng{Lat:26.0111533, Lng:-80.1575536},
                        Polyline:         maps.Polyline{Points:"yiw}CfaehNU??h@?r@BpAJVDfFHlOBfDDlCB|C@z@H~F"},
                        Steps:            nil,
                        TransitDetails:   (*maps.TransitDetails)(nil),
                        TravelMode:       "DRIVING",
                    },
                    &maps.Step{
                        HTMLInstructions: "At the traffic circle, take the <b>3rd</b> exit and stay on <b>Hollywood Blvd</b>",
                        Distance:         maps.Distance{HumanReadable:"0.6 mi", Meters:937},
                        Duration:         148000000000,
                        StartLocation:    maps.LatLng{Lat:26.0111533, Lng:-80.1575536},
                        EndLocation:      maps.LatLng{Lat:26.011037, Lng:-80.16587369999999},
                        Polyline:         maps.Polyline{Points:"uhw}CtwfhNGJAB?H?D?J?FAD?DCDCDCDSNUD]N_@XY\\Ub@GRGPIh@Ch@Bd@BJDXN`@DFLVVZ\\T\\P`@JNJHFFHBFBN@F?JBpB?d@@`ABpD@zB?nAB`C@bB@fAAt@AlA"},
                        Steps:            nil,
                        TransitDetails:   (*maps.TransitDetails)(nil),
                        TravelMode:       "DRIVING",
                    },
                    &maps.Step{
                        HTMLInstructions: "Turn <b>right</b> to merge onto <b>I-95 N</b> toward <b>W Palm Beach</b>",
                        Distance:         maps.Distance{HumanReadable:"4.4 mi", Meters:7063},
                        Duration:         277000000000,
                        StartLocation:    maps.LatLng{Lat:26.011037, Lng:-80.16587369999999},
                        EndLocation:      maps.LatLng{Lat:26.0732058, Lng:-80.1671851},
                        Polyline:         maps.Polyline{Points:"_hw}CtkhhNKVCHEJGJCBEBEBIBQDkBB_BJq@DkBR_Ff@_CT[DM@IBGFKJy@FyAFuAB_CD_B@U@cCBE?A?}BByQLqBA{BGuBQeBQyAQo@KoBc@aBa@sA]iHyBa@MoA[wBm@g@K_Ds@cB]aB[eB[qAScBU{BWMCWCsBWg@G}Ks@cHe@aQkA{CUeCOyCSsE[wDW{CSsKu@qAIEA_@COAA?gBM_@Co@E[Ck@CUAs@E]AWA]A]?q@AU?a@?O?Y?kA?s@Bk@@o@Bm@@wEPcKZc@@w@BiAFi@Bq@Dw@Hk@FoBRw@JSD[DmAR}@P}Dt@oCh@yFdAgB\\A@g@HIBA?eCd@cDn@y@Ns@NiB\\_ARu@LaBZyDt@qCh@}Bf@oDx@sBf@}A`@qAZcBb@aBb@qEpA"},
                        Steps:            nil,
                        TransitDetails:   (*maps.TransitDetails)(nil),
                        TravelMode:       "DRIVING",
                    },
                    &maps.Step{
                        HTMLInstructions: "Take exit <b>24</b> for <b>Interstate 595</b> toward <b>Port Everglades</b>/<b>Ft Laud-Hlwd</b>/<b>International Airport</b>",
                        Distance:         maps.Distance{HumanReadable:"0.3 mi", Meters:416},
                        Duration:         17000000000,
                        StartLocation:    maps.LatLng{Lat:26.0732058, Lng:-80.1671851},
                        EndLocation:      maps.LatLng{Lat:26.0768262, Lng:-80.1680758},
                        Polyline:         maps.Polyline{Points:"qlc~C|shhNKIAAAAA?C?E?E?A?MBc@H_@FqB^uAVoAT{@Na@F[Hi@HYFyAXm@L"},
                        Steps:            nil,
                        TransitDetails:   (*maps.TransitDetails)(nil),
                        TravelMode:       "DRIVING",
                    },
                    &maps.Step{
                        HTMLInstructions: "Keep <b>left</b> at the fork, follow signs for <b>I-595 W</b>/<b>I-75</b>/<b>Florida's Turnpike</b> and merge onto <b>I-595 W</b>",
                        Distance:         maps.Distance{HumanReadable:"4.4 mi", Meters:7161},
                        Duration:         255000000000,
                        StartLocation:    maps.LatLng{Lat:26.0768262, Lng:-80.1680758},
                        EndLocation:      maps.LatLng{Lat:26.0947648, Lng:-80.2317655},
                        Polyline:         maps.Polyline{Points:"ecd~CnyhhN[H[D_@Dm@FWBMBU@u@D_@@]@q@@W?]?MAU?[AYAQAe@C_@CWAk@CWAUAUAM?e@?W@Q@WBSBQBI@KBMBUHOFSFWNMHC@A@MHa@ZIFGF]`@IN[f@_@|@IVGT?BEREPCRALAHAF?BAB?FAJ?RAL?F?\\?F?D?N?RDlABn@Bx@@r@@t@@t@AV?h@A`@Ap@Cb@AZ?N?LATCh@CZ?@?@?@@@?@?@@BDFMhBIlAKdBGt@[rEe@zHAFk@vIE`@C`@UlDSbD?FMbECh@A|@C|A?`ACtEDxLD~PBvFDbE@vB?tF?xB@z@ClA?X?@?DG|BGx@IjAGx@Er@MtAMlAMdA[rBQhAUjA[zAWfA[vAYbAc@zAkAtCKXSj@wAhDiC`GiBdE_@|@]x@s@jBs@lBsAnDoAhEw@vCe@xB{@rD[nAsDjS]hBOn@e@~Bc@zBo@rCUpASfAu@fFUjBIj@MhAKp@S|ASzAS`BIv@]nCMbA_@bCQhAAF?FO~@y@zFeAbHyAtJ"},
                        Steps:            nil,
                        TransitDetails:   (*maps.TransitDetails)(nil),
                        TravelMode:       "DRIVING",
                    },
                    &maps.Step{
                        HTMLInstructions: "Take exit <b>6</b> toward <b>FL-817</b>/<b>University Dr</b>",
                        Distance:         maps.Distance{HumanReadable:"0.4 mi", Meters:566},
                        Duration:         20000000000,
                        StartLocation:    maps.LatLng{Lat:26.0947648, Lng:-80.2317655},
                        EndLocation:      maps.LatLng{Lat:26.0963976, Lng:-80.2371076},
                        Polyline:         maps.Polyline{Points:"gsg~CpguhNM\\Ot@qBfKs@tD{@nEKd@S|@Id@Kj@?@ABABMH"},
                        Steps:            nil,
                        TransitDetails:   (*maps.TransitDetails)(nil),
                        TravelMode:       "DRIVING",
                    },
                    &maps.Step{
                        HTMLInstructions: "Merge onto <b>FL-84 W</b>",
                        Distance:         maps.Distance{HumanReadable:"0.8 mi", Meters:1365},
                        Duration:         96000000000,
                        StartLocation:    maps.LatLng{Lat:26.0963976, Lng:-80.2371076},
                        EndLocation:      maps.LatLng{Lat:26.0994367, Lng:-80.25034819999999},
                        Polyline:         maps.Polyline{Points:"o}g~C|hvhNq@~EG`@_@`C_@jCWrBc@dDi@zDyAnJUvAM|@WnBMz@I|@OlAQ|AWrBQ|AKv@SxAWzASpAUnAYpAI`@"},
                        Steps:            nil,
                        TransitDetails:   (*maps.TransitDetails)(nil),
                        TravelMode:       "DRIVING",
                    },
                    &maps.Step{
                        HTMLInstructions: "Turn <b>left</b> onto <b>S University Dr</b>",
                        Distance:         maps.Distance{HumanReadable:"1.7 mi", Meters:2808},
                        Duration:         279000000000,
                        StartLocation:    maps.LatLng{Lat:26.0994367, Lng:-80.25034819999999},
                        EndLocation:      maps.LatLng{Lat:26.07466, Lng:-80.25254079999999},
                        Polyline:         maps.Polyline{Points:"oph~Ct{xhNKf@t@ND@h@Hd@JRBD@bBZfANl@Jx@JtALr@HjBLd@@pBJR@~BDT?z@AhBAt@A`CCfAArAC~@AfAAfBETA~GIlBCvHK|EGbFIr@AvDGR?jB?dC?N?pCDpCLl@FlBRPBl@Fh@HxCf@hGpAn@NzB`@VBx@LtAN~@Ht@FrAJ"},
                        Steps:            nil,
                        TransitDetails:   (*maps.TransitDetails)(nil),
                        TravelMode:       "DRIVING",
                    },
                    &maps.Step{
                        HTMLInstructions: "Make a <b>U-turn</b><div style=\"font-size:0.9em\">Destination will be on the left</div>",
                        Distance:         maps.Distance{HumanReadable:"0.2 mi", Meters:242},
                        Duration:         38000000000,
                        StartLocation:    maps.LatLng{Lat:26.07466, Lng:-80.25254079999999},
                        EndLocation:      maps.LatLng{Lat:26.0765145, Lng:-80.25211829999999},
                        Polyline:         maps.Polyline{Points:"suc~CjiyhN@i@g@EwAIC?sAMYCm@Iy@M[E@T"},
                        Steps:            nil,
                        TransitDetails:   (*maps.TransitDetails)(nil),
                        TravelMode:       "DRIVING",
                    },
                },
                Distance:          maps.Distance{HumanReadable:"13.4 mi", Meters:21510},
                Duration:          1243000000000,
                DurationInTraffic: 0,
                ArrivalTime:       time.Time{},
                DepartureTime:     time.Time{},
                StartLocation:     maps.LatLng{Lat:26.0112983, Lng:-80.1494921},
                EndLocation:       maps.LatLng{Lat:26.0765145, Lng:-80.25211829999999},
                StartAddress:      "Hollywood, FL, USA",
                EndAddress:        "Davie, FL, USA",
            },
        },
        WaypointOrder:    {},
        OverviewPolyline: maps.Polyline{Points:"siw}CheehNCwAAi@U??|ABpAJVNtVHtHDxEH~FGJAL?PEXGJSNUD]N_@XY\\Ub@Od@Ih@Ch@Fp@Tz@R^t@p@\\P`@JXRJPDVDdFH~NBjDCbC]x@YNQDkBBqCPgNvAWDSRsCNuEH_GF_CByQLqBA{BGuBQ_Ec@_Do@uD_AkIgCgEiAgE_AeEy@wDo@_Fm@yC_@eM{@eZqBoSuAqZsBsE[sCMeCEaB?{EHmTp@}ETkGp@{Dp@oTbEmMdCaIzAkI~AmH`BgKhCsHtBMKMAy@L{M`C}Dx@aCVyBLeCByAEoBKoBI}ABgAL_AX}@b@{@n@e@h@e@v@i@tAGXQfACTEfBDvBJrEAxCG`CInBBHDFMhBUrDkAlQkAhQSjDQlFIrLJx^HzLBbPChBGbCQdCMlB[bDi@xDg@tCs@bDu@zCc@zAkAtC_@dAkJnTyEbMgC`JaBlH[nAsDjSm@xCoC`NiAnH_@vCaAtHiAlJs@|EiAzH_DxS]rAeD|PeBxIOt@MHq@~Eg@bDeC`RuCtRy@dHiAbJk@lDo@`DUhAz@PhBZjDj@fBVhCVpCNdCLtCD|IIbHIjOUlX_@jEGpF?`DDpCLl@F~BVvAPxCf@hGpAjDp@pAPtCXhCR@i@g@E{AImBQgBW[E@T"},
        Bounds:           maps.LatLngBounds{
            NorthEast: maps.LatLng{Lat:26.0994988, Lng:-80.14883689999999},
            SouthWest: maps.LatLng{Lat:26.0110222, Lng:-80.25254079999999},
        },
        Copyrights: "Map data ©2016 Google",
        Warnings:   {},
    },
}

1 个答案:

答案 0 :(得分:0)

该JSON输出是一个DirectionsResult对象,它通过DirectionsRendererOptions直接传递给您所询问的DirectionsRenderer()构造函数。

如果您正试图将其编组到Go中,则可以使用相同的结构来为寻找DirectionsResult对象的消费者进行编组。