Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You would generally have a property in you <code>UIView</code> subclass for your coordinate (either using your <code>CGPoint</code> or perhaps a <code>CLLocationCoordinate2D</code>) and then call <code>setNeedsDisplay</code> so that the device knows that it needs to call your <code>drawRect</code> method.</p> <p>Thus, have a property:</p> <pre><code>@property (nonatomic) CLLocationCoordinate2D coordinate; </code></pre> <p>And then update the implementation of your view as such:</p> <pre><code>- (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); } </code></pre> <p>Then, when you want to update your view, you can do something like:</p> <pre><code>myCustomView.coordinate = CLLocationCoordinate2DMake(41.998035, -116.012215); </code></pre> <p>While I've tried to remedy some flaws in <code>convertLatLongCoord</code>, it's still not perfect, as it will yield values ranging from <code>-EARTH_RADIUS</code> to <code>EARTH_RADIUS</code> (which obviously won't work for a <code>CGRect</code> of a <code>UIView</code>, which is expecting values between zero and the width/height of the view). </p> <p>I assume the underlying algorithm is one like discussed here: <a href="https://stackoverflow.com/questions/1185408/converting-from-longitude-latitude-to-cartesian-coordinates/1185413#1185413">Converting from longitude\latitude to Cartesian coordinates</a>. I've tried to fix the degrees to radians conversion needed by <code>cos</code> and <code>sin</code>, you still need to scale this and translate it to values appropriate for what portion of the map/radar you're intending to show.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload