## Zoom and span on a cluster of points

Tutorials that use the MapActivity. Many using GPS functionality.

### Zoom and span on a cluster of points

I have been playing around with zooming and spanning in order to fit a list of Points on the view. Here is an exemple (not complete):

First off, created 4 int variables:

Using java Syntax Highlighting
1.
2.         // Minimum & maximum latitude so we can span it
3.
4.         // The latitude is clamped between -80 degrees and +80 degrees inclusive
5.
6.         // thus we ensure that we go beyond that number
7.
8.         private int minLatitude = (int)(+81 * 1E6);
9.
10.         private int maxLatitude = (int)(-81 * 1E6);
11.
12.
13.
14.         // Minimum & maximum longitude so we can span it
15.
16.         // The longitude is clamped between -180 degrees and +180 degrees inclusive
17.
18.         // thus we ensure that we go beyond that number
19.
20.         private int minLongitude  = (int)(+181 * 1E6);;
21.
22.         private int maxLongitude  = (int)(-181 * 1E6);;
23.
24.
Parsed in 0.011 seconds, using GeSHi 1.0.8.4

I created a function to setup the map:

Using java Syntax Highlighting
1.
2.         /*
3.
4.          * Basic Map setup
5.
6.          */
7.
8.         private void setupMap(){
9.
10.
11.
12.                 // Holds all the picture location as Point
13.
14.                 List<Point> mPoints = new ArrayList<Point>();
15.
16.
17.
18.                 while (mCursor.next()) {
19.
20.
21.
22. //You get the latitude/longitude as you want, here I take it from the db
23.
24.                         int latitude = mCursor.getInt(LATITUDE_INDEX);
25.
26.                         int longitude = mCursor.getInt(LONGITUDE_INDEX);
27.
28.
29.
30.                         // Sometimes the longitude or latitude gathering
31.
32.                         // did not work so skipping the point
33.
34.                         // doubt anybody would be at 0 0
35.
36.                         if (latitude != 0 && longitude !=0)  {
37.
38.
39.
40.                                 // Sets the minimum and maximum latitude so we can span and zoom
41.
42.                                 minLatitude = (minLatitude > latitude) ? latitude : minLatitude;
43.
44.                                 maxLatitude = (maxLatitude < latitude) ? latitude : maxLatitude;
45.
46.
47.
48.                                 // Sets the minimum and maximum latitude so we can span and zoom
49.
50.                                 minLongitude = (minLongitude > longitude) ? longitude : minLongitude;
51.
52.                                 maxLongitude = (maxLongitude < longitude) ? longitude : maxLongitude;
53.
54.
55.
57.
58.                         }
59.
60.                 }
61.
62.
63.
64.                 // Get the controller
65.
66.                 mMapController = mMapView.getController();
67.
68.
69.
70.                 // Zoom to span from the list of points
71.
72.                 mMapController.zoomToSpan(
73.
74.                                 (maxLatitude - minLatitude),
75.
76.                                 (maxLongitude - minLongitude));
77.
78.
79.
80.                 // Animate to the center cluster of points
81.
82.                 mMapController.animateTo(new Point(
83.
84.                                  (maxLatitude + minLatitude)/2,
85.
86.                                  (maxLongitude + minLongitude)/2 ));
87.
88.
89.
90.                 // Add all the point to the overlay
91.
92.                 mMapOverlay = new MyPhotoMapOverlay(mPoints);
93.
94.                 mMapOverlayController = mMapView.createOverlayController();
95.
96.
97.
98.                 // Add the overlay to the mapview
99.
101.
102.         }
103.
104.
Parsed in 0.012 seconds, using GeSHi 1.0.8.4

That is about it. I will not go into details of the overlay and mapView setup etc... But this should help out on zooming and spanning to the correct level and position.

./C
charroch
Freshman

Posts: 7
Joined: Thu Dec 06, 2007 1:08 pm

Hello Charroch,

thx for sharing this useful snippet.

Regards,
plusminus

| Android Development Community / Tutorials

plusminus

Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Hi,

I am trying to do pretty much the same in my application, but my points are moving dynamically on the screen.
As you did I calculated the midpoint of the cluster by averaging latitude and longitude, but I actually found out that this midpoint is not the same of the actual screen coordinate midpoint (obtained by averaging all points that needs to be drawn in screen coordinates). The offset between the two points is very small if the points are near on the screen but grows when the cluster spreads.

Do you know a way to center the map on a screen coordinate point?

In the attached shot you can see the two midpoints, the geo one (in black) and the screen coordinate one (in yellow) for a cluster of two points (green and red).
By the way, the city in the map is Turin in Italy if you're curious
Attachments
map.jpg (39.92 KiB) Viewed 24856 times
s.semeria
Junior Developer

Posts: 23
Joined: Mon Jan 28, 2008 10:18 am
Location: Italy

charroch, thank you for the code, I just used it and it seems to work fine with 2 points. Have you seen any flaws with it? With how many points have you tried this? Also, s.semeria, I don't see why this wouldn't work with dynamic points, If you constantly update the minimum and maximum lat & long. Anyone has any other ideas on this matter?
yorgosg
Developer

Posts: 35
Joined: Wed Jan 30, 2008 9:53 am

### Re: Zoom and span on a cluster of points

charroch wrote:I have been playing around with zooming and spanning in order to fit a list of Points on the view. Here is an exemple (not complete):

First off, created 4 int variables:

Using java Syntax Highlighting
1.         // Minimum & maximum latitude so we can span it
2.         // The latitude is clamped between -80 degrees and +80 degrees inclusive
3.         // thus we ensure that we go beyond that number
4.         private int minLatitude = (int)(+81 * 1E6);
5.         private int maxLatitude = (int)(-81 * 1E6);
6.
7.         // Minimum & maximum longitude so we can span it
8.         // The longitude is clamped between -180 degrees and +180 degrees inclusive
9.         // thus we ensure that we go beyond that number
10.         private int minLongitude  = (int)(+181 * 1E6);;
11.         private int maxLongitude  = (int)(-181 * 1E6);;
12.
Parsed in 0.013 seconds, using GeSHi 1.0.8.4

I created a function to setup the map:

Using java Syntax Highlighting
1.         /*
2.          * Basic Map setup
3.          */
4.         private void setupMap(){
5.
6.                 // Holds all the picture location as Point
7.                 List<Point> mPoints = new ArrayList<Point>();
8.
9.                 while (mCursor.next()) {
10.
11. //You get the latitude/longitude as you want, here I take it from the db
12.                         int latitude = mCursor.getInt(LATITUDE_INDEX);
13.                         int longitude = mCursor.getInt(LONGITUDE_INDEX);
14.
15.                         // Sometimes the longitude or latitude gathering
16.                         // did not work so skipping the point
17.                         // doubt anybody would be at 0 0
18.                         if (latitude != 0 && longitude !=0)  {
19.
20.                                 // Sets the minimum and maximum latitude so we can span and zoom
21.                                 minLatitude = (minLatitude > latitude) ? latitude : minLatitude;
22.                                 maxLatitude = (maxLatitude < latitude) ? latitude : maxLatitude;
23.
24.                                 // Sets the minimum and maximum latitude so we can span and zoom
25.                                 minLongitude = (minLongitude > longitude) ? longitude : minLongitude;
26.                                 maxLongitude = (maxLongitude < longitude) ? longitude : maxLongitude;
27.
29.                         }
30.                 }
31.
32.                 // Get the controller
33.                 mMapController = mMapView.getController();
34.
35.                 // Zoom to span from the list of points
36.                 mMapController.zoomToSpan(
37.                                 (maxLatitude - minLatitude),
38.                                 (maxLongitude - minLongitude));
39.
40.                 // Animate to the center cluster of points
41.                 mMapController.animateTo(new Point(
42.                                  (maxLatitude + minLatitude)/2,
43.                                  (maxLongitude + minLongitude)/2 ));
44.
45.                 // Add all the point to the overlay
46.                 mMapOverlay = new MyPhotoMapOverlay(mPoints);
47.                 mMapOverlayController = mMapView.createOverlayController();
48.
49.                 // Add the overlay to the mapview
51.         }
52.
Parsed in 0.014 seconds, using GeSHi 1.0.8.4

That is about it. I will not go into details of the overlay and mapView setup etc... But this should help out on zooming and spanning to the correct level and position.

./C

Hey guys! Great site...and a wealth of information.

I was goofing around today and tried this code using anywhere from 10 to 40 points and needless to say it would not zoom properly. The min values and max values in my case are all very close...once changed to int...the max and min values are the same. So, for instance the min point (lat) would be 27.4386958 ends up 27 and furthest point (27.4386999) is also 27. The same happens for the longitude values. So I end up with a max/min of 27/27 and 87/87. The zoom in this case does not encompass all the points.

Any thoughts?
starrman73
Freshman

Posts: 2
Joined: Thu Jan 22, 2009 10:50 pm
Location: Texas

Everyone's suggestions were really buggy for me, here is my version with improved math.

Code: Select all
`mapController.zoomToSpan(    (locationPoint.getLatitudeE6() > mapPoint.getLatitudeE6()        ? locationPoint.getLatitudeE6() - mapPoint.getLatitudeE6()        : mapPoint.getLatitudeE6() - locationPoint.getLatitudeE6()),     (locationPoint.getLongitudeE6() > mapPoint.getLongitudeE6()        ? locationPoint.getLongitudeE6() - mapPoint.getLongitudeE6()        : mapPoint.getLongitudeE6() - locationPoint.getLongitudeE6()));    mapController.animateTo(        new GeoPoint(            locationPoint.getLatitudeE6() - ((locationPoint.getLatitudeE6() - mapPoint.getLatitudeE6())/2),              locationPoint.getLongitudeE6() - ((locationPoint.getLongitudeE6() - mapPoint.getLongitudeE6())/2)        ));`
_tek_
Freshman

Posts: 2
Joined: Sun Mar 01, 2009 12:43 am

### Re: Zoom and span on a cluster of points

charroch wrote:I have been playing around with zooming and spanning in order to fit a list of Points on the view. Here is an exemple (not complete):

First off, created 4 int variables:

Using java Syntax Highlighting
1.         // Minimum & maximum latitude so we can span it
2.         // The latitude is clamped between -80 degrees and +80 degrees inclusive
3.         // thus we ensure that we go beyond that number
4.         private int minLatitude = (int)(+81 * 1E6);
5.         private int maxLatitude = (int)(-81 * 1E6);
6.
7.         // Minimum & maximum longitude so we can span it
8.         // The longitude is clamped between -180 degrees and +180 degrees inclusive
9.         // thus we ensure that we go beyond that number
10.         private int minLongitude  = (int)(+181 * 1E6);;
11.         private int maxLongitude  = (int)(-181 * 1E6);;
12.
Parsed in 0.011 seconds, using GeSHi 1.0.8.4

I created a function to setup the map:

Using java Syntax Highlighting
1.         /*
2.          * Basic Map setup
3.          */
4.         private void setupMap(){
5.
6.                 // Holds all the picture location as Point
7.                 List<Point> mPoints = new ArrayList<Point>();
8.
9.                 while (mCursor.next()) {
10.
11. //You get the latitude/longitude as you want, here I take it from the db
12.                         int latitude = mCursor.getInt(LATITUDE_INDEX);
13.                         int longitude = mCursor.getInt(LONGITUDE_INDEX);
14.
15.                         // Sometimes the longitude or latitude gathering
16.                         // did not work so skipping the point
17.                         // doubt anybody would be at 0 0
18.                         if (latitude != 0 && longitude !=0)  {
19.
20.                                 // Sets the minimum and maximum latitude so we can span and zoom
21.                                 minLatitude = (minLatitude > latitude) ? latitude : minLatitude;
22.                                 maxLatitude = (maxLatitude < latitude) ? latitude : maxLatitude;
23.
24.                                 // Sets the minimum and maximum latitude so we can span and zoom
25.                                 minLongitude = (minLongitude > longitude) ? longitude : minLongitude;
26.                                 maxLongitude = (maxLongitude < longitude) ? longitude : maxLongitude;
27.
29.                         }
30.                 }
31.
32.                 // Get the controller
33.                 mMapController = mMapView.getController();
34.
35.                 // Zoom to span from the list of points
36.                 mMapController.zoomToSpan(
37.                                 (maxLatitude - minLatitude),
38.                                 (maxLongitude - minLongitude));
39.
40.                 // Animate to the center cluster of points
41.                 mMapController.animateTo(new Point(
42.                                  (maxLatitude + minLatitude)/2,
43.                                  (maxLongitude + minLongitude)/2 ));
44.
45.                 // Add all the point to the overlay
46.                 mMapOverlay = new MyPhotoMapOverlay(mPoints);
47.                 mMapOverlayController = mMapView.createOverlayController();
48.
49.                 // Add the overlay to the mapview
51.         }
52.
Parsed in 0.013 seconds, using GeSHi 1.0.8.4

That is about it. I will not go into details of the overlay and mapView setup etc... But this should help out on zooming and spanning to the correct level and position.

./C

Is it posible to show information about the scale?

xavi911
Freshman

Posts: 2
Joined: Wed May 12, 2010 10:59 am
Location: spain