Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Focus MKMapView on NSString Latitude and Longitude Coordinates with MKCoordinateRegion

I have a latitude and longitude coordinate that I would like to focus my map on. They are stored as NSStrings and converted to locations below:

NSString *placeLatitude = [elementCoords objectForKey:@"placeLatitude"];
NSString *placeLongitude = [elementCoords objectForKey:@"placeLongitude"];

CLLocationCoordinate2D location;
location.latitude = [placeLatitude doubleValue];
location.longitude = [placeLongitude doubleValue];

How would I modify the below to not focus on the user's current location, but on the latitude and longitude specified above?

MKCoordinateRegion mapRegion;
mapRegion.center = mapView.userLocation.coordinate;
mapRegion.span.latitudeDelta = 0.05;
mapRegion.span.longitudeDelta = 0.05;
like image 928
Brandon Avatar asked Mar 07 '13 06:03

Brandon


3 Answers

    MKCoordinateRegion region;
   CLLocation *locObj = [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake([placeLatitude doubleValue], [placeLongitude doubleValue])
                                                   altitude:0
                                         horizontalAccuracy:0
                                           verticalAccuracy:0
                                                  timestamp:[NSDate date]];
    region.center = locObj.coordinate;  
    MKCoordinateSpan span; 
    span.latitudeDelta  = 1; // values for zoom
    span.longitudeDelta = 1; 
    region.span = span;
    [self.mapView setRegion:region animated:YES];
like image 72
BhushanVU Avatar answered Nov 16 '22 11:11

BhushanVU


I would use setCenterCoordinate:animated: in order to move the map focus point. If you're loading the view and want to have it set to the correct location immediately, set animated:NO, otherwise, if you want to pan the map to smoothly center on location then set animated:YES

[mapView setCenterCoordinate:location animated:YES];

Of course, this won't change the zoom level of the map view. If you want to update the zoom level you should use setRegion:animated:. For example, if you want to zoom in twice as close:

// Halve the width and height in the zoom level.
// If you want a constant zoom level, just set .longitude/latitudeDelta to the
// constant amount you would like.
// Note: a constant longitude/latitude != constant distance depending on distance
//       from poles or equator.
MKCoordinateSpan span =
    { .longitudeDelta = mapView.region.span.longitudeDelta / 2,
      .latitudeDelta  = mapView.region.span.latitudeDelta  / 2 };

// Create a new MKMapRegion with the new span, using the center we want.
MKCoordinateRegion region = { .center = location, .span = span };
[mapView setRegion:region animated:YES];
like image 28
Ben S Avatar answered Nov 16 '22 10:11

Ben S


UPDATED TO SWIFT 3

let span = MKCoordinateSpan(latitudeDelta: mapView.region.span.latitudeDelta / 2, longitudeDelta: mapView.region.span.latitudeDelta  / 2)

        // Create a new MKMapRegion with the new span, using the center we want.
        let region = MKCoordinateRegion(center: location, span: span)

        mapView.setRegion(region, animated: true)
like image 1
Mikel Sanchez Avatar answered Nov 16 '22 11:11

Mikel Sanchez