Reputation: 1250
I tried to debug this code using Chrome and JSLint for 4 days without any promising. My code should show 2 regions on a Google Map, separated by their RegionID
My 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
// 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. I tried deleting the for loop and push to region
array by one variable and it worked again. It is ridiculous but it is what I've figured out after 4 days.
Thank you
Upvotes: 0
Views: 338
Reputation: 117354
I can't tell you what's happening there, but it works for me when I replace
region.length=0;
with
region=[];
Upvotes: 1