6

我有一个纬度和经度坐标,我想把我的地图集中在上面。它们存储为 NSStrings 并转换为以下位置:

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

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

我将如何修改以下内容以不关注用户的当前位置,而是关注上面指定的纬度和经度?

MKCoordinateRegion mapRegion;
mapRegion.center = mapView.userLocation.coordinate;
mapRegion.span.latitudeDelta = 0.05;
mapRegion.span.longitudeDelta = 0.05;
4

5 回答 5

8
    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];
于 2013-03-07T07:11:58.420 回答
6

我会用setCenterCoordinate:animated:它来移动地图焦点。如果您正在加载视图并希望立即将其设置到正确的位置,请设置animated:NO,否则,如果您想平移地图以平滑居中,请location设置animated: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];
于 2013-03-07T07:22:47.973 回答
1

更新为 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)
于 2016-09-27T14:34:06.320 回答
0
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];
于 2014-09-18T06:51:42.280 回答
0

不将用户当前位置设置为 mapRegion 中心。您必须将坐标设置为地图中心位置

于 2013-03-07T07:04:43.863 回答