Focus MKMapView in 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 saved as NSStrings and converted to the following places:

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

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

How can I change below so as not to focus on the user's current location, but on the latitude and longitude indicated above?

MKCoordinateRegion mapRegion;
mapRegion.center = mapView.userLocation.coordinate;
mapRegion.span.latitudeDelta = 0.05;
mapRegion.span.longitudeDelta = 0.05;
+5
source share
5 answers

I would use setCenterCoordinate:animated:to move the focus point of the map. If you download the view and want to immediately install it in the right place, install animated:NO, otherwise, if you want to adjust the map to smoothly center on location, then installanimated:YES

[mapView setCenterCoordinate:location animated:YES];

, . , setRegion:animated:. , :

// 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];
+5
    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];
+8

UPDATED WITH 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)
+1
source

Do not set the user's current location as the center of mapRegion. You must set the coordinate as the location of the center of the map.

0
source
CLLocationCoordinate2D location;
location.latitude =  [[[matchingItems objectAtIndex:j] valueForKey:@"latitude"] doubleValue];
location.longitude = [[[matchingItems objectAtIndex:j] valueForKey:@"longitude"] doubleValue];

MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance (location, 2000, 2000);
[mywebView setRegion:region animated:YES];
0
source

All Articles