2

在我的 Android 应用程序中,我使用Picasso加载图像。这通常工作得很好。

今天我尝试从谷歌地图 api 加载静态图像,但这似乎不起作用。当我打开他们信息页面上提供的示例链接时,我可以很好地看到静态地图图像。当我使用下面的行在我的 Android 应用程序中加载它时,我什么也得不到。

Picasso.with(getContext()).load("http://maps.googleapis.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=13&size=370x250&maptype=roadmap%20&markers=color:blue|label:S|40.702147,-74.015794&markers=color:green|label:G|40.711614,-74.012318%20&markers=color:red|color:red|label:C|40.718217,-73.998284&sensor=false").into(mapView);

我还尝试下载图像并将其上传到我的个人网站空间,从中可以很好地加载,但不知何故,它似​​乎并没有直接从直接的 google API url 加载。

有谁知道为什么会这样,我该如何解决?

4

3 回答 3

0

唯一想到的编程故障点是解析 URI。查看当前的毕加索代码(https://github.com/square/picasso/blob/master/picasso/src/main/java/com/squareup/picasso/Picasso.java)我看到以下内容:

  public RequestCreator load(String path) {
    if (path == null) {
      return new RequestCreator(this, null, 0);
    }
     if (path.trim().length() == 0) {
      throw new IllegalArgumentException("Path must not be empty.");
    }
    return load(Uri.parse(path));
  }

所以我先调试

Uri.parse("http://maps.googleapis.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=13&size=370x250&maptype=roadmap%20&markers=color:blue|label:S|40.702147,-74.015794&markers=color:green|label:G|40.711614,-74.012318%20&markers=color:red|color:red|label:C|40.718217,-73.998284&sensor=false")

看看那个对象是什么样子的。它是否会降低或混淆您的任何参数?

如果这不能带您到任何地方,请尝试使用 HttpClient [或类似的] 手动下载文件。然后至少您可以完全调试请求/响应。

另外,我知道谷歌地图有一些限制——你确定你没有达到它们吗?

于 2013-12-10T16:34:30.330 回答
0
  • 替换httphttps
  • 替换 | 与 %7C
  • 添加 api 密钥
于 2019-09-03T15:08:48.543 回答
0

.loadMap() 函数有许多声明的变量。这是整个过程的核心。

因此,静态地图 API 为我们提供图像所需的是,我们使用给定的 url 发出 http 请求,并接收到图像响应 (URL)。让我们来看看这些变量的含义和效用。是的,它们都有完全不同的含义!

在进行 API 调用时,mapUrlInitial 变量始终相同。它有一个中心查询( ?center ),它指定我们希望该位置在地图中居中。

mapUrlProperties 变量包含一个字符串,您可以在其中控制您将获得的图像响应的实际缩放、图像的大小和将指出我们位置的标记的颜色。

mapUrlMapType 变量是一个字符串,您可以在其中实际确定所需的标记大小和地图的类型。我们在应用程序中使用路线图。

最后 latLong 是一个字符串,它连接我们想要精确定位的地方的纬度和经度!

然后我们连接所有这些字符串以形成一个可行的 Url。然后在 Picasso 代码中加载 Url,如我们上面所见。我们可以注意到的一件事是,所有这一切都需要一个事件对象,因为我们能够使用事件对象获取位置详细信息!最终代码:-

fun loadMap(event: Event): String{
  //location handling

  val mapUrlInitial = “https://maps.googleapis.com/maps/api/staticmap?center=”

  val mapUrlProperties = “&zoom=12&size=1200×390&markers=color:red%7C”

  val mapUrlMapType = “&markers=size:mid&maptype=roadmap”
  val latLong: String = “” +event.latitude + “,” + event.longitude
  return mapUrlInitial + latLong + mapUrlProperties + latLong + mapUrlMapType

}
//load image

Picasso.get()

      .load(loadMap(event))

      .placeholder(R.drawable.ic_map_black_24dp)

      .into(rootView.image_map)
于 2019-09-03T15:14:32.903 回答