I made a function for the same requirement. You can check and let me know if this gives some error:
function setMarkersWithCenter(map,latiarray,longiarray) { if(latiarray.length != longiarray.length) return; var total_locations = latiarray.length; var minLongi = null, minLati = null, maxLongi = null, maxLati = null; var totalLongi = 0.0, totalLati = 0.0; for(var i = 0; i < total_locations; i++) { if(minLati == null || minLati > latiarray[i]) { minLati = latiarray[i]; } if(minLongi == null || minLongi > longiarray[i]) { minLongi = longiarray[i]; } if(maxLati == null || maxLati < latiarray[i]) { maxLati = latiarray[i]; } if(maxLongi == null || maxLongi < longiarray[i]) { maxLongi = longiarray[i]; } } var ltDiff = maxLati-minLati; var lgDiff = maxLongi-minLongi; var delta = ltDiff>lgDiff ? ltDiff : lgDiff; if(total_locations>0 && delta>0) { map.setLocation({ animate : true, latitude:((maxLati+minLati)/2), longitude:((maxLongi+minLongi)/2), latitudeDelta:delta, longitudeDelta:delta, }); } }
Hope this helps.
source share