mapview蓝点不再出现第二次

发布于 2024-11-25 03:58:55 字数 1419 浏览 5 评论 0原文

我正在开发一个应用程序来显示特定邮政编码中可用的商店。我从当前位置开始。我的当前位置出现一个蓝色脉动点。当我输入邮政编码时,我可以在地图视图中显示注释。我有一个“当前位置”按钮,可以再次返回当前位置。但是第二次我没有得到蓝点?怎么获取呢,代码如下:

- (void)showCurrentLocation 
{  
 self.mapView.showsUserLocation = YES;
    GPSAnnotation *annotation = [[GPSAnnotation alloc] initWithCoordinate:self.mapView.userLocation.coordinate];

    annotation.title = @"Location";
    [self.mapView addAnnotation:annotation];
    [annotation release];    

    MKCoordinateRegion zoomIn;
    zoomIn.center.latitude = self.mapView.userLocation.coordinate.latitude;
    zoomIn.center.longitude=self.mapView.userLocation.coordinate.longitude;

    zoomIn.span.latitudeDelta = .1f;
    zoomIn.span.longitudeDelta =.1f;

    self.mapView.showsUserLocation = YES;
    [self.mapView setRegion:zoomIn animated:TRUE];
}

// annotation view 

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(GPSAnnotation *)annotation
{
    if ([[annotation title] isEqualToString:@"Location"]) 
    {
        MKAnnotationView *annotationView=[[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil] autorelease];

        annotationView = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"blueDot"];
        annotationView.annotation = annotation;
        return  annotationView;
    }

    else  if ([annotation isKindOfClass:[MKUserLocation class]])
    {        
        return nil;
    }
}

I'm developing an app to show the stores available in a particular zipcode. I start with the current location. I'm getting a blue pulsating dot for my current location. when I enter a zipcode I'm able to show the annotations in the mapview. I have a button "current location" which can to go back to the current location again. However the second time I'm not getting the blue dot? How do I get it, this is the code:

- (void)showCurrentLocation 
{  
 self.mapView.showsUserLocation = YES;
    GPSAnnotation *annotation = [[GPSAnnotation alloc] initWithCoordinate:self.mapView.userLocation.coordinate];

    annotation.title = @"Location";
    [self.mapView addAnnotation:annotation];
    [annotation release];    

    MKCoordinateRegion zoomIn;
    zoomIn.center.latitude = self.mapView.userLocation.coordinate.latitude;
    zoomIn.center.longitude=self.mapView.userLocation.coordinate.longitude;

    zoomIn.span.latitudeDelta = .1f;
    zoomIn.span.longitudeDelta =.1f;

    self.mapView.showsUserLocation = YES;
    [self.mapView setRegion:zoomIn animated:TRUE];
}

// annotation view 

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(GPSAnnotation *)annotation
{
    if ([[annotation title] isEqualToString:@"Location"]) 
    {
        MKAnnotationView *annotationView=[[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil] autorelease];

        annotationView = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"blueDot"];
        annotationView.annotation = annotation;
        return  annotationView;
    }

    else  if ([annotation isKindOfClass:[MKUserLocation class]])
    {        
        return nil;
    }
}

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

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

发布评论

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

评论(1

夢归不見 2024-12-02 03:58:55

您是否在某个地方创建了 [self.mapView removeAnnotations:self.mapView.annotations] 来清空地图?这是一个常见的错误,因为您还通过这样做删除了用户位置。

Do you make somewhere a [self.mapView removeAnnotations:self.mapView.annotations] to empty the map? Thats a common error because you also remove the userlocation by doing that.

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