[英]Failed to show polygon on Google Map
I tried to debug this code using Chrome and JSLint for 4 days without any promising. 我尝试使用Chrome和JSLint调试此代码4天,但没有任何希望。 My code should show 2 regions on a Google Map, separated by their
RegionID
我的代码应在Google地图上显示2个区域,并用其
RegionID
My HTML 我的HTML
<body>
<div id="map_canvas" style="width: 100%; height: 100%;"></div>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?sensor=false®ion=vi&language=vi">
</script>
<script type="text/javascript" src="jquery-1.9.1.min.js"></script>
<script type="text/javascript">
var map;
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(10.79, 826.694),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
}
</script>
<script type="text/javascript">
$(document).ready(function () {
initialize();
var model = [{"ID":946,"Latitude":10.7654200827348,"Longitude":106.681716282384,"RegionID":80},{"ID":947,"Latitude":10.7563117596896,"Longitude":106.685397072824,"RegionID":80},{"ID":948,"Latitude":10.7537788369752,"Longitude":106.68713674361,"RegionID":80},
{"ID":1271,"Latitude":10.8577143149377,"Longitude":106.657884916756,"RegionID":82},{"ID":1272,"Latitude":10.8576257110992,"Longitude":106.657710314914,"RegionID":82},{"ID":1273,"Latitude":10.8575762617472,"Longitude":106.657500586099,"RegionID":82},{"ID":1274,"Latitude":10.8575794962578,"Longitude":106.657309632748,"RegionID":82},{"ID":1275,"Latitude":10.8576187762307,"Longitude":106.65709567219,"RegionID":82}]
polygon(model);
});
</script>
And here is my polygon
function 这是我的
polygon
函数
// Take an array of objects that have lat and lon attribute
function polygon(polygonCoords) {
// Initialize
// First dimension
var id = polygonCoords[0].RegionID;
var long;
var lat;
var region = new Array();
for (var i = 0; i < polygonCoords.length; i++) {
if ((id != polygonCoords[i].RegionID) || (i == polygonCoords.length - 1)) {
// Draw polygon
// Set path to a polygon along with polygon's option
var regionPolygon = new google.maps.Polygon({
paths: region,
fillColor: "#0000A0",
editable: false,
});
// Set to map
regionPolygon.setMap(map);
// Reset stuffs
region.length = 0;
id = polygonCoords[i].RegionID;
}
long = polygonCoords[i].Longitude;
lat = polygonCoords[i].Latitude;
region.push(new google.maps.LatLng(lat, long));
}
}
All I receive is just a Google Map without any polygon. 我收到的只是一张没有任何多边形的Google地图。 I tried deleting the for loop and push to
region
array by one variable and it worked again. 我试着删除for循环,并通过一个变量推送到
region
数组,它再次起作用。 It is ridiculous but it is what I've figured out after 4 days. 这太荒谬了,但这是我四天后才想到的。 Thank you
谢谢
I can't tell you what's happening there, but it works for me when I replace 我无法告诉您那里发生了什么,但是当我更换时它对我有用
region.length=0;
with 与
region=[];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.