I am trying to create bound and zoom to the my markers. But i have the error i mentioned at line:
bounds.extend(markers[i].position);
When i set the length of my markers.length in for loop then it gives Too much recursion error
I have all the markers displayed but they are not zoomed and bounded and i also have the error which i just mentioned.
My code to do this is:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
#map-canvas {
width: 1350px;
height: 600px;
}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.3.js"></script>
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script>
function initialize() {
//var markers = new Array();
var markers = [];
$.ajax({
type: 'GET',
url: './Content/TextFile/LongLat.txt',
//filereader\filereader\Content\TextFile\LongLat.txt
// url: './distinctGimeiNo.txt',
dataType: 'text',
}).success(function (data) {
var infowindow = new google.maps.InfoWindow();
var marker;
var s2 = data.replace(/^.*$/, " ").replace(/\r\n/g, " ");
var array = s2.split(/[ ]+/g);
var test = [].concat.apply([], array.map(function (array) { return array.split(/\s+/); }))
var col1 = [];
var col2 = [];
var col3 = [];
var j = 0;
// var currentLocation =
var mapOptions =
{
zoom: 2,
center: new google.maps.LatLng(73, 23),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
for (var i = 0; i <= test.length - 3; i = i + 3) {
col1[j] = test[i];
col2[j] = test[i + 1];
col3[j] = test[i + 2];
var myLatlng = new google.maps.LatLng(col3[j], col2[j]);
marker = new google.maps.Marker(
{
position: myLatlng,
map: map,
title: 'Hello World! ' + col1[j]
});
markers.push(marker);
console.log("test2");
j++;
}
function AutoCenter() {
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < markers.length; i++) {
bounds.extend(markers[i].position);
}
map.fitBounds(bounds);
}
AutoCenter();
})
}
google.maps.event.addDomListener(window, 'load', initialize);
console.log("Check finished2");
</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>
Could someone please let me know the solution of the problem ?