Titanium Community Questions & Answer Archive

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

Set map zoom level

I have a mapView and on the click of a button, want it to reset to a specific zoom level, is there a property for this (I can't see one in the API docs, unless I have misunderstood one).

I currently use mapView.zoom(x) but this is relative to current zoom level.

Thanks,
Dan

— asked April 20th 2010 by Dan Duke
  • android
  • level
  • map
  • win32
  • zoom
0 Comments

8 Answers

  • Hey Dan,
    I think that you can zoom into a specific level by settin latitudeDelta and longitudeDelta ex:

    var mapview = Titanium.Map.createView({
        mapType: Titanium.Map.STANDARD_TYPE,
        region: {latitude:45.46411, longitude:9.19193, latitudeDelta:0.05, longitudeDelta:0.05}, 
        animate:true, 
        regionFit:true,
        userLocation:true
    });
    
    win2.add(mapview);
    
    — answered April 20th 2010 by Nicolo Verrini
    permalink
    0 Comments
  • Thanks for that Nicolo, a little stuck on how to impliment that at the moment. I have a click listener setup for a button as the map already exists and is displayed. My code is something like:

    mapView.region = {latitude:mapLatitude, longitude:mapLongitude, latitudeDelta:0.05, longitudeDelta:0.05};
    mapView.animate = true;
    

    But I get the useful 'stopped unexpectedly' error message. Have I set the variable wrong in here, I tried several variations, but didn't hit upon one that did the trick.

    — answered April 21st 2010 by Dan Duke
    permalink
    0 Comments
  • Still having difficulty with this, anyone got some advice on what I could try next?

    — answered April 27th 2010 by Dan Duke
    permalink
    0 Comments
  • I haven't tested it, but you could try setting the region back to its original setting.

    — answered April 27th 2010 by Don Thorp
    permalink
    0 Comments
  • Don, sadly that still gave me the 'stopped unexpectedly' error. I tried putting the below within the click handler:

    mapOverlayLink.addEventListener('click',function(e)
    {
        Titanium.API.info("Latitude: " + mapLatitude);
        Titanium.API.info("Longitude: " + mapLongitude);
        var mapWindow = Ti.UI.createWindow({title:'Map'});
    
        var mapView = Titanium.Map.createView({
            mapType: Titanium.Map.STANDARD_TYPE,
            region: {latitude:50, longitude:0, latitudeDelta:0.001, longitudeDelta:0.001}, 
            animate:true, 
            regionFit:true,
            userLocation:true
        });
        mapWindow.add(mapView);
        mapWindow.open();
    
    });
    

    The region setting are identical to the ones initially used when the map is created in the first place, but errors when I try to run it again as an update.

    — answered April 28th 2010 by Dan Duke
    permalink
    0 Comments
  • Haha, got it today, it wasn't region which I needed to use, it was location. Not sure I quite get the difference between the 2, but this one appears to work nicely.

    mapView.location = {latitudeDelta:0.05, longitudeDelta:0.05};
    

    Although sadly it seems to only run it once, if I were to then zoom out again, it wont rerun for some reason.

    — answered April 29th 2010 by Dan Duke
    permalink
    0 Comments
  • Hey Dan ,
    I think that if u have a click listener set up for a button u have to redeclare the mapview variable try this:

    button.addEventListener('click',function(e)
            {
    var w = Ti.UI.createWindow({title:'Map'});
    var mapview = Titanium.Map.createView({
    mapType: Titanium.Map.STANDARD_TYPE,
    region: {
    latitude:mapLatitude, 
    longitude:mapLongitude, 
    latitudeDelta:0.05,
    longitudeDelta:0.05}, 
    animate:true, 
    regionFit:true,
    userLocation:true
         });
    w.add(mapview);
    w.open();
            });
    
    — answered April 23rd 2010 by Nicolo Verrini
    permalink
    1 Comment
    • O.O You are creating a new window and map every click! Is this a potential memory leak?

      — commented August 13th 2013 by Devin Weaver
  • Hi Nicolo, gave that a go this morning, and sadly I am getting the same error message coming up:

    mapOverlayLink.addEventListener('click',function(e)
    {
        Titanium.API.info("Latitude: " + mapLatitude);
        Titanium.API.info("Longitude: " + mapLongitude);
        var mapWindow2 = Ti.UI.createWindow({title:'Map'});
    
        var mapView2 = Titanium.Map.createView({
            mapType: Titanium.Map.STANDARD_TYPE,
            region: {latitude:mapLatitude, longitude:mapLongitude, latitudeDelta:0.05, longitudeDelta:0.05}, 
            animate:true, 
            regionFit:true,
            userLocation:true
        });
        mapWindow2.add(mapView2);
        mapWindow2.open();
    
    });
    

    The Lat/Long variables are populated so I don't think that is the issue (although I did also test with hard numbers, and still got the error). The view is also identical pretty much to how it was added originally (otherthan the number 2 on the variable name).

    The window display if empty, so it's to do with the map somehow, is it that I need to close the other mapView first do you think, if so, how to I do that, is it just .close()?

    — answered April 26th 2010 by Dan Duke
    permalink
    0 Comments
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.