Titanium Community Questions & Answer Archive

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

Titanium.Map.MapView - Get Map Bounds?

Hi, I am new to Appcelerator and am curious if there is a way to get the map bounds from the MapView object.

For example in the Google Maps API you can do the following:

var map = new GMap2(document.getElementById("map_canvas"));
map.setCenter(new GLatLng(37.4419, -122.1419), 13);

var bounds = map.getBounds();
var southWest = bounds.getSouthWest();
var northEast = bounds.getNorthEast();

The reason I am asking is because I want to query for items which are geocoded within those bounds.

I'm guessing there is a simple way to return these bounds but I don't see anything in the documentation.

Any help is appreciated! Thanks!

— asked May 4th 2010 by J.P. Given
  • geospacial
  • ipad
  • iphone
  • maps
0 Comments

3 Answers

  • Accepted Answer

    Hello,

    I've made a function which can count the boundaries from the center point and deltas.

    /**
     * Get the screen boundaries as latitude and longitude values 
     */
    function getMapBounds(region) {
        var b = {};
        b.northWest = {}; b.northEast = {};
        b.southWest = {}; b.southEast = {};
    
        b.northWest.lat = parseFloat(region.latitude) + 
            parseFloat(region.latitudeDelta) / 2.0;
        b.northWest.lng = parseFloat(region.longitude) - 
            parseFloat(region.longitudeDelta) / 2.0;
    
        b.southWest.lat = parseFloat(region.latitude) - 
            parseFloat(region.latitudeDelta) / 2.0;
        b.southWest.lng = parseFloat(region.longitude) - 
            parseFloat(region.longitudeDelta) / 2.0;
    
        b.northEast.lat = parseFloat(region.latitude) + 
            parseFloat(region.latitudeDelta) / 2.0;
        b.northEast.lng = parseFloat(region.longitude) + 
            parseFloat(region.longitudeDelta) / 2.0;
    
        b.southEast.lat = parseFloat(region.latitude) - 
            parseFloat(region.latitudeDelta) / 2.0;
        b.southEast.lng = parseFloat(region.longitude) + 
            parseFloat(region.longitudeDelta) / 2.0;
    
        return b;
    }
    

    You can use it like this:

    var bounds = getMapBounds(e);
    
    var a = Ti.Map.createAnnotation({
        latitude: bounds.southWest.lat,
        longitude: bounds.southWest.lng,
        pincolor: Ti.Map.ANNOTATION_RED,
        animate: false,
    });
    
    mapview.addAnnotation(a);
    
    — answered June 18th 2010 by Adam Wallner
    permalink
    0 Comments
  • Okay, there's an answer, but it ain't easy…

    We have support for a regionChanged event (shown here in our documentation). You have four properties of note on that event - the x/y coordinates on which the map is centered, and the "longitudeDelta" and "latitudeDelta". With this information, you can calculate the currently visible region of the map:

    latitudeDelta

    The amount of north-to-south distance (measured in degrees) to use for the span. Unlike longitudinal distances, which vary based on the latitude, one degree of latitude is approximately 111 kilometers (69 miles) at all times.

    longitudeDelta

    The amount of east-to-west distance (measured in degrees) to use for the span. The number of kilometers spanned by a longitude range varies based on the current latitude. For example, one degree of longitude spans a distance of approximately 111 kilometers (69 milies) at the equator but shrinks to 0 kilometers at the poles.

    You'll have to use this information to calculate the viewable area of the map. A quick Google search reveals a site which should help calculate the length of a degree of lat/long. That JavaScript (check it out in view source) ought to run fine in Titanium to perform the calculation.

    You have the primordial material to make this work, but will have to do some math - your solution would be very helpful to the community if you want to post it back here.

    — answered May 28th 2010 by Kevin Whinnery
    permalink
    0 Comments
  • Hmmm…I found this post…and while it's a possible alternative I would think that there should be an exposed method to the Google Map API for Bounds.

    https://developer.appcelerator.com/question/19881/near-me-functionality

    — answered May 4th 2010 by J.P. Given
    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.