I’ve been researching this on Google and SO but I’m stuck, I think I’m missing something fundamental. Most examples I’ve seen don’t deal with an arbitrary mapWidth and a single point, just the span of an Overlay.
I have a database of map points, a MapView and a Geocoder. I can search for a postcode in my app, and have an Address returned by my Geocoder.
Using this Address, I can build a GeoPoint and search my DB and get back a list of nearby points. The issue comes from trying to zoomToSpan using a span constructed from from the returned Address point and the distance to the nearest point in the database.
I only want the span to encompass the nearest two points (if available). Here’s the relevant code:
Collections.sort(listingDisplay, mComparator);
listingDisplayAdapter.notifyDataSetChanged();
float spanWidth =0;
if (listingDisplay.size() > 1) {
spanWidth = (float) (2 * distanceFromPoint(listingDisplay.get(1),
current));
} else if (listingDisplay.size() == 1) {
spanWidth = (float) (2 * distanceFromPoint(listingDisplay.get(0),
current));
}
Log.v(TAG, "SpanWidth: " + spanWidth);
// Create span
int minLat = (int) (current.getLatitudeE6() - (spanWidth * 1E6) / 2);
int maxLat = (int) (current.getLatitudeE6() + (spanWidth * 1E6) / 2);
int minLong = (int) (current.getLongitudeE6() - (spanWidth * 1E6) / 2);
int maxLong = (int) (current.getLongitudeE6() + (spanWidth * 1E6) / 2);
// Zoom against span. This appears to create a very small region that doesn't encompass the points
mapController.setCenter(current);
mapController.zoomToSpan(Math.abs( minLat - maxLat ), Math.abs( minLong - maxLong ));
ListingDisplay contains a list of the closest points, with a comparator, mComparator sorting this list with the closest locations to my returned Address (the GeoPoint called: current) at the top of the list .
I then set the value of spanWidth based on the closest, and try to figure out the span from this.
My question is, how can I construct a span from a given distance and centerpoint?
After a very, very long time, I eventually realized that I wasn’t considering some important information, chiefly among them was the fact that distances on Android are calculated using the WGS84 ellipsoid.
I ended up using the helper methods inside Jan Matuschek’s excellent and simple GeoLocation class, which comes with a very thorough explanation of the concepts involved.
My method essentially boiled down to the following. It can probably be optimized a good deal, down to a simple SQL query, but here it is for my purposes, where
listingDisplayis an array of database-retrieved customLocationNodeobjects, and theGeoPointcurrent is created directly from a returnedAddressof a standard Android Geocoder.