在控制台中打印正确的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
}
答案 0 :(得分:1)
var mapURL = encodeURI(mapURL);
可能是罪魁祸首。您可以删除该行,还是立即移动console.log()
来检查?