pjd
pjd

Reputation: 1173

Android: MapView.GetLatitudeSpan(), GetLongitudeSpan() Anomaly?

I'm working on a mapping app that plots pins on a MapView based on a user's query. I'm trying to scale the map to fit all the results pins, but I've run into a seemingly strange situation.

I have two variables set up:

latSpan is the difference between the maximum latitude and minimum latitude of any of the results points

lonSpan is the difference between the maximum longitude and minimum longitude of any of the results points

This method

        while ((mapView.getLatitudeSpan()) < latSpan) || (mapView.getLongitudeSpan() < lonSpan)){
            mapController.zoomOut();
        }//end of while loop

is supposed to zoom out to make sure all the pins fit on the viewable map screen.

But I'm experiencing something rather strange. The results of mapView.getLatitudeSpan() and mapView.getLongitudeSpan() are routinely greater than my latSpan and lonSpan values, so the MapController doesn't zoom out enough.

My map is zoomed in pretty far--level 15 or higher.

As an example, one set of search results gave the following values:

latSpan = 17928

lonSpan = 11636

mapView.getLatitudeSpan() = 21933

mapView.getLongitudeSpan() = 20598

Based on these numbers, you wouldn't think that the MapController would need to zoom out. Yet there are pins plotted both above the top and below the bottom of the screen. I changed my WHILE loop to read

while ((mapView.getLatitudeSpan() - 6000) < latSpan...

and that helps, but the right query will still cause issues.

But the real question is, why is this happening?

Upvotes: 2

Views: 976

Answers (1)

Louth
Louth

Reputation: 12423

I'm not sure why you're code isn't working from the snippet provided. Its possible that you are not converting your latSpan and lonSpan to microDegrees (as shown below) and this would cause some issues.

Also if you're trying to make sure your mapView is showing all of the results, there's not much point trying to determine if it needs to zoom before zooming, just zoom it every time. If it turns out that it doesn't need to zoom then nothing will appear to happen and if it does then it does.

You can set a map up to encompass all of your points and move to the centroid of the points as follows:

GeoPoint max = new GeoPoint(maxLatitude, maxLongitude);
GeoPoint min = new GeoPoint(minLatitude, minLongitude);

int maxLatMicro = max.getLatitudeE6();
int maxLonMicro = max.getLongitudeE6();
int minLatMicro = min.getLatitudeE6();
int minLonMicro = min.getLongitudeE6();

GeoPoint center = new GeoPoint((maxLatMicro+minLatMicro)/2,(maxLonMicro + minLonMicro)/2);

controller.zoomToSpan(maxLatMicro - minLatMicro, maxLonMicro - minLonMicro);
controller.animateTo(center);

Upvotes: 1

Related Questions