Skip to content

Instantly share code, notes, and snippets.

@DMCApps
Last active January 29, 2017 21:42
Show Gist options
  • Save DMCApps/47bbeb1a58df99313fdfac6639df6181 to your computer and use it in GitHub Desktop.
Save DMCApps/47bbeb1a58df99313fdfac6639df6181 to your computer and use it in GitHub Desktop.
This is a helper class for generating a URL to grab a static map from Google's Static Map API.
import android.location.Location;
import android.support.annotation.NonNull;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
/**
* Created by Daniel Carmo on 2016-12-30.
* Copyright © 2016 DMCApps Inc. All rights reserved.
*
* This is a helper class for generating a URL to grab a static map from Google's Static Map API.
*/
public final class StaticMapUrlBuilder {
public enum Color {
BLACK("black"),
BROWN("brown"),
GREEN("green"),
PURPLE("purple"),
YELLOW("yellow"),
BLUE("blue"),
GRAY("gray"),
ORANGE("orange"),
RED("red"),
WHITE("white");
private String color;
Color(String color) {
this.color = color;
}
String toApiString() {
return color;
}
}
private static final String TAG = StaticMapUrlBuilder.class.getSimpleName();
private static final String BASE_URL = "https://maps.googleapis.com/maps/api/staticmap?size=%dx%d&key=%s";
private final String apiKey;
private final int width;
private final int height;
private Integer scale = null;
private String centerLocation = null;
private Double centerLat = null;
private Double centerLng = null;
private Integer zoom = null;
private List<MarkerLocation> markerLocations;
private List<Path> paths;
public static class MarkerLocation {
public enum MarkerSize {
TINY("tiny"),
MID("mid"),
SMALL("small");
private String color;
MarkerSize(String color) {
this.color = color;
}
String toApiString() {
return color;
}
}
Color color;
MarkerSize size;
Character label;
String location;
Double lat;
Double lng;
private MarkerLocation(Color color, MarkerSize size) {
this.color = color;
this.size = size;
}
public MarkerLocation(Color color, MarkerSize size, @NonNull String location) {
this(color, size);
this.location = location;
}
public MarkerLocation(Color color, MarkerSize size, @NonNull String location, Character label) {
this(color, size, location);
this.label = label;
}
public MarkerLocation(Color color, MarkerSize size, Location location) {
this(color, size, location.getLatitude(), location.getLongitude(), null);
}
public MarkerLocation(Color color, MarkerSize size, Location location, Character label) {
this(color, size, location.getLatitude(), location.getLongitude(), label);
}
public MarkerLocation(Color color, MarkerSize size, double lat, double lng) {
this(color, size, lat, lng, null);
}
public MarkerLocation(Color color, MarkerSize size, double lat, double lng, Character label) {
this(color, size);
this.lat = lat;
this.lng = lng;
this.label = label;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(String.format(Locale.ENGLISH, "color:%s", color.toApiString()));
sb.append(String.format(Locale.ENGLISH, "%%7Csize:%s", size.toApiString()));
if (label != null) {
sb.append(String.format(Locale.ENGLISH, "%%7Clabel:%s", label.toString().toUpperCase()));
}
if (lat != null && lng != null) {
sb.append(String.format(Locale.ENGLISH, "%%7C%f,%f", lat, lng));
}
else if (location != null) {
sb.append(String.format(Locale.ENGLISH, "%%7C%s", location.replace(" ", "+")));
}
return sb.toString();
}
}
public static class Path {
private Color color;
private Integer weight;
private List<Location> pointsOnPath;
private String encodedPoints;
public Path(Color color, Integer weight, List<Location> pointsOnPath) {
this.color = color;
this.weight = weight;
this.pointsOnPath = pointsOnPath;
}
public Path(Color color, Integer weight, String encodedPoints) {
this.color = color;
this.weight = weight;
this.encodedPoints = encodedPoints;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
if (weight != null) {
sb.append(String.format(Locale.ENGLISH, "weight:%d%%7C", weight));
}
sb.append(String.format(Locale.ENGLISH, "color:%s", color.toApiString()));
if (pointsOnPath != null) {
for (Location location : pointsOnPath) {
sb.append("%7C");
sb.append(String.format(Locale.ENGLISH, "%f,%f", location.getLatitude(), location.getLongitude()));
}
}
if (encodedPoints != null && !encodedPoints.isEmpty()) {
sb.append(String.format(Locale.ENGLISH, "%%7Cenc:%s", encodedPoints));
}
return sb.toString();
}
}
public StaticMapUrlBuilder(int width, int height, @NonNull String apiKey) {
this.width = width;
this.height = height;
this.apiKey = apiKey;
}
public StaticMapUrlBuilder center(@NonNull String location) {
this.centerLocation = location;
return this;
}
public StaticMapUrlBuilder center(double lat, double lng) {
this.centerLat = lat;
this.centerLng = lng;
return this;
}
public StaticMapUrlBuilder scale(int scale) {
this.scale = Math.min(scale, 2);
return this;
}
public StaticMapUrlBuilder zoom(int zoom) {
this.zoom = Math.min(zoom, 20);
return this;
}
public StaticMapUrlBuilder addMarkerLocation(@NonNull MarkerLocation markerLocation) {
if (markerLocations == null) {
markerLocations = new ArrayList<>();
}
markerLocations.add(markerLocation);
return this;
}
public StaticMapUrlBuilder addPath(@NonNull Path path) {
if (paths == null) {
paths = new ArrayList<>();
}
paths.add(path);
return this;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(String.format(Locale.ENGLISH, BASE_URL, width, height, apiKey));
if (scale != null) {
sb.append(String.format(Locale.ENGLISH, "&scale=%d", scale));
}
if (centerLat != null && centerLng != null) {
sb.append(String.format(Locale.ENGLISH, "&center=%f,%f", centerLat, centerLng));
}
else if (centerLocation != null) {
sb.append(String.format(Locale.ENGLISH, "&center=%s", centerLocation.replace(" ", "")));
}
if (zoom != null) {
sb.append(String.format(Locale.ENGLISH, "&zoom=%d", zoom));
}
if (markerLocations != null) {
for (MarkerLocation markerLocation: markerLocations) {
sb.append(String.format(Locale.ENGLISH, "&markers=%s", markerLocation.toString()));
}
}
if (paths != null) {
for (Path path : paths) {
sb.append(String.format(Locale.ENGLISH, "&path=%s", path.toString()));
}
}
return sb.toString();
}
public URL toUrl() throws MalformedURLException {
return new URL(this.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment