2011-03-16 68 views

回答

91

Google提供免费的静态地图API。

您可以从网站下载动态配置的位图,将其存储在文件系统或内存中,然后从中获取可绘制的图表以将其设置为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" 

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

你可以找到这里是Google Static Maps API的文档。

+0

不知道......谢谢你的分享! – WarrenFaith 2011-03-17 12:19:56

+0

太棒了!我使用了第二个mapview,这给我带来了很多麻烦(通常每个进程只能有一个),但我可能会坚持这个解决方案。只是一个问题 - 每个应用程序的25k下载(https://developers.google.com/maps/documentation/staticmaps/#Limits)是否适用于此?如果是这样,他们如何检查它?请求来自不同的设备,网址没有API密钥... – 2012-11-29 11:55:51

+0

你是对的,没有API密钥,他们无法强制执行每个应用25K限制。目前这个API可能没有限制,如果请求来自不同的IPS。 – pcans 2012-12-02 12:49:22

-6

首先,您将在可绘制文件夹中显示的地图图像用作该图像视图的背景。

+0

不相关的问题 – 2017-03-25 04:10:55

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; 
    } 
+4

此代码已过时! – 2016-09-22 13:06:37

-1

正如@pcans所说,我真的很喜欢他的解决方案。您可以使用字符串作为URL在ImageView中加载图像。我建议图像加载第三方库: https://github.com/koush/ion

希望它可以帮助你:)

0

此外,另一种解决方案是毕加索库:“强大的图像下载和缓存库为Android

你给图片网址,毕加索,然后它做一个GET请愿书,并加载图像。如果申请失败,毕加索可以设置默认图片。

您甚至可以在加载图像时设置进度动画。

这是一个很好的和干净的解决方案。

一个例子:

ImageView ivUrl = (ImageView) view.findViewById(R.id.iv_photo); 

Picasso.with(getContext()).cancelRequest(ivUrl); 

Picasso.with(getContext()) 
    .load(imageUrlString) 
    .error(R.drawable.ic_error) 
    .placeholder(R.drawable.progress_animation) 
    .into(ivUrl); 
4

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> 

然后编程创建静态地图网址并更换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) { 

}