I am using swift 4. I have added MKMapView with some pins but I need to resize the pin image based on the map zooming.
So if anyone knows, how to resize the image based on map zooming?
You can't. Pins always say a constant size as the user zooms the map.
You'll want to look into using a MKOverlay and possibly a custom MKOverlayRenderer subclass. That will allow you to draw content that grows and shrinks as the user zooms in and out.
Related
I have to show in zoom of a selected area of image continuously. Means if you are changing position simultaneously zoom also to be change. I don't know how to implement this.
UIScrollView has a great method - zoomToRect:animated: - that allows you to zoom on a specific rect. If you have a UIImageView sitting on a UIScrollView then you can zoom around the image view using this one simple method.
Is there a way that I can control which subviews of a UIScrollView are scaled and which are not?
For example, I have a map as the bottom layer (not MapKit, just a flat image) which can be zoomed and panned. Depending on user selections, markers are dropped on the map to indicate specific buildings/places etc. These markers are are also UIImageView, using pixel co-ordinates of the map image (eq Building X is at (934, 842), marker is placed here). Zoomed out however these markers are somewhat difficult to see as they also scale/zoom out to the same level as the map.
So is there a way that I can tell the UIScrollView NOT to scale the marker images, but still allow it to pan/reposition them when the map image is zoomed?
I think the method you may be looking for is the UIScrollViewDelegate's
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
This lets you return the view which you want the zoom to be applied to, in your case this will be the background map image.
If you don't want the markers to be scaled at the same time, you will need to add the markers directly as subviews of the UIScrollView, instead of adding them as subviews of your background map UIImageView.
As you mentioned, the markers are placed relatively to the map background UIImageView, this means if you add the markers to the UIScrollView instead, they won't be placed in the same position on the map once you change the scroll.
To solve this, you will need to calculate the equivalent coordinates relative to the UIScrollView.
While this may seem rather daunting, it is actually quite trivial to do once you know the zoom factor.
To get the zoom factor, use the UIScrollViewDelegate method
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
Then you can get the zoom factor by calling :
scrollView.zoomScale
and recalculate the marker coordinates by applying this zoomScale to the original values, and reposition your markers accordingly.
Reference:
UIScrollView Class Reference
UIScrollViewDelegate Protocol Reference
Hope this helps :)
I am working with mapkit application.In it I have made custom overlay for get detail about specific location when I touched that location.In it at when I touch at map's border location I get image out of map view bound.(gray image which is out of map view).So would you suggest me how can I make it in map's bound and how can I handle it.
Thank you all.
In my app, I have a ScrollView that shows a map(just a jpeg).
On top of the ScrollView, I added some pins(UIImageView)
So far so good.
But when I zoom in, the pins also get larger.
I would like the pins to stay at a fixed size, just like the pins on the google map application on the iPhone.
How do I solve this?
Thanks!
I've been looking into this for a few days and I've come to the conclusion that there is currently no good way to do this.
First, put your pins as a direct subview of the UIScrollView. Now they'll stay the same size, but will move around when you zoom.
In OS version >=3.2 in UIScrollViewDelegate there is -scrollViewDidZoom:, where you can update the pins to the right coords as it scrolls, and this works great. But in 3.1 we're out of luck. I tried having a NSTimer going and updating the coords instead, but it's not performant on the device.
As a fallback what I do is hide the pins when zooming starts (viewForZoomingInScrollView) and when zooming ends (scrollViewDidEndZooming) update their coords and unhide them.
Make a container UIView that is the same size as the map. Add the map to the UIView with autoresizing of flexible width and height. Add all the pins as individual images, each with flexible margins. Use the container as the zooming view.
If the system autoresizeing does not retain the margin ratio then you can implement layoutSubviews in a custom container view.
How can I have the size of my icons scale with zoom in a MKMapView? As far as I can tell there is no way to do this?
The only thing I can think of is to have the MKMapView a subview of some UIView, and catch all of the user input in this parent view, scale the images on zoom and relay to the subview.
You need to do a CGTransform on the annotation views on each mapRegionChanged