如何在android imageview上显示静态谷歌地图?

Adn*_*nan 46 android

我想在imageview上显示静态谷歌地图.类似于"Whatsapp"的东西在分享位置时显示.我怎样才能做到这一点?

pca*_*ans 100

Google免费提供静态地图API.

您可以从Web下载动态配置的位图,将其存储在文件系统或内存中,然后从中获取可绘制的位图以将其设置为ImageView.

您需要从坐标生成一个URL,以使用此URL从Web加载数据.例如显示巴黎埃菲尔铁塔的200x200位图:

String latEiffelTower = "48.858235";
String lngEiffelTower = "2.294571";
String url = "http://maps.google.com/maps/api/staticmap?center=" + latEiffelTower + "," + lngEiffelTower + "&zoom=15&size=200x200&sensor=false&key=YOUR_API_KEY"
Run Code Online (Sandbox Code Playgroud)

StackOverflow已经有一些关于如何从网上下载图像以便在imageview中显示它的答案:https://stackoverflow.com/search q = imageview + url +%5Bandroid%5D

您可以在此处找到Google Static Maps API的文档.

  • 如何使用此代码段在地图上添加标记? (2认同)
  • 现在,这不是最佳解决方案,因为API已更改,现在需要API密钥,如果未提供,则有时会返回错误。此外,每天最多有25,000个请求。 (2认同)

小智 20

public static Bitmap getGoogleMapThumbnail(double lati, double longi){
        String URL = "http://maps.google.com/maps/api/staticmap?center=" +lati + "," + longi + "&zoom=15&size=200x200&sensor=false";
        Bitmap bmp = null;
        HttpClient httpclient = new DefaultHttpClient();   
        HttpGet request = new HttpGet(URL); 

        InputStream in = null;
        try {
            in = httpclient.execute(request).getEntity().getContent();
            bmp = BitmapFactory.decodeStream(in);
            in.close();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return bmp;
    }
Run Code Online (Sandbox Code Playgroud)


Nic*_*ian 15

您现在还可以使用SDK本地显示静态图像(这是2014年12月添加的):https: //developers.google.com/maps/documentation/android/lite

这将导致MapView.

  • 我不建议这样做,即使在lite模式下,MapView对象在加载时会挂起你的UI一秒钟,如果你要在RecyclerView中加载地图图像,这一点尤其明显.因此,根据您的使用情况,您可能希望使用静态图像API. (3认同)

Vin*_*ran 5

Web视图是我的建议.

在资产文件夹中的html文件夹中创建一个html文件static_map.html

其内容可能如下所示

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style>
    html,body{
        padding:0;
        margin:0;
    }
    .map{
        position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 100%;
    }
</style>
</head>

<body>
<img class="map" src="REPLACE_HERE" >
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

然后以编程方式创建一个静态映射URL并用它替换REPLACE_HERE字符串.然后将其加载到Web视图上.这将极大地减少我们的努力.

代码如下

try {
    String url ="https://maps.googleapis.com/maps/api/staticmap?";
    url+="&zoom=13";
    url+="&size=600x300";
    url+="&maptype=roadmap";
    url+="&markers=color:green%7Clabel:G%7C"+latitude+", "+longitude;
    url+="&key="+ YOUR_GOOGLE_API_KEY;

    InputStream is = context.getAssets().open("html/static_map.html");
    int size = is.available();
    byte[] buffer = new byte[size];
    is.read(buffer);
    is.close();
    String str = new String(buffer);
    str = str.replace("REPLACE_HERE", url);
    wv_map.getSettings().setJavaScriptEnabled(true);
    wv_map.loadDataWithBaseURL("", str, "text/html", "UTF-8", "");
} catch (IOException e) {

} 
Run Code Online (Sandbox Code Playgroud)