我正在尝试放大一张地图,该地图的焦点是与该地图相关联的所有图钉。我将这些信息保存在我的地图属性中。
我从这个开始,但它还不起作用:
double maxLatitude = 0;
double minLatitude = 0;
double maxLongitude = 0;
double minLongitude = 0;
for (MKAnnotation *address in self.map.locations) {
// Latitude
if ([address.latitude doubleValue] > 0) {
maxLatitude = MAX(maxLatitude, [address.latitude doubleValue]);
}
else {
minLatitude = MAX(abs(minLatitude), abs([address.latitude doubleValue]));
}
// Longitude
if ([address.longitude doubleValue] > 0) {
maxLongitude = MAX(maxLongitude, [address.longitude doubleValue]);
}
else {
minLongitude = MAX(abs(minLongitude), abs([address.longitude doubleValue]));
}
}
double centerLatitude = (maxLatitude - abs(minLatitude)) / 2;
centerLatitude *= [self calculateSignWithFirstValue:maxLatitude secondValue:minLatitude];
double centerLongitude = (maxLongitude - abs(minLongitude)) / 2;
centerLongitude *= [self calculateSignWithFirstValue:maxLongitude secondValue:minLongitude];//使用坐标创建一些MKMapRect?
我不认为我理解MKMapRect,因为当我尝试这样做的时候:
CLLocationCoordinate2D theOrigin = CLLocationCoordinate2DMake(32, -117);
MKMapRect mapRect;
mapRect.origin = MKMapPointForCoordinate(theOrigin);
mapRect.size = MKMapSizeMake(10, 10);我被送进了海洋而不是圣地亚哥。不确定MKMapRect是怎么回事。
发布于 2012-09-02 07:26:07
/**
* Return a region covering all the annotations in the given array.
* @param annotations Array of objects conforming to the <MKAnnotation> protocol.
*/
+(MKCoordinateRegion) regionForAnnotations:(NSArray*) annotations
{
double minLat=90.0f, maxLat=-90.0f;
double minLon=180.0f, maxLon=-180.0f;
for (id<MKAnnotation> mka in annotations) {
if ( mka.coordinate.latitude < minLat ) minLat = mka.coordinate.latitude;
if ( mka.coordinate.latitude > maxLat ) maxLat = mka.coordinate.latitude;
if ( mka.coordinate.longitude < minLon ) minLon = mka.coordinate.longitude;
if ( mka.coordinate.longitude > maxLon ) maxLon = mka.coordinate.longitude;
}
CLLocationCoordinate2D center = CLLocationCoordinate2DMake((minLat+maxLat)/2.0, (minLon+maxLon)/2.0);
MKCoordinateSpan span = MKCoordinateSpanMake(maxLat-minLat, maxLon-minLon);
MKCoordinateRegion region = MKCoordinateRegionMake (center, span);
return region;
}
// usage
MKCoordinateRegion region = [XXXX regionForAnnotations:self.mapView.annotations];
[self.mapView setRegion:region animated:YES];MKMapView缩放到离散的间隔,这意味着如果您在随机区域上缩放,它将选择最近的缩放间隔。这可能与tiles的分辨率有关,但AFAIK是未记录的。
发布于 2012-09-03 06:10:09
只是为了解释你问题的第二部分,关于在圣地亚哥上空创建MKMapRect并最终落入海洋……
首先,坐标32, -117只在圣地亚哥“附近”。
实际上,它在南几公里处,在距离墨西哥西海岸几公里的太平洋上。
还要注意,在MKMapRect中,origin是矩形的左上角(而不是中心),因此生成的矩形不会完全包含您指定的坐标周围的区域。
另一个实际问题是跨度大小设置为MKMapSizeMake(10, 10)。
MKMapSize使用MKMapPoint单位(而不是度、米、英里、公里等)。
地图点等于的距离以米为单位因纬度而异。
在latitude 32中,10地图点对应于1.261110米(可以使用MapKit函数MKMetersPerMapPointAtLatitude使用10.0 * MKMetersPerMapPointAtLatitude(32)进行计算)。
因此,正在创建的地图矩形位于墨西哥西海岸外,大小约为1.26x1.26米。因此,您只能看到海洋(直到您大量缩小)。
虽然您可以使用上面提到的函数将米转换为地图点并创建MKMapRect,但使用MKCoordinateRegionMakeWithDistance函数会更容易,该函数采用常规坐标(纬度和经度)以及所需的宽度和高度(以米为单位),因此所有计算都由地图视图处理。
发布于 2012-09-02 08:39:45
我有一个很好的感觉,Jano的答案也很好用,但为了多样化,这里有另一个解决方案。这是我通常用来放大给定注释的方法:
-(void)zoomToFitMapAnnotations:(MKMapView *)mapView {
if([mapView.annotations count] == 0)
return;
CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;
CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;
for(MKPointAnnotation *annotation in mapView.annotations)
{
topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);
bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
}
MKCoordinateRegion region;
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1;
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1;
region = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];
}https://stackoverflow.com/questions/12232153
复制相似问题