Brandon
Brandon

Reputation: 2171

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;

Upvotes: 6

Views: 9516

Answers (5)

Mikel Sanchez
Mikel Sanchez

Reputation: 2888

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)

Upvotes: 1

Patel Arjun
Patel Arjun

Reputation: 27

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];

Upvotes: 0

BhushanVU
BhushanVU

Reputation: 3455

    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];

Upvotes: 8

Ben S
Ben S

Reputation: 69342

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];

Upvotes: 6

Gopesh Gupta
Gopesh Gupta

Reputation: 707

Not set the user current location as mapRegion center. You have to set the your coordinate as map center location

Upvotes: 0

Related Questions