当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Getting addresses or LatLng within a radius of current location

问题描述:

I am developing an app where it would show "events" that is within a 50 meter radius of the current location of the user. I would get these events from an online database. These events contain a LatLng and an address from the DB.

I already implemented location updates.

My question is that what approach would be better?

One approach:

I draw circle on the current location using addCircle. Then remove and redraw these circles when the current location changes.

The hard part about this is how would I get the locations or lat/lang within 50 meters.

I would then query my DB and return my events.

Another approach I've considered is Geofences. I am still studying it right now.

Maybe I would draw a geofence around my current location and update it as needed.

网友答案:

You need a function called

InRadius(centerX, centerY, radius, x, y) 
{
Return (radius * 111000f) >= sqrt((x-centerX )^2 +(y-centerY) ^2) 
} 

Center is user location. But you must call on every record in event table, feeding the lat into y and the long into x

网友答案:
public static LatLng getLocation(double lon, double lat, int radius) 
{
    Random random = new Random();

    // Convert radius from meters to degrees
    double radiusInDegrees = radius / 111000f;

    double u = random.nextDouble();
    double v = random.nextDouble();
    double w = radiusInDegrees * Math.sqrt(u);
    double t = 2 * Math.PI * v;
    double x = w * Math.cos(t);
    double y = w * Math.sin(t);

    // Adjust the x-coordinate for the shrinking of the east-west distances
    double new_x = x / Math.cos(lat);

    double foundLongitude = new_x + lon;
    double foundLatitude = y + lat;
    System.out.println("Longitude: " + foundLongitude + "  Latitude: "
            + foundLatitude);

    return new LatLng(foundLatitude, foundLongitude);

}

Then call function where you want. :)

private LatLng latlon1;

    latlon1 = getLocation(location.getLongitude(),location.getLatitude(),50);

->location = your current location
->50 = radius what ever you want to give

hope you get your answer :)

分享给朋友:
您可能感兴趣的文章:
随机阅读: