Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7566523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:26:13+00:00 2026-05-30T14:26:13+00:00

I’ve been researching this on Google and SO but I’m stuck, I think I’m

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:26:14+00:00Added an answer on May 30, 2026 at 2:26 pm

    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 listingDisplay is an array of database-retrieved custom LocationNode objects, and the GeoPoint current is created directly from a returned Address of a standard Android Geocoder.

    public void setRegionForGeoPoint(GeoPoint current) {
    
        // Earth radius in KM
        final double EARTH_RADIUS = 6371.01;
    
        // Dummy span distance in KM for initial search; distance buffer is in M
        final double DISTANCE_BUFFER = 50;
        final double dummyDistance = 100.0;
    //Create a list to store distances
        List<Double> distancesList = new ArrayList<Double>();
    
    
        // Loop through and modify LocationNodes with distance from user
        for (LocationNode location : listingDisplay) {
            location.setDistance((float) distanceFromUser(location));
    
            // Dynamically calculate distance from our current point (epicentre)
            distancesList.add(distanceFromPoint(location, current));
        }
    
        // Sort distances
        Collections.sort(distancesList);
    
        // Calculate regional span
        float spanWidth = (float) dummyDistance;
        double distance = 0;
    
        if (distancesList.size() > 0) {
            if (distancesList.size() > 1) {
    
                distance = distancesList.get(1);
                spanWidth = (float) (distance + DISTANCE_BUFFER);
    
            } else if (distancesList.size() == 1) {
    
                distance = distancesList.get(0);
                spanWidth = (float) (distance + DISTANCE_BUFFER);
    
            }
    
            //Obtain the spanwidth in metres.
            double spanWidthInKM = (double) spanWidth / 1000;
    
            // Create span
            GeoLocation[] boundingBoxSpan = currentGeoLocation
                    .boundingCoordinates(spanWidthInKM, EARTH_RADIUS);
    
            //Create min/max values for final span calculation
            int minLatSpan = (int) (boundingBoxSpan[0].getLatitudeInDegrees() * 1E6);
            int maxLatSpan = (int) (boundingBoxSpan[1].getLatitudeInDegrees() * 1E6);
            int minLongSpan = (int) (boundingBoxSpan[0].getLongitudeInDegrees() * 1E6);
            int maxLongSpan = (int) (boundingBoxSpan[1].getLongitudeInDegrees() * 1E6);
    
            //Finally calculate span
            int latSpanE6 = Math.abs(minLatSpan - maxLatSpan);
            int lonSpanE6 = Math.abs(minLongSpan - maxLongSpan);
    
            // Set center
            mapController.setCenter(current);
    
            // Zoom to span
            mapController.zoomToSpan(latSpanE6, lonSpanE6);
    
        } else {
    
            //TODO: Handle the case when we have no distance values to use
        }
    }
    
    
    public double distanceFromPoint(LocationNode location, GeoPoint point) {
    
        // Calculate distance from user via result
        Location locationA = new Location("point A");
        locationA.setLatitude(location.getLatitude());
        locationA.setLongitude(location.getLongitude());
    
        Location locationB = new Location("point B");
        locationB.setLatitude((double) (point.getLatitudeE6() / 1E6));
        locationB.setLongitude((double) (point.getLongitudeE6() / 1E6));
        double distance = locationA.distanceTo(locationB);
        Log.v(TAG, "Calculated Distance: " + distance);
        return distance;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.