I hope it's generic for all zoom levels.. it should be :).
How it works is that Google Maps at a zoom level of 1 displays the
entire world in 256 pixels... When you go to zoom level 2 you get half
the world in 256 pixels, zoom level 3 is 1/4 the world in 256 pixels...
which is the same as 1 divided by 2 (to the power of zoom level -
1), so the Math.pow(2, zoomLevel - 1) just gets the number that
corresponds to what you need to adjust by for the zoom level.
All these functions could be (and will be) combined into about 3 lines
for production.. but I just needed to step through it one step at a time
to test it out.
The double entireWorld = zoomToPower * 256; just calculates how many
pixels it would take to show the entire world (height or width)
and then I just brought it down to 1 degree so I could check the
calculations against a map and see what I was doing :).
double OneDegree = entireWorld / 360;
This calculates how much of a degree your screen will show for both the
height and width of your screen. I subtracted 40 pixels from the height
because that's roughly what my activity uses... and I didn't care if I
was exact.. I only want very close. I use it to call a web service and I
pass the Map Center latitude and longitude to the web service along with
the screen size (in degrees),
double heightSpan = (display.getHeight() - 40) / OneDegree;
double widthSpan = display.getWidth() / OneDegree;
and this is how I used it. Because my function returns the fraction of a
degree that will show on the screen, I adjust the getLatitudeSpan()
function to pass the same value by dividing it by 1E6. Again that should
be combined for production.
LatLonSpans latLonSpans = getSpans();
if (mapView.getLatitudeSpan() == 0)
{
nvps.add(new BasicNameValuePair("latitudespan",
String.valueOf(latLonSpans.LatSpan)));
}
else
{
double latSpan = mapView.getLatitudeSpan() / 1E6;
nvps.add(new BasicNameValuePair("latitudespan",
String.valueOf(latSpan)));
}
also do the Longitude....
and latLonSpans = null;
My function returns the percentage of a degree that your entire screen
will show, so I pass the map center lat and lon to the server with the
latLonSpan values and then on the server end, I use almost the same
calculation you do:
int topLatitude = mapCenter (latitude) + (latSpanPassed /2);
int bottomLatitude = mapCenter (latitude) - (latSpanPassed/2);
int leftLongitude = mapCenter (longitude) - (lonSpanPassed /2);
int rightLongitude = mapCenter (longitude) + (lonSpanPassed /2);
On 26/06/2010 8:47 AM, Pedro Teixeira wrote:
Hi Brad,
Thanks for your solution. I'm trying to understand how it works.
Is this generic for all zoom levels?
I don't know how to implement the if condition snippet of your code.
Something is missing me or I'm not understanding it right.
Can you explain this value:
latSpan/1E6, is this the simple getLatitudeSpan?
I'm sorry, I'm a little bit overwhelmed with this problem for some
days and I've been really confused.
On Jun 26, 2010, at 3:53 AM, Brad Gies wrote:
Pedro,
I've been working on roughly the same thing today... see the thread
MapView.getLatitudeSpan..
All you're running into is that the map isn't ready when you are
asking for the getLatitudeSpan, so you need to wait until it is
ready...OR...
the formula I've come up with to calculate it is this :
Note that I don't expect this formula is perfect.. and I can
certainly make it more compact and faster by reworking it, but for
working on it I wanted to do it step by step :).
private class LatLonSpans
{
@SuppressWarnings("unused")
public double LatSpan = 0.0;
@SuppressWarnings("unused")
public double LonSpan = 0.0;
}
private LatLonSpans getSpans()
{
LatLonSpans latLonSpans = new LatLonSpans();
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
int zoomLevel = mapView.getZoomLevel();
// zoomToPower times 256 should be equal to the pixels
required to view the entire world
// at the resolution I've set.
double zoomToPower = Math.pow(2, zoomLevel - 1);
// it takes 256 pixels to view the entire world at resolution
1, so this should be the pixels required to view the entire world
// at the resolution I've set.
double entireWorld = zoomToPower * 256;
// Now I need to scale the zoomToPower to my screen size to
figure
// out what I'm really going to see.
//
// this is how many pixels to see 1 degree.
double OneDegree = entireWorld / 360;
// and for height we have x pixels minus 40???? to account for
top bars..... so we will see this much of a degree.
double heightSpan = (height - 40) / OneDegree;
double widthSpan = width / OneDegree;
latLonSpans.LatSpan = heightSpan;
latLonSpans.LonSpan = widthSpan;
return latLonSpans;
}
and I'm using it like this :
if (mapView.getLatitudeSpan() == 0)
{
nvps.add(new BasicNameValuePair("latitudespan",
String.valueOf(latLonSpans.LatSpan)));
}
else
{
nvps.add(new BasicNameValuePair("latitudespan",
String.valueOf(latSpan / 1E6)));
}
Hope it helps... and if anyone can poke holes in my formula .. PLEASE
DO... I'd love to know it doesn't work BEFORE I start relying on it. :).
On 25/06/2010 5:55 PM, Pedro Teixeira wrote:
New approach that makes more sense:
int latSpan = mapView.getLatitudeSpan();
int longSpan = mapView.getLongitudeSpan();
GeoPoint mapCenter = mapView.getMapCenter();
int topLatitude = mapCenter.getLatitudeE6() + (latSpan/2);
int bottomLatitude = mapCenter.getLatitudeE6() - (latSpan/2);
int leftLongitude = mapCenter.getLongitudeE6() - (latSpan/2);
int rightLongitude = mapCenter.getLongitudeE6() + (latSpan/2);
But this values:
int latSpan = mapView.getLatitudeSpan();
int longSpan = mapView.getLongitudeSpan();
are giving me 36000000 and 0 :/
On Jun 25, 2010, at 9:15 PM, Frank Weiss wrote:
Exactly. You need to pass the neLatitude, neLongitude, swLatitude,
swLongitude as query parameters to your back end web service, which
then returns only POIs whose latitude and longitude fall within those
bounds, like this Javascript Google Map does:
http://myhome.bankofamerica.com/?findmlo=94114#/findmlo/94105
Notice that it even handles panning and zooming.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to
[email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
Pedro Teixeira
www.pedroteixeira.org <http://www.pedroteixeira.org>
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to
[email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
Sincerely,
Brad Gies
-----------------------------------------------------------------------
Bistro Bot - Bistro Blurb
http://www.bgies.com
http://www.bistroblurb.com
http://www.ihottonight.com
-----------------------------------------------------------------------
Never doubt that a small group of thoughtful, committed people can
change the world. Indeed. It is the only thing that ever has -
Margaret Mead
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
Pedro Teixeira
www.pedroteixeira.org
--
Sincerely,
Brad Gies
-----------------------------------------------------------------------
Bistro Bot - Bistro Blurb
http://www.bgies.com
http://www.bistroblurb.com
http://www.ihottonight.com
-----------------------------------------------------------------------
Never doubt that a small group of thoughtful, committed people can
change the world. Indeed. It is the only thing that ever has - Margaret Mead
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en