我正在尝试学习创建使用Google Maps的应用程序,但是我不明白为什么试图在A点和B点之间绘制路线时我的请求被拒绝了。我的应用程序运行了,我可以看到点a和点b的标记,但是未绘制路线,并且在检查日志时出现标题中描述的错误。
首先,我创建了一个API密钥,并确保它不受我的Google API控制台的限制。
首先,我的密钥存储在res / values / google_maps_api.xml
public IEnumerable<keyart1> Get([FromUri] string[] keyword1)
{
List<keyart1> keylist;
List<IEnumerable<keyart1>> ll;
using (dbEntities5 entities = new dbEntities5())
{
ll = new List<IEnumerable<keyart1>>();
foreach (var item in keyword1)
{
keylist = entities.keyart1.Where(e => e.keyword != item).ToList();
var result = keylist.Distinct(new ItemEqualityComparer());
ll.Add(result);
}
var intersection = ll.Aggregate((p, n) => p.Intersect(n).ToList());
return intersection;
}
}
我有一个类,其中将参数声明为字符串,并返回完整的字符串和键,如下所示:
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false"><my key here></string>
但是,在以下名为“ DrawRoute”的类的方法中使用此url时,出现标题中描述的错误,并且在运行时未绘制路线。
String parameters = str_origin + "&" + str_dest + "&" + sensor + "key=" + R.string.google_maps_key;
return "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters;
logcat跟踪:
private String getJsonRoutePoint(String strUrl) throws IOException {
String data = "";
InputStream iStream = null;
HttpURLConnection urlConnection = null;
try {
URL url = new URL(strUrl);
// Creating an http connection to communicate with url
urlConnection = (HttpURLConnection) url.openConnection();
// Connecting to url
urlConnection.connect();
// Reading data from url
iStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line);
}
data = sb.toString();
Log.d("getJsonRoutePoint", data.toString());
br.close();
} catch (Exception e) {
Log.d("Exception", e.toString());
} finally {
iStream.close();
urlConnection.disconnect();
}
return data;
}