I have an circle overlay on my MKMap, which the user can change the radius of. How can I make it so when the radius is changed the Map will automatically zoom to fit the new radius size.
I have tried:
_mapView.visibleMapRect = circleOverlay.boundingMapRect;
But it zooms in too far and the stroke around my circle overlay is cut off at the top and bottom. Can someone give me any help on how to fix this please?
Try:
_mapView.visibleMapRect = [_mapView mapRectThatFits:circleOverlay.boundingMapRect];
or even mapRectThatFits:edgePadding: to get a little extra space around the edges.
In addition to David Berry's answer, this lets you animate the zoom:
[self.mapView setVisibleMapRect:[self.mapView mapRectThatFits:circleOverlay.boundingMapRect] edgePadding:UIEdgeInsetsMake(5.0f, 5.0f, 5.0f, 5.0f) animated:YES];
Related
How to get x1 &x2 origin?
self.spinner=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
float x1,x2,y1,y2;
x1=self.view.frame.origin.x;
x2=self.view.frame.origin.x;
y1=self.view.frame.origin.y;
y2=self.view.frame.origin.y;
self.spinner.center=CGPointMake((x1+x2)/2,(y1+y2)/2);
[self.spinner startAnimating];
[self.view addSubview:self.spinner];
i have a spinner,i try to place a spinner in mid point ,i know the concept but how to get that x1&x2 origin really i have confused with this...
I think you are not making point by
float x1,x2,y1,y2;
x1=self.view.frame.origin.x;
x2=self.view.frame.origin.x;
y1=self.view.frame.origin.y;
y2=self.view.frame.origin.y;
If you have to put indicator in the center of view,
self.spinner.center = self.view.center;
You have misunderstood how a rectangle is represented, your formula is based on using two corners but NSRect stores one corner, origin, and the width and height, size - see the documentation. There are also convenience macros for calculating the various properties (max coord etc.) of a rectangle - see the docs as above.
However, for this particular task you might wish to use UIView's center property, which will save you the math.
NSRect
is created with
x,y,width & height
It's also has the centre property, which is a
CGPoint
.
To work it out manually,
center.x = origin.x + size.width/2.
.. And similarly for
center.y
.
I'm drawing a map within a view covering the entire device screen space.
On top of this view is another view occupying the bottom half of the screen. The top view is semi transparent and so the user can see the covered map beneath it.
Within the map I am displaying the user's current location.
The map view is automatically positioning the map such that the user's location is centered within the view - which is therefore the center of the device screen, however the center is also covered by the top view.
However I would like the user's location to be centered within the part of the map view that is not covered by the top view.
The simplest solution is to apply an offset to a coordinate to shift the map a little bit, taking advantage of the span of the region of the MKMapView.
CLLocationCoordinate2D newCenter = userCoordinate;
newCenter.latitude -= _mapView.region.span.latitudeDelta * 0.50;
[self.mapView setCenterCoordinate:newCenter animated:YES];
Value of 0.50 is there just to give you an example. By changing it you can adjust the offset.
You can also take the current center position, convert it to a CGPoint, then add desired offset in pixels to a CGPoint and convert it back to CLLocationCoordinate2D:
UIOffset offset = UIOffsetMake(30.0f, 40.0f);
offset.horizontal
offset.vertical
CGPoint point = [_mapView convertCoordinate:userCoordinate
toPointToView:_mapView];
point.x += offset.horizontal;
point.y += offset.vertical;
CLLocationCoordinate2D newCenter = [_mapView convertPoint:point
toCoordinateFromView:_mapView];
[_mapView setCenterCoordinate:newCenter animated:YES];
Hope it helps.
I am getting some problem finding crop area for rectangle added as subview on imageview.
Why to add a rectangle over imageview, because I have a requirement that the cropping rectangle should be zoomed with the image. So I did the following..
UIView *rectangle;
[self.imageView addSubview:rectangle];
Then adding
UIPinchGestureRecognizer *pinchGesture=[[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinchGesture:)];
[self.imageView addGestureRecognizer:pinchGesture];
On gesture selector for zoom I have added.
-(IBAction)pinchGesture:(UIPinchGestureRecognizer *)recognizer{
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale = 1;
}
Since the rectangle is on the imageview, it gets zoomed with the imageview.
My problem is how to get the rectangle frame used for cropping, as when I zoom imageview, it changes its frame.
Any help, or hint will be appreciated.
Thanks.
Firstly i guess you should not use rectangle as a subview to the image view, although you know the rect area that need to be cropped Rectangle image is just for the reference for the user of the application.. right ?
Refer this link
It Might help you.
Thanks
I have a custom UIView. In this view I am overriding drawRect to draw some paths and some text.
When a tap is detected, the view is zooming in
- (void)tapDetected:(UITapGestureRecognizer *)sender {
float zoom = 3.;
sender.view.transform = CGAffineTransformScale(sender.view.transform, zoom, zoom);
...
}
Zoom works OK, but lines and text become pixelate as I zoom in. I want the lines width and the text size to stay the same, i.e. to be re-rasterized, so I insert a setNeedsDisplay at the end of the above method, but this has no effect, don't works.
Any help?
Thank you.
Try changing the contentScale of the layer in question. This has worked for my paths.
I was having similar problems with my map viewer, so i cooked up an isolated demo project. I got very heavy antialiasing when zoomed in. Basically, to rerasterize when zooming in, you have to play with the rasterizationScale like so:
sublayer.rasterizationScale = scale;
sublayer.contentsScale = scale;
Sample project and readme are here: https://bitbucket.org/robvanderveer/scrollviewdemo
I really love the way foursquare designed venue detail view. Especially the map with venue location in the "header" of view ... How was it done? Details are obviously some uiscrollview (maybe uitableview?) and behind it (in the header) there is a map so when you scroll up the map is beeing uncovered as the scroll view bounces... does anyone has an idea how to do this?
Here's the way I manage to reproduce it:-
You need a UIViewController with a UIScrollView as its view. Then, the content of the UIView you add to your scrollview should look like this :-
- The frame of the MKMapView have a negative y position. In this case, we can only see 100pts of the maps in the default state (before dragging).
- You need to disable zooming and scrolling on your MKMapView instance.
Then, the trick is to move down the centerCoordinate of the MKMapView when you drag down, and adjust its center position.
For that, we compute how much 1point represent as a delta latitude so that we know how much the center coordinate of the map should be moved when being dragged of x points on the screen :-
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView* scrollView = (UIScrollView*)self.view;
[scrollView addSubview:contentView];
scrollView.contentSize = contentView.frame.size;
scrollView.delegate = self;
center = CLLocationCoordinate2DMake(43.6010, 7.0774);
mapView.region = MKCoordinateRegionMakeWithDistance(center, 1000, 1000);
mapView.centerCoordinate = center;
//We compute how much latitude represent 1point.
//so that we know how much the center coordinate of the map should be moved
//when being dragged.
CLLocationCoordinate2D referencePosition = [mapView convertPoint:CGPointMake(0, 0) toCoordinateFromView:mapView];
CLLocationCoordinate2D referencePosition2 = [mapView convertPoint:CGPointMake(0, 100) toCoordinateFromView:mapView];
deltaLatFor1px = (referencePosition2.latitude - referencePosition.latitude)/100;
}
Once those properties are initialized, we need to implement the behavior of the UIScrollViewDelegate. When we drag, we convert the move expressed in points to a latitude. And then, we move the center of the map using the half of this value.
- (void)scrollViewDidScroll:(UIScrollView *)theScrollView {
CGFloat y = theScrollView.contentOffset.y;
// did we drag ?
if (y<0) {
//we moved y pixels down, how much latitude is that ?
double deltaLat = y*deltaLatFor1px;
//Move the center coordinate accordingly
CLLocationCoordinate2D newCenter = CLLocationCoordinate2DMake(center.latitude-deltaLat/2, center.longitude);
mapView.centerCoordinate = newCenter;
}
}
You get the same behavior as the foursquare app (but better: in the foursquare app, the maps recenter tends to jump, here, changing the center is done smoothly).
The example above is nice. If you need more help, I think they're using something very similar to RBParallaxTableViewController. https://github.com/Rheeseyb/RBParallaxTableViewController
It's essentially the same effect that Path uses for its header photo.
Yonel's answer is nice, but I found a problem as I have a pin at the center of the map. Because the negative Y, the point is hidden under my UINavigationBar.
Then, I didn't set the Negative Y, and I correct my mapView.frame according the scroll offset.
My mapView is 320 x 160
_mapView.frame = CGRectMake(0, 160, 320, -160+y);
Hope this helps someone.