views:

2212

answers:

2

I am having some trouble with the Google Maps API. I have an array which holds a ojbect I created to store points.

My array and class:

var tPoints = [];    
function tPoint(name) {
        var id = name;
        var points = [];
        var pointsCount = 0;
        ...
        this.getHeadPoint = function() { return points[pointsCount-1]; }
    }

tPoint holds an array of GLatLng points. I want to write a function to return a GLatLngBounds object which is extended from the current map bounds to show all the HeadPoints.

Heres what I have so far..

function getBounds() {
    var mBound = map.getBounds();
    for (var i = 0; i < tPoints.length; i++) {
     alert(mBound.getSouthWest().lat() + "," + mBound.getSouthWest().lng());
     alert(mBound.getNorthEast().lat() + "," + mBound.getNorthEast().lng());
            currPoint = trackMarkers[i].getHeadPoint();        
            if (!mBound.containsLatLng(currPoint)) {
                mBound.extend(currPoint);
            }
        }
    return mBound;
}

Which returns these values for the alert. (Generally over the US)
"19.64258,NaN"
"52.69636,NaN"
"i=0"
"19.64258,NaN"
"52.69636,-117.20701"
"i=1"

I don't know why I am getting NaN back. When I use the bounds to get a zoom level I think the NaN value is causing the map.getBoundsZoomLevel(bounds) to return 0 which is incorrect. Am I using GLatLngBounds incorrectly?

+1  A: 

The google maps sample is using this code...

var bounds = map.getBounds();
var southWest = bounds.getSouthWest();
var northEast = bounds.getNorthEast();
var lngSpan = northEast.lng() - southWest.lng();
var latSpan = northEast.lat() - southWest.lat();

...which is putting the SouthWest/NorthEast bounds into a variable before attempting to get the individual lng/lat coordinates. Maybe there is something with the "nested" evaluations causing problems. Have tried the granular approach to see if you get the data you need?

Dillie-O
+1  A: 

Hey Dillie-O,

I found that example through my Google searches too and did play with it. That wasn't the problem.

I found my bug. No one would have been able to solve the problem. It turns out that right before I test my bounds I had centered my map with bad data. I did something like the lngSpan = northEast.lng() - southWest.lng(); however JavaScript interpreted my var as a string. So (maxLng-minLng)/2 + minLng returns something like "20.456-116.1178" as the lng. I centered my map on var centerPoint = new GLatLng(setLat, setLng); and after that the maps API gets a little strange ;)

Thanks for the help though.

Bernie Perez