I have a collection of CGPoints obtained from converting geographic coordinates obtained form Core Location frame work delegate method. The collection is a set of start/end points.
I am obtaining the coordinates from CoreLocation delegate method
CLLocationCoordinate2D coordinate;
CLLocationDegrees latitude;
CLLocationDegrees longitude;
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation *location = [locations lastObject];
latitude = location.coordinate.latitude;
longitude = location.coordinate.longitude;
}
I am converting the current latitude and longitude to CGPoint as below
CGPoint startPoint = [mapView convertCoordinate:retrievedCoordinate toPointToView:self.view];
CGPoint endPoint = [mapView convertCoordinate:retrievedEndCoordinate toPointToView:self.view];
I need to draw lines on my UIView based on the values in the collection. How can I adjust/scale correctly, the CGPoints with respect to the UIView. UIView frame size is (0, 0, 768, 1004).
This is the scaling I have done
- (CGPoint)convertLatLongCoord:(CGPoint)latLong {
CGSize screenSize = [UIScreen mainScreen].applicationFrame.size;
CGFloat SCALE = MIN(screenSize.width, screenSize.height) / (2.0 * EARTH_RADIUS);
CGFloat OFFSET = MIN(screenSize.width, screenSize.height) / 2.0;
CGFloat x = EARTH_RADIUS * cos(latLong.x) * cos(latLong.y) * SCALE + OFFSET;
CGFloat y = EARTH_RADIUS * cos(latLong.x) * sin(latLong.y) * SCALE + OFFSET;
return CGPointMake(x, y);
}
where #define EARTH_RADIUS 6371
The line drawing is not correct, since somewhere the conversion to CGPoint might be wrong. What am I doing wrong. Help needed.
For convert CLLocationCoordinate2D to CGPoint see answer here - https://stackoverflow.com/a/37276779/2697425
Related
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
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.
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;
}
}
i have difficulties understanding the math behind on how to calculate the circumference of the knots to the center of the circle. i hope you guys can give me some pointer.
the current calculation set the knot img in the middle of pie, will like to shift it nearer to the outer circle like in img 2
thank you for viewing and commenting, any comments are appreciated.
how i wan to it to be.
/** Draw a white knob over the circle **/
-(void) drawTheHandle:(CGContextRef)ctx{
CGContextSaveGState(ctx);
NSLog(#"handleCenterA.x %f",handleCenterA.x);
NSLog(#" handleCenterA.y %f", handleCenterA.y);
[[UIColor colorWithWhite:1.0 alpha:1.0]set];
UIImage *myImage = [UIImage imageNamed:#"clock-marker.png"];
//this will give me the result of image 1
[myImage drawInRect:CGRectMake(handleCenterA.x-35, handleCenterA.y-40, TB_BUTTON_WIDTH, TB_BUTTON_WIDTH)];
//this will give me the result of image 2
[myImage drawInRect:CGRectMake(handleCenterA.x-35, handleCenterA.y-40, TB_BUTTON_WIDTH, TB_BUTTON_WIDTH)];
CGContextRestoreGState(ctx);
}
#pragma mark - Math -
/** Move the Handle **/
-(void)movehandle:(CGPoint)lastPoint{
//Get the center
CGPoint centerPoint = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
//Calculate the direction from a center point and a arbitrary position.
//float currentAngle = AngleFromNorth(centerPoint, lastPoint, NO);
//int angleInt = floor(currentAngle);
//Calculate the direction from the center point to an arbitrary position.
float currentAngle = AngleFromNorth(centerPoint, lastPoint, NO);
int angleInt = 360 - floor(currentAngle);
if (sliderLock == SliderLockedStart) {
self.startAngle = angleInt;
}
//Redraw
[self setNeedsDisplay];
}
- (CGPoint)centerPointFromAngel:(int)angleInt {
CGPoint point = [self pointFromAngle:angleInt];
point.x += TB_BUTTON_WIDTH/2;
point.y += TB_BUTTON_WIDTH/2;
return point;
}
- (CGFloat)distanceBetween:(CGPoint)p1 and:(CGPoint)p2 {
CGFloat xDist = (p2.x - p1.x);
CGFloat yDist = (p2.y - p1.y);
return sqrt((xDist * xDist) + (yDist * yDist));
}
/** Given the angle, get the point position on circumference **/
-(CGPoint)pointFromAngle:(int)angleInt{
//Circle center
CGPoint centerPoint = CGPointMake(self.frame.size.width/2 - (TB_BUTTON_WIDTH/2), self.frame.size.height/2 - (TB_BUTTON_WIDTH/2));
//The point position on the circumference
CGPoint result;
result.y = round(centerPoint.y + radius * sin(ToRad(-angleInt))) ;
result.x = round(centerPoint.x + radius * cos(ToRad(-angleInt)));
return result;
}
//Sourcecode from Apple example clockControl
//Calculate the direction in degrees from a center point to an arbitrary position.
static inline float AngleFromNorth(CGPoint p1, CGPoint p2, BOOL flipped) {
CGPoint v = CGPointMake(p2.x-p1.x,p2.y-p1.y);
float vmag = sqrt(SQR(v.x) + SQR(v.y)), result = 0;
v.x /= vmag;
v.y /= vmag;
double radians = atan2(v.y,v.x);
result = ToDeg(radians);
return (result >=0 ? result : result + 360.0);
}
finally solved it.
/** Given the angle, get the point position on circumference **/
-(CGPoint)pointFromAngle:(int)angleInt{
//Circle center//TB_BUTTON_WIDTH
CGPoint centerPoint = CGPointMake(self.frame.size.width/2 - (TB_BUTTON_WIDTH/2), self.frame.size.height/2 - (TB_BUTTON_WIDTH/2));
//The point position on the circumference radius
CGPoint result;
result.y = round(centerPoint.y + 120 * sin(ToRad(-angleInt))) ;
result.x = round(centerPoint.x + 120 * cos(ToRad(-angleInt)));
NSLog(#"centerPoint %#",NSStringFromCGPoint(centerPoint));
NSLog(#"result %#",NSStringFromCGPoint(result));
return result;
}
I am trying to learn how to use a polyline to connect two points on a map in ios6. First off, I have read over every tutorial on this subject that a simple Google search turns up and can not get polylines to work for one reason. Every tutorial that I have seen always adds the polyline to the map and adjusts the zoom of the map to fit the whole line. How would I go about making and adding a polyline to a map in ios6 if I want the map to stay zoomed in at a constant distance and only show the end of the polyline if it is larger then the current view?
For example say I had a polyline that was a mile long and wanted the map to stay zoomed in at a constand distacne equivelent to:
MKCoordinateRegion userRegion = MKCoordinateRegionMakeWithDistance(self.currentLocation.coordinate, 1000, 1000);
[self.mainMap setRegion:[self.mainMap regionThatFits:userRegion] animated:YES];
How would I go about doing this? Please provide full code examples or a sample project that I could download!
MKMapPoint * malloc / assign:
MKMapPoint *newPoints = malloc((sizeof (MKMapPoint) * nbPoints));
newPoints[index] = varMKMapPoint;
free(newPoints);
MKPolyline must be initialize in your needed :
MKPolyline *polyline = [MKPolyline polylineWithPoints:newPoints count:nbPoints];
[self.mapView addOverlay:polyline];
to display your MKPolyline, you have to use viewForOverlay :
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MKOverlayView* overlayView = [[MKOverlayView alloc] initWithOverlay:overlay];
if([overlay isKindOfClass:[MKPolyline class]]) {
MKPolylineView *backgroundView = [[MKPolylineView alloc] initWithPolyline:overlay];
backgroundView.fillColor = [UIColor blackColor];
backgroundView.strokeColor = backgroundView.fillColor;
backgroundView.lineWidth = 10;
backgroundView.lineCap = kCGLineCapSquare;
overlayView = backgroundView;
}
return overlayView;
}
To use this method, you have to convert your points to CLLocation, it will returns a MKCoordinateRegion that you will set to mapView :
- (MKCoordinateRegion)getCenterRegionFromPoints:(NSArray *)points
{
CLLocationCoordinate2D topLeftCoordinate;
topLeftCoordinate.latitude = -90;
topLeftCoordinate.longitude = 180;
CLLocationCoordinate2D bottomRightCoordinate;
bottomRightCoordinate.latitude = 90;
bottomRightCoordinate.longitude = -180;
for (CLLocation *location in points) {
topLeftCoordinate.longitude = fmin(topLeftCoordinate.longitude, location.coordinate.longitude);
topLeftCoordinate.latitude = fmax(topLeftCoordinate.latitude, location.coordinate.latitude);
bottomRightCoordinate.longitude = fmax(bottomRightCoordinate.longitude, location.coordinate.longitude);
bottomRightCoordinate.latitude = fmin(bottomRightCoordinate.latitude, location.coordinate.latitude);
}
MKCoordinateRegion region;
region.center.latitude = topLeftCoordinate.latitude - (topLeftCoordinate.latitude - bottomRightCoordinate.latitude) * 0.5;
region.center.longitude = topLeftCoordinate.longitude + (bottomRightCoordinate.longitude - topLeftCoordinate.longitude) * 0.5;
region.span.latitudeDelta = fabs(topLeftCoordinate.latitude - bottomRightCoordinate.latitude) * 1.2; //2
region.span.longitudeDelta = fabs(bottomRightCoordinate.longitude - topLeftCoordinate.longitude) * 1.2; //2
// NSLog(#"zoom lvl : %f, %f", region.span.latitudeDelta, region.span.latitudeDelta);
return region;
}
Hope this helps.
How can I convert an MKCircle's radius (in meters) into a value I can use to draw a circle using core graphics in an MKOverlayPathView subclass?
In the following code the radius is hardcoded to 50 but I need it to reflect the radius of the MKCircle.
For the position I use MKMapPointForCoordinate() to convert the MKCircle's coordinate to an MKMapPoint, then convert the MKMapPoint to a point using MKOverlayPathView's pointForMapPoint:. But how can I convert the MKCircle's radius into a relative distance?
MKCircle *circle = [self circleForLocation:location];
CGPoint relativePoint = [self pointForMapPoint:MKMapPointForCoordinate(circle.coordinate)];
float radius = 50;
//Fill
[self applyFillPropertiesToContext:context atZoomScale:zoomScale];
CGContextAddArc(context, relativePoint.x, relativePoint.y, radius, 0, 2*3.1415926535898, 1);
CGContextDrawPath(context, kCGPathFill);
The height and width of the MKCircle's boundingMapRect should be equal to the circle's diameter:
CGRect boundingRect = [self rectForMapRect:circle.boundingMapRect];
CGFloat radius = boundingRect.size.width / 2;
I have converted a longitude and latitude on my MapView to a MKMapPoint. I then want to move an imageView to that point using the imageViews center property. It seems that I somehow need to convert the MKMapPoint to a CGPoint so that I can change the center, but the numbers seem to be way off. Here is what I am using:
// Convert to MKMapPoint
CLLocationCoordinate2D coord;
coord.latitude = [loc.latitude doubleValue];
coord.longitude = [loc.longitude doubleValue];
MKMapPoint point = MKMapPointForCoordinate(coord);
// Move our image
CGFloat newXPos = point.x;
CGFloat newYPos = point.y;
CGPoint newCenter = {newXPos, newYPos};
self.movingMarker.center = newCenter;
//self.movingMarker.frame.origin.x = point.x;
//self.movingMarker.frame.origin.y = point.y;
Ideas on how to make my MKMapPoint a workable value to use for the center property of my image?
Looks like MKMapView has a method for doing this:
CGPoint newCenter = [self.map convertCoordinate:coord toPointToView:self.map];