0

我希望你能理解我的问题,我的英语不太好。

无论如何,我正在开发一个带有位置的应用程序。它只是一个有趣的应用程序,应该可以帮助我了解更多信息。

它是一个 iOS 应用程序,服务器是一个 WebObjects/WOnder 应用程序(Java)。我想做的是在 iOS 应用程序上获取用户位置,然后将数据发送到服务器。在服务器上,我从数据库中获取注释点。但只发回用户位置附近的注释。

我现在唯一的问题是我不知道如何计算用户附近的位置。我用谷歌搜索了很多,但没有找到有用的东西。只有给我用户的“边界框”的东西。

//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];

有没有人知道如何在 Java 中做到这一点?

4

1 回答 1

1

我使用 c# 代码来计算距离。我枚举数据库中的所有数据,如果数据在我的距离范围内,我将其添加到数组中,然后将其传递给设备。在我的代码中,我为用户当前位置提供 lat 和 lon,为数据库中的对象提供 lat 和 lon。我还提供单位 K - km 和 M - 英里。这是 ac# 代码,但您可以轻松地将其转换为 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;
    }
于 2013-12-03T09:14:52.753 回答