mapsengine.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package main
  5. import (
  6. "fmt"
  7. "log"
  8. "net/http"
  9. "os"
  10. "strings"
  11. "time"
  12. mapsengine "google.golang.org/api/mapsengine/v1"
  13. )
  14. func init() {
  15. scopes := []string{
  16. mapsengine.MapsengineScope,
  17. mapsengine.MapsengineReadonlyScope,
  18. }
  19. registerDemo("mapsengine", strings.Join(scopes, " "), mapsengineMain)
  20. }
  21. func showMapFeatures(svc *mapsengine.Service, id string) {
  22. r, err := svc.Tables.Get(id).Version("published").Do()
  23. if err != nil {
  24. log.Fatalf("Unable to get map %v table: %v", id, err)
  25. }
  26. fmt.Printf("Map ID: %v, Name: %q, Description: %q\n", id, r.Name, r.Description)
  27. pageToken := ""
  28. for {
  29. time.Sleep(1 * time.Second) // Don't violate free rate limit
  30. // Read the location of every Feature in a Table.
  31. req := svc.Tables.Features.List(id).MaxResults(500).Version("published")
  32. if pageToken != "" {
  33. req.PageToken(pageToken)
  34. }
  35. r, err := req.Do()
  36. if err != nil {
  37. log.Fatalf("Unable to list table features: %v", err)
  38. }
  39. for _, f := range r.Features {
  40. if v, ok := f.Geometry.GeometryCollection(); ok {
  41. fmt.Printf("%v: %v\n", f.Geometry.Type(), v)
  42. } else if v, ok := f.Geometry.LineString(); ok {
  43. fmt.Printf("%v: %v\n", f.Geometry.Type(), v)
  44. } else if v, ok := f.Geometry.MultiLineString(); ok {
  45. fmt.Printf("%v: %v\n", f.Geometry.Type(), v)
  46. } else if v, ok := f.Geometry.MultiPoint(); ok {
  47. fmt.Printf("%v: %v\n", f.Geometry.Type(), v)
  48. } else if v, ok := f.Geometry.MultiPolygon(); ok {
  49. fmt.Printf("%v: %v\n", f.Geometry.Type(), v)
  50. } else if v, ok := f.Geometry.Point(); ok {
  51. fmt.Printf("%v: %v\n", f.Geometry.Type(), v)
  52. } else if v, ok := f.Geometry.Polygon(); ok {
  53. fmt.Printf("%v: %v\n", f.Geometry.Type(), v)
  54. } else {
  55. log.Fatalf("Unknown GeoJsonGeometry type %q", f.Geometry.Type())
  56. }
  57. }
  58. if r.NextPageToken == "" {
  59. break
  60. }
  61. pageToken = r.NextPageToken
  62. }
  63. }
  64. // mapsengineMain is an example that demonstrates calling the Mapsengine API.
  65. // Please see https://developers.google.com/maps-engine/documentation/hello-world#go
  66. // for more information.
  67. //
  68. // Example usage:
  69. // go build -o go-api-demo *.go
  70. // go-api-demo -clientid="my-clientid" -secret="my-secret" mapsengine
  71. func mapsengineMain(client *http.Client, argv []string) {
  72. if len(argv) != 0 {
  73. fmt.Fprintln(os.Stderr, "Usage: mapsengine")
  74. return
  75. }
  76. svc, err := mapsengine.New(client)
  77. if err != nil {
  78. log.Fatalf("Unable to create Mapsengine service: %v", err)
  79. }
  80. showMapFeatures(svc, "14137585153106784136-16071188762309719429")
  81. showMapFeatures(svc, "12421761926155747447-06672618218968397709")
  82. }