xcode 使用 MKCoordinateRegion 将 MKMapView 聚焦在 NSString 纬度和经度坐标上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15265009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Focus MKMapView on NSString Latitude and Longitude Coordinates with MKCoordinateRegion
提问by Brandon
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:
我有一个纬度和经度坐标,我想把我的地图放在上面。它们存储为 NSStrings 并转换为以下位置:
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;
回答by BhushanVU
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];
回答by Ben S
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
我会使用setCenterCoordinate:animated:
以移动地图焦点。如果您正在加载视图并希望立即将其设置到正确的位置,请设置animated:NO
,否则,如果您希望平移地图以平滑居中,请location
设置animated:YES
[mapView setCenterCoordinate:location 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:
当然,这不会改变地图视图的缩放级别。如果要更新缩放级别,应使用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];
回答by Mikel Sanchez
UPDATED TO SWIFT 3
更新到 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)
回答by Gopesh Gupta
Not set the user current location as mapRegion center. You have to set the your coordinate as map center location
未将用户当前位置设置为 mapRegion 中心。您必须将您的坐标设置为地图中心位置
回答by Patel Arjun
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];