使用CoreLocation在iPhone App中自定义映射功能

时间:2010-12-16 20:15:56

标签: iphone mapping core-location trigonometry

我正在我的应用程序中开发一些自定义映射功能,并且遇到了绊脚石。

此地图适用于已知区域(即我知道左上角和右下角的确切纬度/经度),我有一个区域的图形图像,可以缩放,我用作背景(我不是我想为此使用MKMapView。

我正在使用CoreLocation向我提供我的纬度/经度,并且我正在尝试将这些“真正的”坐标转换为自定义地图上的某个位置。

需要注意的一点是,我的自定义地图(显示在UIScrollView中)从北向一定角度旋转(即自定义地图的顶部不等于北),因此,我需要翻译/旋转通过CoreLocation返回的所有坐标。

我正在使用以下代码尝试执行此操作:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    CLLocation *pTL = [self rotateLocation:self.topLeftCorner
                          aboutLocation:newLocation];

    CLLocation *pBR = [self rotateLocation:self.bottomRightCorner
                         aboutLocation:newLocation];

    double mapCoordX = ((pTL.coordinate.longitude - newLocation.coordinate.longitude) / (pTL.coordinate.longitude - pBR.coordinate.longitude)) * imageWidth;
    double mapCoordY = ((pTL.coordinate.latitude - newLocation.coordinate.latitude) / (pTL.coordinate.latitude - pBR.coordinate.latitude)) * imageHeight;

    if ((mapCoordX >= -self.beaconImage.frame.size.width && mapCoordX <= imageWidth + self.beaconImage.frame.size.width) && (mapCoordY >= -self.beaconImage.frame.size.height && mapCoordY <= imageHeight + self.beaconImage.frame.size.height)) {

        self.beaconImage.center = CGPointMake(mapCoordX, mapCoordY);
        self.beaconImage.hidden = NO;

    } else {

        UIAlertView *av = [[UIAlertView alloc] 
                       initWithTitle:nil
                       message:@"You're not within the boundaries of the map"
                       delegate:self
                       cancelButtonTitle:@"OK"
                       otherButtonTitles:nil];
        [av show];
        [av release];

        self.beaconImage.hidden = YES;

    }

}   

- (CLLocation *)rotateLocation:(CLLocation *)rotationLocation aboutLocation:(CLLocation *)baseLocation {

    double mapLatitudeOffset = topLeftCorner.coordinate.latitude + bottomRightCorner.coordinate.latitude;

    double mapLongitudeOffset = topLeftCorner.coordinate.longitude - bottomRightCorner.coordinate.longitude;

    double mapOffsetAngle = 0;

    if (mapLongitudeOffset == 0 && mapLatitudeOffset == 0) {
        mapOffsetAngle = 0;
    } else if (mapLongitudeOffset >= 0) {
        mapOffsetAngle = atan2(mapLongitudeOffset, mapLatitudeOffset);
    } else {
        mapOffsetAngle = -atan2(mapLongitudeOffset, mapLatitudeOffset) + M_PI;
    }

    double latitudeOffset = (rotationLocation.coordinate.latitude + (baseLocation.coordinate.latitude * -1));

    double longitudeOffset = (rotationLocation.coordinate.longitude + (baseLocation.coordinate.longitude * -1));

    double radius = sqrt(pow(latitudeOffset, 2) + pow(longitudeOffset, 2));

    double theta = 0;

    if (longitudeOffset == 0 && latitudeOffset == 0) {
        theta = 0;
    } else if (longitudeOffset >= 0) {
        theta = atan2(longitudeOffset, latitudeOffset);
    } else {
        theta = -atan2(longitudeOffset, latitudeOffset) + M_PI;
    }

    double angularOffset = theta + mapOffsetAngle;

    double latitudeRotate = radius * sin(angularOffset);

    double longitudeRotate = radius * cos(angularOffset);

    return [[[CLLocation alloc] initWithLatitude:baseLocation.coordinate.latitude + latitudeRotate longitude:baseLocation.coordinate.longitude + longitudeRotate] autorelease];

}

不幸的是,计算出的值不正确,因此我用来指示我当前位置的信标图像显示在地图的错误区域。

这似乎是一个直截了当的问题,所以我做错了什么?

0 个答案:

没有答案