MKMapview zoom level within Kilometer - ios

i have a application which has 3 things in UIViewController
MapView
Search-control - (Ability to Search any Location in Map view)
Slider - (minimum value 0km - Maximum value 50Km)
Now i have Searched one city Lets Say Barcelona . it will search using Geocode method and i got the latitude and Longitude of the Barcelona and i have added one annotation of red Pin which show title and sub locality in annotation click..
i want to calculate distance of Zoom in zoom out with in the Radius of that annotation in Kilometers. what are the best possible way to achieve this ?
For e.g. i want to zoom around 30 km within that Radius
Any help will be greatly appreciated

Calculate a MKCoordinateRegion with the searched point as the center and 30km(or whatever distance) as the radius,
see this to do it. Set the map region to the calculated value using this method
setVisibleMapRect:animated:

Related

Auto Zoom in for a cluster of map markers

I have multiple map views and each view has a group of map markers
e.g. Map View 1 = 4 map markers, Map View 2 = 10 map markers, Map View 3 = 20 map markers.
In most instances the map markers are a few meters from each other and in other instances they are a few miles apart.
Is there an easy way to set the Map View such that it automatically zooms in such that it encompasses the map markers in each view. If i have to do it manually no amount of zoom level will never be perfect.
An alternative is to define a center and a custom zoom level for each map view but that sounds a bit tedious.
There is no automatic function for doing this - you can calculate the bounding box corresponding to the POI cluster (i.e. find the top left lat and long and bottom right lat and long) and zoom to that bounding box.

How To Detect A User Is Within Range Of An Annotation

So I am working on a project that includes many uses placing annotations all around a map. The annotation, (which is a custom image with a much larger circular range) appears on the screen and, ideally, I would like for a user to be:
Notified if they are within the range of a annotation
and
Not be allowed to place another annotation within the range of another one if the circular pins overlap by, say, more than 25%
I think this is a pretty unique question and should be fun for somebody to help out with, so have fun! Thanks everybody!
You can check the distance from each annotation using
- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location
This method measures the distance between the two locations by tracing
a line between them that follows the curvature of the Earth. The
resulting arc is a smooth curve and does not take into account
specific altitude changes between the two locations.
For more details refer Link
Try this:
let location = CLLocation(latitude: 1, longitude: 1)//Or user's location
let distance = location.distance(from: anotherLocation)
Edit:
As mentioned in the comments, you wanted to create an equidistant point. I suggest manually doing that:
Subtract the annotation's location from he user's location. Then add your distance back to the original one. For example:
The user's location = (1, 1)
The annotation's location = (3, 2)
Vertical difference would be 2
Horizontal difference would be 1
Then:
(3 + 2, 2 + 1)
Your result: (5, 3)
Now you would have two points (the one you just created and the user's location) at each end with a center point (original annotation)

iOS: (Swift) How to show distance from current location and exist annotation on annotation subtitle

I am currently working on map application base on iOS using Swift language. I would like an suggestion because after I plot all the pins on map view
(which I receive data from my server using JSON frameworks call Alamofire)
I would like the subtitle of all annotations on map to show distance from user current location.
Now it can add annotations onto map view but can only show title and subtitle base on information receive from my server.
Thank You.
If you have two CLLocation instances, you can calculate distance with the following code:
var one, two: CLLocation
// assign one and two
let distance = two.distanceFromLocation(one)
CLLocationDistance is just double and distance calculated in meters

iOS MapKit get actual visible area of MKMapView when mapView is 3D

I've tried the following to get the actual visible area of my MKMapView after a region change. None produce the desired result after the user rotates the map.
use mapView.bounds and mapView.convertPoint to get NE and SW CLLocationCoordinate2D.
use mapView.visibleRect to create NE and SW MKMapPoints and convert those points to NE and SW CLLocationCoordinate2D.
use mapView.centerCoodinate and mapView.region.span latitude and longitude delta to calculate NE and SW latitude and longitude, which are then used for new NE and SW CLLocationCoordinate2D.
#1 and #2 come from this post, and all 3 work well enough until the user rotates the map, which brings the mapView.camera into play by changing its heading. Once this happens, the mapView.visibleRect does not match the actual visible area. I'm sure changing altitude and pitch will have similar issues. I understand why properties on MKMapView don't make sense once it goes 3D, but I don't know how to account for the mapView.camera. There is mention of this in a comment on one of the proposed answers in this post, but no solution provided.
My question is, how can I get the area that's actually visible to the user, through the mapView.camera, accounting for heading, altitude and pitch?
I was looking for an answer to the similar situation, and found this. For my project I resolved like the following. Hope this helps.
let northWestCoordinate = self.mapView.convert(CGPoint(x: 0, y: 0), toCoordinateFrom: self.mapView)
let southEastCoordinate = self.mapView.convert(CGPoint(x: self.mapView.frame.size.width, y: self.mapView.frame.size.height), toCoordinateFrom: self.mapView)

How to Get 4lat & Long corner points of any country using maps

I would like to get 4 Lat & Long values of any country, for Suppose let me take India, for India I need east, west, North & south Coordinates, So as i can calculate those 4 points to get a centre point to zoom
or
Please suggest me any idea, How to zoom country wise, if name given I need to that particular country
Find the country in OpenStreetMap using the search facility in the top-left, then press 'Export'. Now press 'Manually select a different area', then drag out a rectangle to enclose the country. You will see the four extremes of latitude and longitude displayed in the top-left of the screen.

Resources