在控制台中打印正确的URL:
https://maps.googleapis.com/maps/api/staticmap?center=37.387138,-122.083237&zoom=15&size=200x200&key=API_KEY
将其插入html时得到以下信息
https://maps.googleapis.com/maps/api/staticmap?center=37.386337,-122.085823%E2%80%A65&size=200x200&maptype=roadmap&key=API_KEY
是什么导致此问题?我确实对URL进行了编码,所以我不确定这是什么问题。
JavaScript函数:
function genStaticMap(mapObj, elemID, width, height, mapType){
var center = mapObj.lat + "," + mapObj.lng; // define map center
var zoom = mapObj.radius; // get radius (zoom) of map
var altTag = mapObj.title; // get alt tag for map
var mapURL = "https://maps.googleapis.com/maps/api/staticmap?center="
+ center + "&zoom=" + zoom + "&maptype=" + mapType
+ "&size=" + width + "x" + height
+ "&key=" + API_KEY;
console.log(mapURL);
var mapURL = encodeURI(mapURL);
var staticMap = "<img src=\"" + mapURL + "\" alt=\"" + altTag + "\" />";
$(staticMap).appendTo(elemID); // append map to element
}
最佳答案
var mapURL = encodeURI(mapURL);
是可能的罪魁祸首。您可以删除该行,还是紧随其后移动console.log()
进行检查?