Skip to content

Static Maps: allow path-only StaticMapRequest without Center/Zoom #318

Open

Description

@hidingmyname

Thanks for stopping by to let us know something could be better!

Is your feature request related to a problem? Please describe.

Location (googlemaps/google-maps-services-go @ default branch 8f18218):

func (c *Client) StaticMap(ctx context.Context, r *StaticMapRequest) (image.Image, error) {
	if len(r.Markers) == 0 && r.Center == "" && r.Zoom == 0 {
		return nil, errors.New("maps: Center & Zoom required if Markers empty")
	}

Problem

I'd like to render a route as a static image by passing only Paths (no markers) and letting the Static Maps API choose the viewport. Client.StaticMap rejects this locally, before any request is sent, because its validation only exempts Markers from the center/zoom requirement.

This is a feature request. I have not verified the backend response for a path-only request myself, so I'd appreciate maintainers confirming the intended service semantics.

Reproduction

r := &maps.StaticMapRequest{
	Size: "600x300",
	Paths: []maps.Path{{
		Color: "green", Weight: 3, Geodesic: true,
		Location: []maps.LatLng{{Lat: -37.8136, Lng: 144.9631}, {Lat: -33.8688, Lng: 151.2093}},
	}},
}
_, err := c.StaticMap(ctx, r)
// err: maps: Center & Zoom required if Markers empty

Observed with a test against a local mock server:

--- FAIL: TestStaticMapWithPathNoCenterNoZoom (0.00s)
    staticmap_path_validation_test.go:33: StaticMap with Path and no Center/Zoom should not return error, got: maps: Center & Zoom required if Markers empty

Environment:

  • Commit: 8f182180c61d5b26d547705e2addf1dd3de7ffbd (master)
  • Toolchain: go1.14.15

Describe the solution you'd like

Treat a non-empty Paths (and possibly Visible, if the service supports it for implicit positioning) like Markers in this check, so path-only requests are sent to the API. Requests with no markers, no paths (and no visible locations) and no center/zoom would still be rejected locally as today, and Size remains required.

Describe alternatives you've considered

Setting Center or Zoom just to pass validation, which defeats automatic fitting of the path; or computing a center/zoom for the path bounds manually.

Additional context

Already implemented in googlemaps/google-maps-services-java

PR: googlemaps/google-maps-services-java#650 — "fixed: Added path exception when there is no center or no zoom" (merged 2020-01-17T18:22:18Z, merge commit 47acc920ca6). The Java client's validateRequest accepts a request that has path without center/zoom.

How google-maps-services-java fixed it:

--- a/src/main/java/com/google/maps/StaticMapsRequest.java
+++ b/src/main/java/com/google/maps/StaticMapsRequest.java
@@ -37,9 +37,10 @@ public StaticMapsRequest(GeoApiContext context) {
...
   protected void validateRequest() {
     if (!((params().containsKey("center") && params().containsKey("zoom"))
-        || params().containsKey("markers"))) {
+        || params().containsKey("markers")
+        || params().containsKey("path"))) {
       throw new IllegalArgumentException(
-          "Request must contain 'center' and 'zoom' if 'markers' isn't present.");
+          "Request must contain 'center' and 'zoom' if 'markers' or 'path' aren't present.");

Suggested fix

Skip the center/zoom requirement in StaticMap when Paths is non-empty and update the error text, as the Java client did in validateRequest. Requests with no markers, no paths and no center/zoom are still rejected, and Size is still required:

diff --git a/staticmap.go b/staticmap.go
--- a/staticmap.go
+++ b/staticmap.go
@@ -327,8 +327,8 @@ func (r *StaticMapRequest) params() url.Values {
 
 // StaticMap makes a StaticMap API request.
 func (c *Client) StaticMap(ctx context.Context, r *StaticMapRequest) (image.Image, error) {
-	if len(r.Markers) == 0 && r.Center == "" && r.Zoom == 0 {
-		return nil, errors.New("maps: Center & Zoom required if Markers empty")
+	if len(r.Markers) == 0 && len(r.Paths) == 0 && r.Center == "" && r.Zoom == 0 {
+		return nil, errors.New("maps: Center & Zoom required if Markers and Paths empty")
 	}
 	if r.Size == "" {
 		return nil, errors.New("maps: Size empty")

Verified on 8f18218 (go1.14.15 linux/amd64): the test above fails without this patch and passes with it. A second test, which checks that a request with no markers, paths, center or zoom is still rejected, passes with and without the patch. No other test results change (go test ./...).

I can submit a PR for this.

Checked: searches for StaticMap path center zoom, "Center & Zoom required", staticmap visible found no existing issue or PR.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions