Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get left-top and right-bottom latitude and longitude of map in MapKit

Tags:

iphone

mapkit

How to get left-top and right-bottom latitude and longitude of map in MapKit? I use this code, but it doesn't work properly. How should I fix it?

MKCoordinateRegion region = [map region];

double topL,topG,bottomL,bottomG;
//if latitude=55 and latitudeDelta=126 topL is 118 and it will be not at top, it will be at buttom of screen
topL = region.center.latitude + region.span.latitudeDelta/2; 

topG = region.center.longitude - region.span.longitudeDelta/2;

CLLocationCoordinate2D lt;
lt.latitude=topL;
lt.longitude=topG;
annotation = [Annotation new];
annotation.coordinate = lt;
annotation.title = @"Left";
[map addAnnotation:annotation];
[annotation release];
//if latitude=55 and latitudeDelta=126 bottomL is -7.23 and it will be not at bottom, it will be at above bottom of screen
bottomL = region.center.latitude - region.span.latitudeDelta/2;


bottomG = region.center.longitude + region.span.longitudeDelta/2;

CLLocationCoordinate2D rb;
rb.latitude=bottomL;
rb.longitude=bottomG;
annotation = [Annotation new];
annotation.coordinate = rb;
annotation.title = @"Right";
[map addAnnotation:annotation];
[annotation release];
like image 981
Sergey Zenchenko Avatar asked Dec 02 '09 09:12

Sergey Zenchenko


People also ask

How can we find the position of places on the map?

Answer: The intersection of latitude and longitude lines, called coordinates, identify the exact location of a place.


2 Answers

There is a much easier approach to getting those coordinates... Use the points of your view, and convert:

CLLocationCoordinate2D topLeft, bottomRight;
topLeft = [mapView convertPoint:CGPointMake(0, 0) toCoordinateFromView:mapView];
CGPoint pointBottomRight = CGPointMake(mapView.frame.size.width, mapView.frame.size.height);
bottomRight = [mapView convertPoint:pointBottomRight toCoordinateFromView:mapView];
like image 158
Douglas Mayle Avatar answered Sep 25 '22 23:09

Douglas Mayle


I created an extension for MKMapView

Swift 4.2

extension MKMapView {
    func topLeftCoordinate() -> CLLocationCoordinate2D {
        return convert(.zero, toCoordinateFrom: self)
    }

    func bottomRightCoordinate() -> CLLocationCoordinate2D {
        return convert(CGPoint(x: frame.width, y: frame.height), toCoordinateFrom: self)
    }
}

Swift 2.0

extension MKMapView {
    func topLeftCoordinate() -> CLLocationCoordinate2D {
        return convertPoint(CGPoint.zero, toCoordinateFromView: self)
    }

    func bottomRightCoordinate() -> CLLocationCoordinate2D {
        return convertPoint(CGPoint(x: frame.width, y: frame.height), toCoordinateFromView: self)
    }
}
like image 30
Tai Le Avatar answered Sep 22 '22 23:09

Tai Le