Returns a map image in response to an HTTP request. You can specify the location for requesting the map and the map image size, as well as add ground overlays such as markers, polylines, and polygons.
| Figure 1 Marker | Figure 2 Polygon | Figure 3 Polyline |
You can create a map image based on the URL parameter in the HTTP request, and directly embed the map image to the web page, without using the JavaScript or dynamic page loading.
Protocol | HTTPS GET |
|---|---|
Direction | Your server -> Map Kit server |
URL | https://mapapi.cloud.huawei.com/mapApi/v1/mapService/getStaticMap |
Data Format | Response: Content-Type: image/png |
At least one of Authorization in Request Header and key in Query String need to be set.
Request Header
Parameter | Mandatory | Type | Description |
|---|---|---|---|
Content-Type | Yes | application/json | Request data format. |
Authorization | No | String | Authentication mode. Access token (recommended) authentication and API key authentication are supported. NOTE: For access token authentication:
For API key authentication:
|
Query String
Parameter | Mandatory | Type | Description |
|---|---|---|---|
key | No | String | Authentication key. For details about how to obtain one, please refer to Obtaining the API Key. NOTE:
|
width | Yes | Integer | Image width. Value range: (0, 1024]. |
height | Yes | Integer | Image height. Value range: (0, 1024]. |
location | No | String | Address information. The value can be a pair of latitude and longitude or a specific address. The latitude and longitude are in the Latitude,Longitude format, for example, 41.43206,-81.38992. If markers and path are not set, this parameter is mandatory. |
zoom | No | int | Zoom level. If markers and path are not set, this parameter is mandatory. The value ranges from 1 to 18. |
scale | No | Integer | Map scale. The options are 1 and 2. The default value is 1. |
pattern | No | String | Map image format. Currently, only PNG images are supported. |
mapType | No | String | Map type. Currently, only the standard map type roadmap is supported. |
language | No | String | Map display language. The value is a language code complying with ISO 639-2 or BCP47. BCP47 is recommended. For details about the value range, please refer to Supported Languages. |
markers | No | String | Marker description. The value can be marker styles, longitudes and latitudes, or addresses. You can enter multiple groups of location information and separate them using vertical bars (|). The following is an example: markers=Marker style|{Latitude 1,Longitude 1}|{Latitude 2,Longitude 2}|Address If the marker style is not specified, markerStyles is used as the default style. NOTE: You must set at least one of the following groups of parameters: markers/path and location/zoom. If both groups are set, the location/zoom group takes precedence. |
markerStyles | No | String | Marker style description. The attributes that can be set include the following:
Example: size:mid|color:black|label:C|icon:https%253A%252F%252Fwww-file.huawei.com%252F-%252Fmedia%252Fcorporate%252Fimages%252Fhome%252Flogo%252Fhuawei_logo.png NOTE:
|
path | No | String | Path description. The value can be path styles, longitudes and latitudes, or addresses. You can enter multiple groups of location information and separate them using vertical bars (|). The following is an example: path=Path style|{Latitude 1,Longitude 1}|{Latitude 2,Longitude 2}|Address If the path style is not specified, pathStyles is used as the default style. NOTE: You must set at least one of the following groups of parameters: markers/path and location/zoom. If both groups are set, the location/zoom group takes precedence. |
pathStyles | No | String | Path style description. The attributes that can be set include the following:
If fillcolor is set, the path style is polygon. Otherwise, the path style is polyline. Example: color:0x0000ff80|weight:1|fillcolor:0x0000ff80 |
logo | No | String | Logo style description. The attributes that can be set are logoAnchor and visible.
Example: logoAnchor:bottomleft|visible:true |
signature | No | String | Message signature generated using the signature key. You are advised to use the signature with the API key to ensure API security. NOTE: For details about how to obtain the signature key, please refer to Obtaining the Signature Key. |
GET https://mapapi.cloud.huawei.com/mapApi/v1/mapService/getStaticMap? width=1024& height=512& location=41.43206,-81.38992& scale=1& zoom=5& language=en& logo=logoAnchor:bottomleft& markers={41.43206,-81.38992}& markers={43.43206,-85.38992}& markerStyles=size:tiny|color:blue|label:p& path={41.43206,-81.38992}|{43.43206,-85.38992}& path=fillcolor:red|{41.43206,-81.38992}|{43.43206,-85.38992}|{39.43206,-83.38992}& pathStyles=weight:1|color:0x0000ff80|fillcolor:0x0000ff80& key=xxx& signature=xxx
After escape:
GET https://mapapi.cloud.huawei.com/mapApi/v1/mapService/getStaticMap? width=1024& height=512& location=41.43206,-81.38992& scale=1& zoom=5& language=en& logo=logoAnchor:bottomleft& markers=%7B41.43206,-81.38992%7D& markers=%7B43.43206,-85.38992%7D& markerStyles=size:tiny%7Ccolor:blue%7Clabel:p& path=%7B41.43206,-81.38992%7D%7C%7B43.43206,-85.38992%7D& path=fillcolor:red%7C%7B41.43206,-81.38992%7D%7C%7B43.43206,-85.38992%7D%7C%7B39.43206,-83.38992%7D& pathStyles=weight:1%7Ccolor:0x0000ff80%7Cfillcolor:0x0000ff80& key=xxx& signature=xxx
When using the sample code, you need to escape the vertical bar (|) with %7C, left curly brace ({) with %7B, and right curly brace (}) with %7D.
When the status code is 200:
Response Header
Parameter | Mandatory | Type | Description |
|---|---|---|---|
Content-Type | Yes | image/png | Response data format. |
Response Body
Parameter | Type | Description |
|---|---|---|
Body | String | Static map image. |
Access token authentication public class GetStaticMapService { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("https://mapapi.cloud.huawei.com/mapApi/v1/mapService/getStaticMap?" + // Set the width of the returned map image to 1024. "width=1024&" + // Set the height of the returned map image to 512. "height=512&" + // Set the address information. (Latitude: 41.43206; Longitude: -81.38992) "location=41.43206%2C-81.38992&" + // Set the map scale to 1. "scale=1&" + // Set the zoom level to 5. "zoom=5&" + // Set the map image format to PNG. "pattern=PNG&" + // Set the map display language to English. "language=en&" + // Set the logo style description. "logo=logoAnchor%3Abottomleft&" + // Set the maker description information. (Latitude: 41.43206; Longitude: -81.38992) "markers=%7B41.43206%2C-81.38992%7D&" + // Set the maker description information. (Latitude: 43.43206; Longitude: -85.38992) "markers=%7B43.43206%2C-85.38992%7D&" + // Set the maker style description. "markerStyles=size%3Atiny%7Ccolor%3Ablue%7Clabel%3Amjy&" + // Set the path description information. {Latitude, Longitude}: {41.43206, -81.38992}|{43.43206, -85.38992} "path=%7B41.43206%2C-81.38992%7D%7C%7B43.43206%2C-85.38992%7D&" + // Set the path description, and path style to red. {Latitude, Longitude}: {41.43206, -81.38992}|{43.43206, -85.38992}|{39.43206, -83.38992} "path=fillcolor%3Ared%7C%7B41.43206%2C-81.38992%7D%7C%7B43.43206%2C-85.38992%7D%7C%7B39.43206%2C-83.38992%7D&" + // Set the path style description. "pathStyles=weight%3A1%7Ccolor%3A0x0000ff80%7Cfillcolor%3A0x0000ff80&") .addHeader("Authorization", "Bearer " + AT) .get() .build(); Response response = client.newCall(request).execute(); }
API key authentication public class GetStaticMapService { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("https://mapapi.cloud.huawei.com/mapApi/v1/mapService/getStaticMap?" + // Set the width of the returned map image to 1024. "width=1024&" + // Set the height of the returned map image to 512. "height=512&" + // Set the address information. (Latitude: 41.43206; Longitude: -81.38992) "location=41.43206%2C-81.38992&" + // Set the map scale to 1. "scale=1&" + // Set the zoom level to 5. "zoom=5&" + // Set the map image format to PNG. "pattern=PNG&" + // Set the map display language to English. "language=en&" + // Set the logo style description. "logo=logoAnchor%3Abottomleft&" + // Set the maker description information. (Latitude: 41.43206; Longitude: -81.38992) "markers=%7B41.43206%2C-81.38992%7D&" + // Set the maker description information. (Latitude: 43.43206; Longitude: -85.38992) "markers=%7B43.43206%2C-85.38992%7D&" + // Set the maker style description. "markerStyles=size%3Atiny%7Ccolor%3Ablue%7Clabel%3Amjy&" + // Set the path description information. {Latitude, Longitude}: {41.43206, -81.38992}|{43.43206, -85.38992} "path=%7B41.43206%2C-81.38992%7D%7C%7B43.43206%2C-85.38992%7D&" + // Set the path description, and path style to red. {Latitude, Longitude}: {41.43206, -81.38992}|{43.43206, -85.38992}|{39.43206, -83.38992} "path=fillcolor%3Ared%7C%7B41.43206%2C-81.38992%7D%7C%7B43.43206%2C-85.38992%7D%7C%7B39.43206%2C-83.38992%7D&" + // Set the path style description. "pathStyles=weight%3A1%7Ccolor%3A0x0000ff80%7Cfillcolor%3A0x0000ff80&" + "key=xxxx") .get() .build(); Response response = client.newCall(request).execute(); }
Access token authentication import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLDecoder; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Base64; import java.util.List; import java.util.SortedMap; import java.util.TreeMap; import javax.crypto.Mac; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import okhttp3.HttpUrl; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class Demo { public static void main(String[] args) throws Exception { new Demo().getStaticMapWithUrlSignature(); } private void getStaticMapWithUrlSignature() throws NoSuchAlgorithmException, InvalidKeyException, IOException { OkHttpClient client = new OkHttpClient(); HttpUrl httpUrl = new HttpUrl.Builder().scheme("https") .host("mapapi.cloud.huawei.com") .encodedPath("/mapApi/v1/mapService/getStaticMap") // Set the width of the returned map image to 1024. .addQueryParameter("width", "1024") // Set the height of the returned map image to 512. .addQueryParameter("height", "512") // Set the address information. (Latitude: 41.43206; Longitude: -81.38992) .addQueryParameter("location", "41.43206,-81.38992") // Set the map scale to 1. .addQueryParameter("scale", "1") // Set the zoom level to 5. .addQueryParameter("zoom", "5") // Set the map display language to English. .addQueryParameter("language", "en") // Set the logo style description. .addQueryParameter("logo", "logoAnchor:bottomleft") // Set the maker description information. (Latitude: 41.43206; Longitude: -81.38992) .addQueryParameter("markers", "{41.43206,-81.38992}") // Set the maker description information. (Latitude: 43.43206; Longitude: -85.38992) .addQueryParameter("markers", "{43.43206,-85.38992}") // Set the maker style description. .addQueryParameter("markerStyles", "size:tiny|color:blue|label:p") // Set the path description information. {Latitude, Longitude}: {41.43206, -81.38992}|{43.43206, -85.38992} .addQueryParameter("path", "{41.43206,-81.38992}|{43.43206,-85.38992}") // Set the path description, and path style to red. {Latitude, Longitude}: {41.43206, -81.38992}|{43.43206, -85.38992}|{39.43206, -83.38992} .addQueryParameter("path", "fillcolor:red|{41.43206,-81.38992}|{43.43206,-85.38992}|{39.43206,-83.38992}") // Set the path style description. .addQueryParameter("pathStyles", "weight:1|color:0x0000ff80|fillcolor:0x0000ff80") .build(); String urlToSign = httpUrl.toString(); // Signature key. String secret = "xxx"; String signature = sign(urlToSign, secret); String url = urlToSign + "&signature=" + signature; Request request = new Request.Builder().url(ROOT_URL).addHeader("Authorization", "Bearer " + AT) .get() .build(); Response response = client.newCall(request).execute(); // Handle the response. } private String sign(String urlToSign, String secret) throws MalformedURLException, NoSuchAlgorithmException, InvalidKeyException { URL url = new URL(urlToSign); String canonicalUrl = url.getPath() + "?" + new CanonicalQueryString(url.getQuery()).toString(); SecretKey secretKey = new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"); Mac mac = Mac.getInstance("HmacSHA256"); mac.init(secretKey); byte[] sigBytes = mac.doFinal(canonicalUrl.getBytes()); String signature = Base64.getEncoder().encodeToString(sigBytes); signature = signature.replace('+', '-'); signature = signature.replace('/', '_'); return signature; } static class CanonicalQueryString { private static final int INIT_BUFFER_SIZE = 128; /** * The constant PARAMETER_EQ. */ public static final String PARAMETER_EQ = "="; /** * The constant PARAMETER_SEP. */ public static final String PARAMETER_SEP = "&"; private SortedMap<String, List<String>> params = new TreeMap<>(); /** * Instantiates a new Canonical query string. * * @param queryString the query string */ public CanonicalQueryString(String queryString) { if (queryString == null || queryString.length() == 0) { return; } for (String pair : queryString.split(PARAMETER_SEP)) { int idx = pair.indexOf(PARAMETER_EQ); if (idx != -1) { String value = pair.substring(idx + 1); if (value == null || value.length() == 0) { continue; } String paramKey = pair.substring(0, idx); List<String> values = params.computeIfAbsent(paramKey, key -> new ArrayList<>()); try { values.add(URLDecoder.decode(value, "UTF-8")); } catch (UnsupportedEncodingException e) { // ignore } } } } @Override public String toString() { StringBuilder sb = new StringBuilder(INIT_BUFFER_SIZE); for (SortedMap.Entry<String, List<String>> item : params.entrySet()) { String key = item.getKey(); for (String value : item.getValue()) { if (sb.length() > 0) { sb.append(PARAMETER_SEP); } sb.append(key).append(PARAMETER_EQ).append(value); } } return sb.toString(); } } }
API key authentication import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLDecoder; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Base64; import java.util.List; import java.util.SortedMap; import java.util.TreeMap; import javax.crypto.Mac; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import okhttp3.HttpUrl; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class Demo { public static void main(String[] args) throws Exception { new Demo().getStaticMapWithUrlSignature(); } private void getStaticMapWithUrlSignature() throws NoSuchAlgorithmException, InvalidKeyException, IOException { OkHttpClient client = new OkHttpClient(); HttpUrl httpUrl = new HttpUrl.Builder().scheme("https") .host("mapapi.cloud.huawei.com") .encodedPath("/mapApi/v1/mapService/getStaticMap") // Set the width of the returned map image to 1024. .addQueryParameter("width", "1024") // Set the height of the returned map image to 512. .addQueryParameter("height", "512") // Set the address information. (Latitude: 41.43206; Longitude: -81.38992) .addQueryParameter("location", "41.43206,-81.38992") // Set the map scale to 1. .addQueryParameter("scale", "1") // Set the zoom level to 5. .addQueryParameter("zoom", "5") // Set the map display language to English. .addQueryParameter("language", "en") // Set the logo style description. .addQueryParameter("logo", "logoAnchor:bottomleft") // Set the maker description information. (Latitude: 41.43206; Longitude: -81.38992) .addQueryParameter("markers", "{41.43206,-81.38992}") // Set the maker description information. (Latitude: 43.43206; Longitude: -85.38992) .addQueryParameter("markers", "{43.43206,-85.38992}") // Set the maker style description. .addQueryParameter("markerStyles", "size:tiny|color:blue|label:p") // Set the path description information. {Latitude, Longitude}: {41.43206, -81.38992}|{43.43206, -85.38992} .addQueryParameter("path", "{41.43206,-81.38992}|{43.43206,-85.38992}") // Set the path description, and path style to red. {Latitude, Longitude}: {41.43206, -81.38992}|{43.43206, -85.38992}|{39.43206, -83.38992} .addQueryParameter("path", "fillcolor:red|{41.43206,-81.38992}|{43.43206,-85.38992}|{39.43206,-83.38992}") // Set the path style description. .addQueryParameter("pathStyles", "weight:1|color:0x0000ff80|fillcolor:0x0000ff80") .build(); String urlToSign = httpUrl.toString(); // Signature key. String secret = "xxx"; String signature = sign(urlToSign, secret); // API key encoded using URLEncoder.encode. String encodedApiKey = "xxx"; String url = urlToSign + "&key=" + encodedApiKey + "&signature=" + signature; Request request = new Request.Builder().url(url).get().build(); Response response = client.newCall(request).execute(); // Handle the response. } private String sign(String urlToSign, String secret) throws MalformedURLException, NoSuchAlgorithmException, InvalidKeyException { URL url = new URL(urlToSign); String canonicalUrl = url.getPath() + "?" + new CanonicalQueryString(url.getQuery()).toString(); SecretKey secretKey = new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"); Mac mac = Mac.getInstance("HmacSHA256"); mac.init(secretKey); byte[] sigBytes = mac.doFinal(canonicalUrl.getBytes()); String signature = Base64.getEncoder().encodeToString(sigBytes); signature = signature.replace('+', '-'); signature = signature.replace('/', '_'); return signature; } static class CanonicalQueryString { private static final int INIT_BUFFER_SIZE = 128; /** * The constant PARAMETER_EQ. */ public static final String PARAMETER_EQ = "="; /** * The constant PARAMETER_SEP. */ public static final String PARAMETER_SEP = "&"; private SortedMap<String, List<String>> params = new TreeMap<>(); /** * Instantiates a new Canonical query string. * * @param queryString the query string */ public CanonicalQueryString(String queryString) { if (queryString == null || queryString.length() == 0) { return; } for (String pair : queryString.split(PARAMETER_SEP)) { int idx = pair.indexOf(PARAMETER_EQ); if (idx != -1) { String value = pair.substring(idx + 1); if (value == null || value.length() == 0) { continue; } String paramKey = pair.substring(0, idx); List<String> values = params.computeIfAbsent(paramKey, key -> new ArrayList<>()); try { values.add(URLDecoder.decode(value, "UTF-8")); } catch (UnsupportedEncodingException e) { // ignore } } } } @Override public String toString() { StringBuilder sb = new StringBuilder(INIT_BUFFER_SIZE); for (SortedMap.Entry<String, List<String>> item : params.entrySet()) { String key = item.getKey(); for (String value : item.getValue()) { if (sb.length() > 0) { sb.append(PARAMETER_SEP); } sb.append(key).append(PARAMETER_EQ).append(value); } } return sb.toString(); } } }