取消 MkMapView 中心

发布于 2024-08-17 04:41:28 字数 2187 浏览 1 评论 0原文

我试图“取消中心”地图视图并删除所有注释。这样做的原因是不同的视图控制器将使用mapView来显示不同的注释和位置。现在发生的情况是,我正在使用“删除注释”方法并删除它们,但地图保持在该位置的中心位置,因此当新注释进入时,它不会移动。有没有办法重置该区域,如果可以,我应该将其重置为什么值。我尝试了零、零和一些计算值,但“核心动画”说它们都是无效的。我如何让它缩小并返回到我的新注释?

这是一些代码

-(void)recenterMap {

NSArray *coordinates = [_mapView valueForKeyPath:@"annotations.coordinate"];
CLLocationCoordinate2D maxCoord = {-90.0f, -180.0f};
CLLocationCoordinate2D minCoord = {90.0f, 180.0f};

//NSLog(@"%@", [coordinates description]);

for (NSValue *value in coordinates) {
    CLLocationCoordinate2D coord = {0.0f, 0.0f};
    [value getValue: &coord];

    if(coord.longitude > maxCoord.longitude) {
        maxCoord.longitude = coord.longitude;
    }

    if(coord.latitude > maxCoord.latitude){

        maxCoord.latitude = coord.latitude;
    }

    if(coord.longitude < minCoord.longitude){
        minCoord.longitude = coord.longitude;
    }

    if(coord.latitude < minCoord.latitude){
        minCoord.latitude = coord.latitude;
    }
}


MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}};
region.center.longitude = (minCoord.longitude + maxCoord.longitude) / 2.0;
region.center.latitude = (minCoord.latitude + maxCoord.latitude) / 2.0;
region.span.longitudeDelta = maxCoord.longitude - minCoord.longitude;
region.span.latitudeDelta = maxCoord.latitude - minCoord.latitude;


[_mapView setRegion: region animated: YES];
}

这就是我正在使用的最近方法

- (MKAnnotationView *)mapView: (MKMapView *)mapView viewForAnnotation: (id <MKAnnotation>)annotation{

MKAnnotationView *view = nil;
if(annotation != mapView.userLocation){

    Annotation *schoolAnn = (Annotation*)annotation;
    view = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"schoolLoc"];
    if(nil == view){
        view = [[[MKPinAnnotationView alloc] initWithAnnotation:schoolAnn     reuseIdentifier:@"schoolLoc"]autorelease];
    }

    [(MKPinAnnotationView *)view setAnimatesDrop:YES];
    [view setCanShowCallout:YES];

}
else { 
    [self recenterMap];
}

return view;
}

这个“其他”部分会等到蓝色大理石落入然后将地图重新​​放置到中心。我对该部分的问题是,当我返回到上一个视图控制器时,我删除了所有注释,因此当我返回用户位置(由于某种原因)时不会弹出。我如何让它弹出回来了?

预先感谢大家

I am trying to "uncenter" the map view and remove all annotations. the reason for this is that the mapView will be used by different view controllers to display different annotations and locations. What's happening now is that i am using the "remove annotations" method and i removes them, but the map stays centered on the spot and thus when the new annotations come in, it does not move. Is there a way to reset the region and if so, what value do i reset it to. I tried nil, zero and some calculated value but "core animation" says they are all invalid. how do i get that to zoom out and back in on my new annotations?

here is some code

-(void)recenterMap {

NSArray *coordinates = [_mapView valueForKeyPath:@"annotations.coordinate"];
CLLocationCoordinate2D maxCoord = {-90.0f, -180.0f};
CLLocationCoordinate2D minCoord = {90.0f, 180.0f};

//NSLog(@"%@", [coordinates description]);

for (NSValue *value in coordinates) {
    CLLocationCoordinate2D coord = {0.0f, 0.0f};
    [value getValue: &coord];

    if(coord.longitude > maxCoord.longitude) {
        maxCoord.longitude = coord.longitude;
    }

    if(coord.latitude > maxCoord.latitude){

        maxCoord.latitude = coord.latitude;
    }

    if(coord.longitude < minCoord.longitude){
        minCoord.longitude = coord.longitude;
    }

    if(coord.latitude < minCoord.latitude){
        minCoord.latitude = coord.latitude;
    }
}


MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}};
region.center.longitude = (minCoord.longitude + maxCoord.longitude) / 2.0;
region.center.latitude = (minCoord.latitude + maxCoord.latitude) / 2.0;
region.span.longitudeDelta = maxCoord.longitude - minCoord.longitude;
region.span.latitudeDelta = maxCoord.latitude - minCoord.latitude;


[_mapView setRegion: region animated: YES];
}

Thats the recenter methos i'm using

- (MKAnnotationView *)mapView: (MKMapView *)mapView viewForAnnotation: (id <MKAnnotation>)annotation{

MKAnnotationView *view = nil;
if(annotation != mapView.userLocation){

    Annotation *schoolAnn = (Annotation*)annotation;
    view = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"schoolLoc"];
    if(nil == view){
        view = [[[MKPinAnnotationView alloc] initWithAnnotation:schoolAnn     reuseIdentifier:@"schoolLoc"]autorelease];
    }

    [(MKPinAnnotationView *)view setAnimatesDrop:YES];
    [view setCanShowCallout:YES];

}
else { 
    [self recenterMap];
}

return view;
}

This "else" piece waits until the blue marble drops in then recenters the map. The problem i have with that part is that when i go back to the previous View Controller i remove all annotations, so when i come back in the User location (for some reason) does not pop back in. How do i get this to pop back in?

thanks in advance guys

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鲜肉鲜肉永远不皱 2024-08-24 04:41:28

得到了第一部分,只需调用我制作的将区域设置为美国上方的函数即可使其再次缩小。

-(void)unCenterMap{

MKCoordinateRegion region2 = {{0.0f, 0.0f}, {0.0f, 0.0f}};
region2.center.longitude = (-98.151855); //coordinate for approximately the center of the US
region2.center.latitude = 39.402244;
region2.span.longitudeDelta = 3.0;
region2.span.latitudeDelta = 3.0;


[_mapView setRegion: region2 animated: YES];

}

第二部分仍然让我困惑,我怎样才能让那个位置再次出现?

Got the first part, made it zoom out again by just calling a function i made that set the region up to be over the US.

-(void)unCenterMap{

MKCoordinateRegion region2 = {{0.0f, 0.0f}, {0.0f, 0.0f}};
region2.center.longitude = (-98.151855); //coordinate for approximately the center of the US
region2.center.latitude = 39.402244;
region2.span.longitudeDelta = 3.0;
region2.span.latitudeDelta = 3.0;


[_mapView setRegion: region2 animated: YES];

}

The second part still baffles me, how do I get that location to pop in again?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文