4

I have multiple annotations on the map - although the user may need to scroll to see them. I want the user to be able to select a button to fit them all on the map - eliminating the need to scroll/manually resize.

I have determined the min. and max. latitudes and longitudes (e.g minLat, maxLat, minLong, maxLong) for the points I want to show on the map.

I'm struggling, however, with the specific formula to use to get the longitude and latitude properties to pass to mapview.setLocation. Any pointers on what formula I can use?

nigel
  • 43
  • 3

1 Answers1

7

I have made a function for the same requirement. You can test and let me know if it 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 it helps.

Dhairya Vora
  • 1,281
  • 12
  • 35
  • @DhairyaVora your code works really nice. I was to use it in my app. However, when annotations are separated with greater distance (e.g. on different continents it throws an error. Something like: "Invalid Region – kernelpanic Nov 25 '12 at 11:08
  • @kernelpanic, that is a good idea. We can put this code in try catch and if some error occurs, catch can show first annotation. And by the way, can you show me example locations for which this error occurs? – Dhairya Vora Nov 27 '12 at 16:17
  • @DhairyaVora I'm glad you're open to some improvements. In my example the error is thrown when I have two annotations - one in Atlanta, USA and another in Switzerland – kernelpanic Nov 27 '12 at 23:15
  • I could not find any error. As per the locations given by you, I have taken: 33.749,-84.387982 AND 47.051411,8.315826. Above code is running fine with me. Function sets center point at 40.4002055,-38.0360779 – Dhairya Vora Nov 28 '12 at 10:06
  • this is a fantastic question and brilliant answer. Did you mange to resolve/repeat the issue described by kernelpanic? It the function good in its current form? – Dave Feb 14 '13 at 16:02
  • This function working fine for me so far. I have not found any bug till now. If you find some bug, let me know; I can try to update the function. – Dhairya Vora Feb 14 '13 at 19:32