1

How to Pass latitude and Longitude values form CLLocation Manager to My CustomView. I have A ViewController With CLLocationManger where iam getting the Locations, I have Another UIView Class with drawRect Where i have Divided the View with Coordinates as

#define EARTH_RADIUS 6371

- (void)drawRect:(CGRect)rect
{
    CGPoint latLong = {41.998035, -116.012215};

    CGPoint newCoord = [self convertLatLongCoord:latLong];

    NSLog(@"Cartesian Coordinate: (%f, %f)",newCoord.x, newCoord.y);

    //Draw dot at coordinate
    CGColorRef darkColor = [[UIColor colorWithRed:21.0/255.0
                                            green:92.0/255.0
                                             blue:136.0/255.0
                                            alpha:1.0] CGColor];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, darkColor);
    CGContextFillRect(context, CGRectMake(newCoord.x, newCoord.y, 100, 100));

}

-(CGPoint)convertLatLongCoord:(CGPoint)latLong
{
    CGFloat x = EARTH_RADIUS * cos(latLong.x) * cos(latLong.y);
    CGFloat y = EARTH_RADIUS * cos(latLong.x) * sin(latLong.y);

    return CGPointMake(x, y);
}

I have Took the CGPoint latLong = {41.998035, -116.012215} static.

Can you say me How to Pass A ViewController Values to UIView Class

rajesh.k
  • 125
  • 1
  • 8

2 Answers2

0

You would generally have a property in you UIView subclass for your coordinate (either using your CGPoint or perhaps a CLLocationCoordinate2D) and then call setNeedsDisplay so that the device knows that it needs to call your drawRect method.

Thus, have a property:

@property (nonatomic) CLLocationCoordinate2D coordinate;

And then update the implementation of your view as such:

- (void)setCoordinate:(CLLocationCoordinate2D)coordinate
{
    _coordinate = coordinate;

    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    CGPoint newCoord = [self convertLatLongCoord:self.coordinate];

    NSLog(@"Cartesian Coordinate: (%f, %f)",newCoord.x, newCoord.y);

    //Draw dot at coordinate
    CGColorRef darkColor = [[UIColor colorWithRed:21.0/255.0
                                            green:92.0/255.0
                                             blue:136.0/255.0
                                            alpha:1.0] CGColor];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, darkColor);
    CGContextSetStrokeColorWithColor(context, darkColor);
    CGContextFillRect(context, CGRectMake(newCoord.x, newCoord.y, 100.0, 100.0));
}

// I assume you're using the algorithm from here: https://stackoverflow.com/a/1185413/1271826

- (CGPoint)convertLatLongCoord:(CLLocationCoordinate2D)coordinate
{
    CGFloat x = EARTH_RADIUS * cos(coordinate.latitude * M_PI / 180.0) * cos(coordinate.longitude * M_PI / 180.0);
    CGFloat y = EARTH_RADIUS * cos(coordinate.latitude * M_PI / 180.0) * sin(coordinate.longitude * M_PI / 180.0);

    // TODO: The above calculates x and y values from -EARTH_RADIUS to EARTH_RADIUS.
    // You presumably want to scale this appropriately for your view. The
    // specifics will vary based upon your desired user interface.

    return CGPointMake(x, y);
}

Then, when you want to update your view, you can do something like:

myCustomView.coordinate = CLLocationCoordinate2DMake(41.998035, -116.012215);

While I've tried to remedy some flaws in convertLatLongCoord, it's still not perfect, as it will yield values ranging from -EARTH_RADIUS to EARTH_RADIUS (which obviously won't work for a CGRect of a UIView, which is expecting values between zero and the width/height of the view).

I assume the underlying algorithm is one like discussed here: Converting from longitude\latitude to Cartesian coordinates. I've tried to fix the degrees to radians conversion needed by cos and sin, you still need to scale this and translate it to values appropriate for what portion of the map/radar you're intending to show.

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • I want to Update the Location as the Device Moves. its Without map. – rajesh.k May 12 '13 at 14:40
  • @rajesh.k - Understood, but I don't think that `convertLatLongCoord` will achieve whatever you think it does. Play around with it and you'll see what I mean. – Rob May 12 '13 at 14:43
  • Check this, i want to locate the boat without map. https://play.google.com/store/apps/details?id=com.terboel.myanchorwatch – rajesh.k May 12 '13 at 14:55
  • How to Draw Continuous line Connecting Locations on UIView – rajesh.k May 12 '13 at 14:59
  • Ok, but your algorithm has two problems: First, you need to convert your latitude an longitude to radians (e.g. `coordinate.latitude * M_PI / 180.0`). To use radian based `cos` and `sin` functions with latitude and longitudes measured in degrees will not render the result you're looking for. Second, even if you do that, you'll get values ranging from `-EARTH_RADIUS` to `EARTH_RADIUS`. So you have to scale that to coordinates appropriate for your view. – Rob May 12 '13 at 15:10
  • Yes Thanks, Can you say me Please How to do it..? – rajesh.k May 12 '13 at 15:22
  • @rajesh.k Seems like simple algebra to me: First, you need to know the range of cartesian coordinates to be shown by the view. Let's say you have a `CGRect` of cartesian coordinates of the current view called, say `viewCartesianRect`. Then, second, you could probably calculate the `x` and `y` coordinates on the view of some given the `CGPoint` cartesian coordinates of the point being considered, e.g. `pointCartesian`, and the resulting view coordinates would be `(pointCartesian.x - viewCartesian.origin.x) / viewCartesian.size.width * self.frame.width` (and repeat that for the height). – Rob May 12 '13 at 15:46
  • @rajesh.k If this is not obvious, I'd suggest that you (a) consider accepting my answer by clicking on the checkmark next to my answer (as I think I've answered the original question); and (b) you post a new question with your cartesian mapping question, as that's very different topic from your original question. – Rob May 12 '13 at 15:48
0

Based on the comment that you want to "find your boat" it sounds like you want the heading from your current location to a specified location (your boat). That calculation is this

+ (float)getHeadingForDirectionFromCoordinate:(CLLocationCoordinate2D)fromLoc toCoordinate:(CLLocationCoordinate2D)toLoc
{
    float fLat = degreesToRadians(fromLoc.latitude);
    float fLng = degreesToRadians(fromLoc.longitude);
    float tLat = degreesToRadians(toLoc.latitude);
    float tLng = degreesToRadians(toLoc.longitude);

    float degree = radiansToDegrees(atan2(sin(tLng-fLng)*cos(tLat), cos(fLat)*sin(tLat)-sin(fLat)*cos(tLat)*cos(tLng-fLng)));

    if (degree >= 0) {
        return degree;
    } else {
        return 360+degree;
    }
}
software evolved
  • 4,314
  • 35
  • 45