Iphone 使用MKCoordinateRegion将MKMapView聚焦在NSString纬度和经度坐标上

Iphone 使用MKCoordinateRegion将MKMapView聚焦在NSString纬度和经度坐标上,iphone,ios,objective-c,xcode,Iphone,Ios,Objective C,Xcode,我有一个纬度和经度坐标,我想集中在我的地图上。它们存储为NSSTRING,并转换到以下位置: NSString *placeLatitude = [elementCoords objectForKey:@"placeLatitude"]; NSString *placeLongitude = [elementCoords objectForKey:@"placeLongitude"]; CLLocationCoordinate2D location; location.latitude = [

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

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;

未将用户当前位置设置为mapRegion center。您必须将坐标设置为地图中心位置,以便移动地图焦点。如果正在加载视图并希望立即将其设置到正确的位置,请将
设置为动画:否
,否则,如果希望平移地图以平滑地居中于
位置
,请将
设置为动画:是

    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];
[mapView设置中心坐标:位置动画:是]

当然,这不会更改地图视图的缩放级别。如果要更新缩放级别,应使用。例如,如果要放大两倍的距离:

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

更新为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)

谢谢,关于如何将我得到的转换为一个可以用于此目的的坐标,有什么建议吗?以CLLocation对象为例,设置其坐标。纬度和坐标。经度。你能给你的答案添加一个例子吗:)CLLocation locationObject;locationObject.latitude=@“您的纬度”;locationObject.longitude=@“您的经度”;mapRegion.center=locationObject.coordinate;您好,有几个错误:无法静态分配,在对象配置中找不到属性纬度。。。any ideasClose-在时间戳处获取预期标识符错误:nil lineHmmm,仍然获取预期标识符错误。有什么想法吗?@bhushanparkar如果我想从其他类更新MKMapView的位置怎么办。在我的例子中,当我在tableview中选择一个值时,我将纬度和经度分配给另一个类中的mapview。但它并没有改变。有什么建议,请帮助我。谢谢:)@bhushanparkar请帮助我,我尝试了协议&通过类实例对象访问地图视图。什么都没用。谢谢:)太棒了-我如何设置缩放级别?更新了我的答案,让我知道创建结构的C语法是否有任何问题,我可以使用更少的内联版本进行更新。这段代码是使用大脑诱导的编译器编写的:)呸,尝试在Xcode中编译,发现我的内联结构定义失败,再次更新了我的答案:)得到错误“成员引用基类型int不是结构或联合”-有想法吗?是的,结构中的成员定义之间缺少逗号。再次更新答案。
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)