How can I calculate the current zoomScale for an MKMapView?
Use the following code:
#define MERCATOR_RADIUS 85445659.44705395
#define MAX_GOOGLE_LEVELS 20
- (double)getZoomLevel {
CLLocationDegrees longitudeDelta = self.mapView.region.span.longitudeDelta;
CGFloat mapWidthInPixels = self.mapView.bounds.size.width;
double zoomScale = longitudeDelta * MERCATOR_RADIUS * M_PI / (180.0 * mapWidthInPixels);
double zoomer = MAX_GOOGLE_LEVELS - log2( zoomScale );
if ( zoomer < 0 ) zoomer = 0;
// zoomer = round(zoomer);
return zoomer;
}
The return value of the getZoomLevel method will be the current zoom level of your mapView property.
Related
I'm trying to create clockwise text in Xcode. My current coding is just displaying like that.
But I want to change outer text to as follow
By using this library : https://github.com/javenisme/CurvaView
CoreTextArcView * cityLabel = [[[CoreTextArcView alloc] initWithFrame:rect1
font:font1
text:#"Hello this is radious arc with text"
radius:85
arcSize:110
color:color1] autorelease];
Remove arc for one file like as follows (set -fno-objc-arc to that library's .m file from build phases of your project target) :
Output like this
This is my code to do this, it draw attributed strings.
I can't post images but the result it's just right.
Text is also automatically reversed if the angle is between 100° and 260°
#define degreesToRadians(degrees) (( degrees ) / 180.0 * M_PI )
#define radiansToDegrees(radians) (( radians ) * ( 180.0 / M_PI ) )
+ (void)drawCurvedStringOnLayer:(CALayer *)layer
withAttributedText:(NSAttributedString *)text
atAngle:(float)angle
withRadius:(float)radius {
// angle in radians
CGSize textSize = CGRectIntegral([text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
context:nil]).size;
float perimeter = 2 * M_PI * radius;
float textAngle = (textSize.width / perimeter * 2 * M_PI); // text curved width
float textRotation;
float textDirection;
if (angle > degreesToRadians(10) && angle < degreesToRadians(170))
{
//down
textRotation = 0.5 * M_PI ;
textDirection = - 2 * M_PI;
angle += textAngle / 2 + degreesToRadians(6);
}
else
{
//up
textRotation = 1.5 * M_PI ;
textDirection = 2 * M_PI;
angle -= textAngle / 2; // + degreesToRadians(6);
}
for (int c = 0; c < text.length; c++)
{
NSRange range = {c, 1};
NSAttributedString* letter = [text attributedSubstringFromRange:range];
CGSize charSize = CGRectIntegral([letter boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
context:nil]).size;
float letterAngle = ( (charSize.width / perimeter) * textDirection );
float x = radius * cos(angle + (letterAngle/2));
float y = radius * sin(angle + (letterAngle/2));
//NSLog(#"Char %# with size: %f x %f", letter, charSize.width, charSize.height);
//NSLog(#"Char %# with angle: %f x: %f y: %f", letter, angle, x, y);
CATextLayer *text = [layers drawTextOnLayer:layer
withText:letter
frame:CGRectMake(layer.frame.size.width/2 - charSize.width/2 + x,
layer.frame.size.height/2 - charSize.height/2 + y,
charSize.width, charSize.height)
bgColor:nil
opacity:1];
text.transform = CATransform3DMakeAffineTransform( CGAffineTransformMakeRotation(angle - textRotation) );
angle += letterAngle;
}
}
+ (CATextLayer *)drawTextOnLayer:(CALayer *)layer
withText:(NSAttributedString *)text
frame:(CGRect)frame
bgColor:(UIColor *)bgColor
opacity:(float)opacity {
CATextLayer *textLayer = [[CATextLayer alloc] init];
textLayer.needsDisplayOnBoundsChange = true;
//[textLayer setFont:(__bridge CFTypeRef)(font)];
//[textLayer setFontSize:fontSize];
[textLayer setFrame:frame];
[textLayer setString:text];
[textLayer setAlignmentMode:kCAAlignmentCenter];
// [textLayer setForegroundColor:color.CGColor];
[textLayer setBackgroundColor:bgColor.CGColor];
[textLayer setContentsScale:[UIScreen mainScreen].scale];
[textLayer setOpacity:opacity];
[layer addSublayer:textLayer];
}
Hello I want to get current zoom level of Google Map view, like in a condition to check.
For example,
if(mapView.zoom==18.0)
{
//code goes here..
}
How to get that ?
It's pretty easy. GMSMapView has a camera (GMSCameraPosition) property that has a zoom property.
CGFloat zoom = mapView.camera.zoom;
Note that zoom property is readonly.
You can use below code.
#define MERCATOR_RADIUS 85445659.44705395
#define MAX_GOOGLE_LEVELS 20
#interface MKMapView (ZoomLevel)
- (double)getZoomLevel;
#end
#implementation MKMapView (ZoomLevel)
- (double)getZoomLevel
{
CLLocationDegrees longitudeDelta = self.region.span.longitudeDelta;
CGFloat mapWidthInPixels = self.bounds.size.width;
double zoomScale = longitudeDelta * MERCATOR_RADIUS * M_PI / (180.0 * mapWidthInPixels);
double zoomer = MAX_GOOGLE_LEVELS - log2( zoomScale );
if ( zoomer < 0 ) zoomer = 0;
// zoomer = round(zoomer);
return zoomer;
}
#end
Also can use MKCoordinateSpan check document for more information.
typedef struct {
CLLocationDegrees latitudeDelta;
CLLocationDegrees longitudeDelta;
} MKCoordinateSpan;
I have to point the arrow to particular location based on the current location. For that I am using didUpdateHeading delegate method for getting the true heading value.
In iPhone( Portrait mode) the arrow image is showing perfectly. But in iPad its not showing correctly. It is in Landscape mode.
The code I am using to find the bearing distance is,
- (void)UpdateCompass:(NSNotification *)notification
{
CLLocation *newLocation = [[CLLocation alloc] initWithLatitude:m_nLatitude longitude:m_nLongitude];
CLLocation* target = [[CLLocation alloc] initWithLatitude:questionLatitude longitude:questionLangitude ];
_latestBearing = [self getHeadingForDirectionFromCoordinate:newLocation.coordinate toCoordinate:target.coordinate];
CGFloat degrees = _latestBearing - m_CLHeading.magneticHeading;
CGAffineTransform cgaRotate = CGAffineTransformMakeRotation([self degreesToRadians:degrees]);
m_arrowImage.transform = cgaRotate;
}
- (double)degreesToRadians:(double)degrees
{
return degrees * M_PI / 180.0;
}
- (double)radiansToDegrees:(double)radians
{
return radians * 180.0/M_PI;
}
- (double)getHeadingForDirectionFromCoordinate:(CLLocationCoordinate2D)fromLoc toCoordinate:(CLLocationCoordinate2D)toLoc
{
double lat1 = [self degreesToRadians:fromLoc.latitude];
double lon1 = [self degreesToRadians:fromLoc.longitude];
double lat2 = [self degreesToRadians:toLoc.latitude];
double lon2 = [self degreesToRadians:toLoc.longitude];
double dLon = lon2 - lon1;
double y = sin(dLon) * cos(lat2);
double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon);
double radiansBearing = atan2(y, x);
if(radiansBearing < 0.0)
radiansBearing += 2*M_PI;
return [self radiansToDegrees:radiansBearing];
}
How can I make this work for iPad?
Check whether device is ipad or iphone and write your code accordingly
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// The device is an iPad running iPhone 3.2 or later.
}
else
{
// The device is an iPhone or iPod touch.
}
I am trying use the accelerometer to move an image within a circle. I am having issue that when the image hits the edge of the circle, it just moves the other side of the circle. My code is below:
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
//NSLog(#"x : %g", acceleration.x);
//NSLog(#"y : %g", acceleration.y);
//NSLog(#"z : %g", acceleration.z);
delta.x = acceleration.x * 10;
delta.y = acceleration.y * 10;
joypadCap.center = CGPointMake(joypadCap.center.x + delta.x, joypadCap.center.y - delta.y);
distance = sqrtf(((joypadCap.center.x - 160) * (joypadCap.center.x - 160)) +
((joypadCap.center.y -206) * (joypadCap.center.y - 206)));
//NSLog(#"Distance : %f", distance);
touchAngle = atan2(joypadCap.center.y, joypadCap.center.x);
NSLog(#"Angle : %f", touchAngle);
if (distance > 50) {
joypadCap.center = CGPointMake(160 - cosf(touchAngle) * 50, 206 - sinf(touchAngle) * 50);
}
I was having the same issue when attempting to implement a circular spirit level using CMDeviceMotion. I found it was an issue with the coordinates passed to atan2(y,x). This function requires cartesian coordinates, with (0,0) in the centre of the view. However, the screen coordinates have (0,0) in the top left corner. I created methods to convert a point between the two coordinate systems, and now it's working well.
I put up a sample project here on github, but here's the most important part:
float distance = sqrtf(((point.x - halfOfWidth) * (point.x - halfOfWidth)) +
((point.y - halfOfWidth) * (point.y - halfOfWidth)));
if (distance > maxDistance)
{
// Convert point from screen coordinate system to cartesian coordinate system,
// with (0,0) located in the centre of the view
CGPoint pointInCartesianCoordSystem = [self convertScreenPointToCartesianCoordSystem:point
inFrame:self.view.frame];
// Calculate angle of point in radians from centre of the view
CGFloat angle = atan2(pointInCartesianCoordSystem.y, pointInCartesianCoordSystem.x);
// Get new point on the edge of the circle
point = CGPointMake(cos(angle) * maxDistance, sinf(angle) * maxDistance);
// Convert back to screen coordinate system
point = [self convertCartesianPointToScreenCoordSystem:point inFrame:self.view.frame];
}
And:
- (CGPoint)convertScreenPointToCartesianCoordSystem:(CGPoint)point
inFrame:(CGRect)frame
{
float x = point.x - (frame.size.width / 2.0f);
float y = (point.y - (frame.size.height / 2.0f)) * -1.0f;
return CGPointMake(x, y);
}
- (CGPoint)convertCartesianPointToScreenCoordSystem:(CGPoint)point
inFrame:(CGRect)frame
{
float x = point.x + (frame.size.width / 2.0f);
float y = (point.y * -1.0f) + (frame.size.height / 2.0f);
return CGPointMake(x, y);
}
In my app, I'm the game is set for landscape mode allowing the user to be able to flip the device with the game auto-rotating to match the angle. When the user tilts the device to its right, the hero will travel to the right, and when tilted to the left, the hero goes left.
I have that part figured out, but the problem comes in when the user flips the iDevice over (because of the headphone jack or placement of the speaker), the app is auto-rotated, but the rollingY is setting the hero's position.x to be inverted. Tilting left makes hero go right, and tilting right makes hero go left. After looking at the NSLogs of the accelY value when flipping around the iDevice, it doesn't seem possible to be able to reverse the position.x when rotated?
I guess this is more of a math question.
Here is my code:
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
#define kFilteringFactor 0.75
static UIAccelerationValue rollingX = 0, rollingY = 0, rollingZ = 0;
rollingX = (acceleration.x * kFilteringFactor) + (rollingX * (1.0 - kFilteringFactor));
rollingY = (acceleration.y * kFilteringFactor) + (rollingY * (1.0 - kFilteringFactor));
rollingZ = (acceleration.z * kFilteringFactor) + (rollingZ * (1.0 - kFilteringFactor));
float accelX = rollingX;
float accelY = rollingY;
float accelZ = rollingZ;
NSLog(#"accelX: %f, accelY: %f, accelZ: %f", accelX, accelY, accelZ);
CGSize winSize = [CCDirector sharedDirector].winSize;
#define kRestAccelX 0.6
#define kShipxMaxPointsPerSec (winSize.height * 1.0)
#define kMaxDiffX 0.2
#define kRestAccelY 0.0
#define kShipyMaxPointsPerSec (winSize.width * 0.5)
#define kMaxDiffY 0.2
float accelDiffX = kRestAccelX - ABS(accelX);
float accelFractionX = accelDiffX / kMaxDiffX;
float pointsPerSecX = kShipxMaxPointsPerSec * accelFractionX;
float accelDiffY = -accelY;
float accelFractionY = accelDiffY / kMaxDiffY;
float pointsPerSecY = kShipyMaxPointsPerSec * accelFractionY;
_shipPointsPerSecX = pointsPerSecY;
_shipPointsPerSecY = pointsPerSecX;
CCLOG(#"accelX: %f, pointsPerSecX: %f", accelX, pointsPerSecX);
CCLOG(#"accelY: %f, pointsPerSecY: %f", accelY, pointsPerSecY);
}
-(void)updateShipPos:(ccTime)dt
{
CGSize winSize = [CCDirector sharedDirector].winSize;
float maxX = winSize.width - _ship.contentSize.width;
float minX = _ship.contentSize.width / 2;
float maxY = winSize.height - _ship.contentSize.height / 2;
float minY = _ship.contentSize.height / 2;
float newX = _ship.position.x + (_shipPointsPerSecX * dt);
float newY = _ship.position.y + (_shipPointsPerSecY * dt);
newX = MIN(MAX(newX, minX), maxX);
newY = MIN(MAX(newY, minY), maxY);
_ship.position = ccp(newX, newY);
}
The idea is, I'm trying to make it so that pointsPerSecY is positive when tilting to the right and negative when tilted to the left. The problem is when the user flips the device, they are inverted since the device is flipped. Is there a way to make it so that it will stay positive when tilted to the right and negative to the left, no matter the orientation, when flipped while being in landscape?
How about to do something like this?
if ([[UIApplication sharedApplication] statusBarOrientaion]==UIInterfaceOrientationLandscapeLeft) {
pointsPerSecY *= -1;
}