Get center coordinates from MapKit and display in UILabel
Declare var center = ""
at the top of your class with other declarations. In the method below, it should automatically change the value of center
:
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
center = mapView.centerCoordinate
}
When you press your button set the value of your label to the value of center.
self.yourLabelName.text = center
To display as "Latitude: ... Longitude: ..." do as follows:
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
let mapLatitude = mapView.centerCoordinate.latitude
let mapLongitude = mapView.centerCoordinate.longitude
center = "Latitude: \(mapLatitude) Longitude: \(mapLongitude)"
print(center)
self.yourLabelName.text = center
}
If you want to format the coordinates to display a little more friendly:
center = "\(String(format: "%.5f", mapLatitude)), \(String(format: "%.5f", mapLongitude))"
Adjust the %.5f according to your preference of number of decimal places.
Get Location From Center of Screen Swift MapKit
You can use the regionDidChangeAnimated delegate method and call mapView.centerCoordinate. It will look like the following:
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
var center = mapView.centerCoordinate
}
Also make sure that your Class is extending MKMapViewDelegate
and you are calling self.mapView.delegate = self
in your viewDidLoad()
function.
The centre locations in MapKit is not visible in the UiLabel
Changed the getCenterlocation function and its working fine!
`func getCenterLocation(for mapView: MKMapView) -> CLLocation {
let latitude=mapView.centerCoordinate.latitude
let longitude=mapView.centerCoordinate.longitude
return CLLocation(latitude:latitude, longitude:longitude)
}
func getCenterLocation(for myMapView: MKMapView) -> CLLocation {
let latitude=myMapView.centerCoordinate.latitude
let longitude=myMapView.centerCoordinate.longitude
return CLLocation(latitude:latitude, longitude:longitude)
}`
Mapkit - How to get bounds coordinate in mapkit?
Assuming that top left and bottom right corners are point1 and point2 respectively:
Method 1
Use MKMapView's convert method to convert a CGPoint on the view to a CLLocationCoordinate2D. If your mapView has a non-zero frame, it will be:
let point1 = mapView.frame.origin
let point2 = CGPoint(x: mapView.frame.maxX, y: mapView.frame.MaxY)
let point1Coord = mapView.convert(point1, toCoordinateFrom: yourReferenceView)
let point2Coord = mapView.convert(point2, toCoordinateFrom: yourReferenceView)
Method 2
Use center and span properties of the mapView.region:
let point1Coord = CLLocationCoordinate2D(latitude: mapView.region.center.latitude + mapView.region.span.latitudeDelta / 2, longitude: mapView.region.center.longitude - mapView.region.span.longitudeDelta / 2)
let point2Coord = CLLocationCoordinate2D(latitude: mapView.region.center.latitude - mapView.region.span.latitudeDelta / 2, longitude: mapView.region.center.longitude + mapView.region.span.longitudeDelta / 2)
MapKit uber style map Center annotation
THe other solutions for this that I have seen have not used a normal annotation. Instead they put another view over the top of the map with the annotation image in the middle. It doesn't matter what happens to the map the view stays centered on the screen and if you do ever need to interact with it, you just ask the map what the center coordinate is at that time.
Find nearest location from existing coordinates mapkit
To find nearest location among fetched data, you need to calculate distance from current location to each location you are having. And then, just sort those data and you will get the nearest location from existing coordinate mapkit.
Following is a method to find out the distance...
-(float)kilometresBetweenPlace1:(CLLocationCoordinate2D) currentLocation andPlace2:(CLLocationCoordinate2D) place2
{
CLLocation *userLoc = [[CLLocation alloc] initWithLatitude:currentLocation.latitude longitude:currentLocation.longitude];
CLLocation *poiLoc = [[CLLocation alloc] initWithLatitude:place2.latitude longitude:place2.longitude];
CLLocationDistance dist = [userLoc getDistanceFrom:poiLoc]/(1000*distance);
NSString *strDistance = [NSString stringWithFormat:@"%.2f", dist];
NSLog(@"%@",strDistance);
return [strDistance floatValue];
}
iPhone MapKit is it possible to map coordinates from a series of touches?
Apply gesture recognizer and then try below code to get the co-ordinate on view and then convert them into latitude and longitude.
CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView];
CLLocationCoordinate2D touchMapCoordinate =
[self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];
NOTE : Not tested, but seems like this will work, This should work without overlay
Code credit goes to : How to add a push pin to a MKMapView(IOS) when touching? 's accepted answer
Center Coordinate in Google Map iOS
Here mapView
is GMSMapView
. You need to user projection
property of google map.
let lat = mapView.projection.coordinate(for: mapView.center).latitude
let lng = mapView.projection.coordinate(for: mapView.center).longitude
print("center Lat & Lng = \(lat), \(lng)")
Related Topics
How to Add a Link for a Rate Button with Swift
iOS Swift Nsmutabledata Has No Member Appendstring
Load Large 3D Object .Scn File in Arscnview Aspect Fit in to the Screen Arkit Swift iOS
Swift 3 Nscache Generic Parameter 'Keytype' Could Not Be Inferred
How to Convert This Opengl Pointer Math to Swift
How to Rotate an Uiimageview Using Touchesmoved
How to Get the Today's and Tomorrow's Date in Swift 4
How to Update Constraints Programmatically
Swift: Programmatically Create Uilabel Fixed Width That Resizes Vertically According to Text Length
Custom Uitoolbar Too Close to the Home Indicator on iPhone X
Swift Uinavigation Bottom Line and Shadow Remove Without Navbar Color Change
Start/Stop Image View Rotation Animations
Get All Urls for Resources in Sub-Directory in Swift