显示损坏的静态路线图片(Google Maps Directions API) [英] Static route image showing broken (Google Maps Directions API)

查看:122
本文介绍了显示损坏的静态路线图片(Google Maps Directions API)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Google Maps Directions API检索路线的静态图片.

I'm attempting to retrieve a static image of a route from Google Maps directions API.

// addresses
$from_address = "Mobile,AL"
$to_address = "Athens,GA"

// build static image url
$url_route_json = "https://maps.googleapis.com/maps/api/directions/json?origin=$from_address&destination=$to_address&mode=driving&key=$goog_map_dist_api_key";

// replace any spaces in the route address with + signs
$url_route_json = str_replace(' ', '+', $url_route_json);

// get the json response
$resp_json = file_get_contents($url_route_json);

// decode the json
$jsondata = json_decode($resp_json, true);

// URL encode overview_polyline
$overview_polyline = urlencode($jsondata['routes'][0]['overview_polyline']['points']);

// build image URL
$url_route_img = "https://maps.googleapis.com/maps/api/staticmap?size=600x400&path=enc%3A$overview_polyline&key=$goog_map_dist_api_key";

url_route_img 变为:

它在img标签中像这样:

That goes in an img tag like so:

<img src="$url_route_img">

包含在img标签中,我看到了:

Included in an img tag, I see this:

我的密钥仅对我们服务器的IP有效,这是在限制"下的"API密钥配置"屏幕中输入的.我还特别允许在同一键配置上使用Directions和Static Maps API(directions部分工作正常).

My key is only valid for our server's IP, which is entered in the API key config screen under Restrictions. I've also specifically allowed the Directions and Static Maps APIs on the same key config (the directions part works fine).

要显示静态路线的图像我会错过什么?

What am I missing to get a static image of a route to appear?

推荐答案

Static Maps API需要基于HTTP的限制,而Directions正在寻找基于IP的限制.

Static Maps API requires HTTP based restriction, while Directions was looking for IP based restriction.

我创建了第二个专门用于静态地图的API密钥,并使用HTTP限制进行了配置.

I created a second API key specifically for Static Maps and configured using HTTP restriction.

图像现在可以完美加载.

Image now loads perfectly.

这篇关于显示损坏的静态路线图片(Google Maps Directions API)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆