Question

I hope you understand my question my english is not too good.

Anyway, I’m working on a application with locations. Its only a fun app which should help me to learn more.

Its a iOS application and the server is a WebObjects/WOnder application(Java). What im trying to do is on the iOS app I fetch the user location then send the data to the server. And on the server I fetch annotation points from a database. but only send the annotations which are in near of the users location back.

My only problem now is I don’t know how to calculate the locations in the near of the user. I googled a lot but did not find something that work. Only something which give me the „bounding box“ of the user.

//To calculate the search bounds...
//First we need to calculate the corners of the map so we get the points
CGPoint nePoint = CGPointMake(self.mapView.bounds.origin.x + mapView.bounds.size.width, mapView.bounds.origin.y);
CGPoint swPoint = CGPointMake((self.mapView.bounds.origin.x), (mapView.bounds.origin.y + mapView.bounds.size.height));

//Then transform those point into lat,lng values
CLLocationCoordinate2D neCoord;
neCoord = [mapView convertPoint:nePoint toCoordinateFromView:mapView];

CLLocationCoordinate2D swCoord;
swCoord = [mapView convertPoint:swPoint toCoordinateFromView:mapView];

Dose anyone have a good idea how to do that in Java?

Was it helpful?

Solution

I use c# code to calculate the distance. I enumerate all of the data in the database and if the data is in range of my distance I add it to the array and after that I pass it to the device. In my code I provide lat and lon for user current position and lat and lon for object from database. I also provide unit K - km and M - miles. This is a c# code but you can easy convert it to java:

    public double GetDistanceFromLatLong(double lat1, double lon1, double lat2, double lon2, string unit)
    {
        double ReturnValue = 0;
        double theta = 0;
        double dist = 0;
        theta = lon1 - lon2;
        dist = Math.Sin(DegreesToRadians(lat1)) * Math.Sin(DegreesToRadians(lat2)) + Math.Cos(DegreesToRadians(lat1)) *
               Math.Cos(DegreesToRadians(lat2)) * Math.Cos(DegreesToRadians(theta));
        dist = Math.Acos(dist);
        dist = RadiansToDegrees(dist);
        ReturnValue = dist * 60 * 1.1515;

        switch (unit.ToUpper())
        {
            case "K":
                ReturnValue = ReturnValue * 1.609344;
                break;
            case "M":
                ReturnValue = ReturnValue * 0.8684;
                break;
        }
        return ReturnValue;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top