【发布时间】:2020-04-13 22:40:54
【问题描述】:
我有一个 Google Maps API 文件,我正在尝试在其中绘制折线和多边形。但是,当我输入用于绘制这些的代码时,我在 Google Chrome 中收到一个错误,提示我的“未捕获的语法错误:输入意外结束”结束脚本括号 ()。
这是我的代码:
<script>
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
//center on the US
center: {lat: 39.8283, lng: -98.5795},
zoom: 5
});
// Define the LatLng coordinates for the polygon's path.
var sampleCounty = [
{lat: 32.344437, lng: -86.496774},
{lat: 32.402814, lng: -86.717897},
{lat: 32.340803, lng: -86.814912},
];
// Construct the polygon.
var samplePolygon = new google.maps.Polygon({
paths: sampleCounty,
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35,
});
//place polygon in map
samplePolygon.setMap(map);
//make HTML element change color when polygon is hovered over
google.maps.event.addListener(samplePolygon, 'mouseover', function (event) {
document.getElementById("sampleDescription").style.color = "blue";
});
//make HTML element change color when polygon is not hovered over
google.maps.event.addListener(samplePolygon, 'mouseout', function (event) {
document.getElementById("sampleDescription").style.color = "red";
});
</script><!--end google maps API-->
<!--api link here--><script src="https://maps.googleapis.com/maps/api/js?key=MY_KEY&libraries=places&callback=initAutocomplete" async defer></script>
在我的代码中,我的 API 密钥在那里。有谁知道为什么会发生这种情况/如何解决?
【问题讨论】:
-
使用好的 IDE 或将您的代码粘贴到在线 Javascript 验证器中,它会告诉您问题所在。
标签: javascript google-maps-api-3 polygon polyline