Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Mapview - Show all of my added Annotations?

Hi, again!

I just implemented a "Things nearby your location" function to my app. I cant determ how to set the zoomlevel - so that all of my annotations are visible in the mapview?

I cant find any documentation regarding "region" and "regionFit"…

Best regards, Joakim

— asked November 9th 2010 by Joakim Krassman
  • mapview
0 Comments

2 Answers

  • You will have to detect the min/max lat/long of all the annotations and set the region to this values

    — answered November 9th 2010 by Dan Tamas
    permalink
    1 Comment
    • Okey, but how does that look like in the code? I assumed that the region only could hold one pair of long/lat?

      region: {latitude:'59.6493301391602',
          longitude:'17.8708362579346'}
      

      — commented November 9th 2010 by Joakim Krassman
  • Once you know your min and max lats and longs (I calculate mine as I'm looping to add my annotations) you can do something like this which works nicely for me in my app:

    //create the map boundary area values
    var decMapBoundaryLatitude = (decLatitudeMax + decLatitudeMin) / 2;
    var decMapBoundaryLongitude = (decLongitudeMax + decLongitudeMin) / 2;
    var decMapBoundaryLatitudeDelta = decLatitudeMax - decLatitudeMin + 0.01;
    var decMapBoundaryLongitudeDelta = decLongitudeMax - decLongitudeMin + 0.01;
    
    //create the map region definition for the boundaries containing the sites
    var mapRegionSites = {latitude: decMapBoundaryLatitude, longitude: decMapBoundaryLongitude, animate: true, latitudeDelta: decMapBoundaryLatitudeDelta, longitudeDelta: decMapBoundaryLongitudeDelta};
    
    //tell the map view to display the region with the sites in
    objMapView.setLocation(mapRegionSites);
    
    — answered November 9th 2010 by Keith Rosenheck
    permalink
    3 Comments
    • Hmmm…I am ending up in a ocean with just water around me ;) I am supposed to end up i Sweden with these min/max cords…Can you see if something is wrong with the code?

      Thanks!

      var mapview = Titanium.Map.createView({
          mapType: Titanium.Map.HYBRID_TYPE,
      
          animate:true,
          regionFit:true,
          userLocation:true
          });
      
          var decLatitudeMin = 59.3442195636367;
          var decLatitudeMax = 59.737108462092;
      
          var decLongitudeMin = 17.3122215270996;
          var decLongitudeMax = 18.1620311737061;
      
          var decMapBoundaryLatitude = (decLatitudeMax + decLatitudeMin) / 2;
          var decMapBoundaryLongitude = (decLongitudeMax + decLongitudeMin) / 2;
          var decMapBoundaryLatitudeDelta = decLatitudeMax - decLatitudeMin + 0.01;
          var decMapBoundaryLongitudeDelta = decLongitudeMax - decLongitudeMin + 0.01;
      
      
      
          //Add annotations from array
      
          for (var c=0;c<s.length;c++) {
      
              var destPosition = Titanium.Map.createAnnotation({
                  latitude: s[c].latitude,
                  longitude: s[c].longitude,
                  title: s[c].spot,
                  subtitle: 'Click för details',
                  pincolor: Titanium.Map.ANNOTATION_GREEN,
                  animate: true,
                  rightButton: Titanium.UI.iPhone.SystemButton.DISCLOSURE
                  });
                  mapview.addAnnotation(destPosition);
          }
      
          var mapRegionSites = {latitude: decMapBoundaryLatitude, longitude: decMapBoundaryLongitude, latitudeDelta: decMapBoundaryLatitudeDelta, longitudeDelta: decMapBoundaryLongitudeDelta};
          mapview.setLocation(mapRegionSites)
      

      — commented November 9th 2010 by Joakim Krassman
    • You need to set the min and max variables in your add annotations loop using something like:

      if (!decLatitudeMax || decCurrentLatitude > decLatitudeMax) decLatitudeMax = s[c].latitude;
      if (!decLatitudeMin || decCurrentLatitude < decLatitudeMin) decLatitudeMin = s[c].latitude;
      if (!decLongitudeMax || decCurrentLongitude > decLongitudeMax) decLongitudeMax = s[c].longitude;
      if (!decLongitudeMin || decCurrentLongitude < decLongitudeMin) decLongitudeMin = s[c].longitude;
      

      — commented November 9th 2010 by Keith Rosenheck
    • Hmmm…..I think I need a bigger piece of code to get a hold of this.

      Regards Joakim

      — commented November 13th 2010 by Joakim Krassman
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.