在撰寫本文時,我意識到在移動應用程序中使用Google Static Maps API存在一些許可限制…無論如何,我只是出于研究目的而發布它,但我必須警告您注意以下限制:
?
http://code.google.com/intl/zh-CN/apis/maps/faq.html#mapsformobile
Google Static Maps API快速審核
?
使用此API,您可以基于URL和一些參數(可以傳入以獲得個性化地圖)來獲取圖像。 您可以使用縮放,地圖類型,圖像大小(寬度,高度),地圖位置處的標記等來玩。您必須記住一個限制,使用API??需遵守每天每位查看者最多只能查詢1000個獨特(不同)圖像請求,其中包含大量圖像…但是如果您需要更多圖像,還可以使用Premium許可證。 欲獲得更多信息:
http://code.google.com/intl/zh-CN/apis/maps/documentation/staticmaps/
好的,我們要做的是:
- 創建一個方法,該方法接收緯度和經度點以及圖像的大小作為參數。
- 使用以下網址請求地圖圖像: http : //maps.googleapis.com/maps/api/staticmap ,并添加一些參數。
- 創建一個Image對象并返回它,以便我們可以在屏幕上顯示它。
動手實驗室
以下是我們正在談論的方法。 它具有用于緯度和經度的參數,也用于我們請求的圖像的寬度和高度的參數。 可以使用Location API檢索緯度和經度,并可以使用Canvas類檢索寬度和高度。
public Image getMap(double lat, double lon, int width, int height)
throws IOException
{String url = "http://maps.google.com/maps/api/staticmap";url += "?zoom=15&size=" + width + "x" + height;url += "&maptype=roadmap";url += "&markers=color:red|label:A|" + lat + "," + lon;url += "&sensor=true";HttpConnection http = (HttpConnection) Connector.open(url);InputStream in = null;byte[] imgBytes = null;try {http.setRequestMethod(HttpConnection.GET);in = http.openInputStream();ByteArrayOutputStream bos = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int n = 0;while ((n = in.read(buffer)) != -1) {bos.write(buffer, 0, n);}imgBytes = bos.toByteArray();} finally {if (in != null) {in.close();}http.close();}Image img = Image.createImage(imgBytes, 0, imgBytes.length);return img;
}
如您所見,獲取地圖圖像非常簡單。 檢索的是純HTTP請求。
接下來,您可以找到Google靜態地圖從我家鄉的某個位置檢索到的圖像。
好的,您剛剛看到如果不存在限制,那將是多么簡單……您如何看待該限制? 這有點令人困惑,不是嗎?
無論如何,我們將需要尋找另一種在我們的移動應用程序上顯示地圖的方法。
參考:來自Java和ME博客的JCG合作伙伴 Alexis Lopez的Google Static Maps API和JavaME 。
翻譯自: https://www.javacodegeeks.com/2012/05/javame-google-static-maps-api.html