jjzjj

ios - MKAnnotation 在点击时没有响应

coder 2024-01-21 原文

我在 map 上显示了几个图钉,就像这样:

for (int i = 0; i < points.count; i++) {
    if([[[points objectAtIndex:i] objectForKey:@"lat"] class] != [NSNull class]) {
        southWest.latitude = MAX(southWest.latitude , [[[points objectAtIndex:i] objectForKey:@"lat"] doubleValue]);
        southWest.longitude = MIN(southWest.longitude, [[[points objectAtIndex:i] objectForKey:@"lon"] doubleValue]);
        northEast.latitude = MIN(northEast.latitude, [[[points objectAtIndex:i] objectForKey:@"lat"] doubleValue]);
        northEast.longitude = MAX(northEast.longitude, [[[points objectAtIndex:i] objectForKey:@"lon"] doubleValue]);

        pin.latitude = [[[points objectAtIndex:i] objectForKey:@"lat"] doubleValue];
        pin.longitude = [[[points objectAtIndex:i] objectForKey:@"lon"] doubleValue];
        CarPin *cPin = [[CarPin alloc] initWithName:[[self.brain.cars objectAtIndex:i] objectForKey:@"name"] state:[self getStateStringFor:[[[points objectAtIndex:i] objectForKey:@"state"] intValue]] coordinate:pin];
        cPin.state = [[[points objectAtIndex:i] objectForKey:@"state"] intValue];
        cPin.carID = [[points objectAtIndex:i] objectForKey:@"objekt_id"];
        [self.mapView addAnnotation:cPin];
    }
}

CLLocation *locSouthWest = [[CLLocation alloc] initWithLatitude:southWest.latitude longitude:southWest.longitude];
CLLocation *locNorthEast = [[CLLocation alloc] initWithLatitude:northEast.latitude longitude:northEast.longitude];
CLLocationDistance meters = [locSouthWest distanceFromLocation:locNorthEast];

MKCoordinateRegion region;
region.center.latitude = (southWest.latitude + northEast.latitude) / 2.0;
region.center.longitude = (southWest.longitude + northEast.longitude) / 2.0;
region.span.latitudeDelta = meters / 111319.5;
region.span.longitudeDelta = 0.0;

[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];

然后我像这样选择每个图钉的“外观”:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = @"CarPin";
if ([annotation isKindOfClass:[CarPin class]]) {
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (annotationView == nil) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
    } else {
        annotationView.annotation = annotation;
    }
    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    if (((CarPin *)annotation).state == 1) {
        annotationView.pinColor = MKPinAnnotationColorGreen;
    } else {
        annotationView.pinColor = MKPinAnnotationColorRed;
    }
    return annotationView;
}
return nil;
}

我也设置了委托(delegate)

[self.mapView setDelegate:self];

但是,当我点击每个图钉时,屏幕上没有显示任何带有详细信息的气泡!有人有同样的经历吗?

最佳答案

仅仅实现 title 属性是不够的。
您必须确保 title 没有返回 nil 或空白。
如果是,即使 canShowCallout 设置为 YES,注释 View 也不会显示标注。


与您的问题无关,但关于这一行:

region.span.latitudeDelta = meters / 111319.5;

至少出于以下三个原因,不推荐也不需要这样做:

  • 计算两个角之间的米距(您有以为单位的纬度和经度,然后将这些米转换回)是不必要的因为 latitudeDeltalongitudeDelta 只是上/下或左/右之间的度数差。所以你所要做的就是:

    region.span.latitudeDelta = fabs(southWest.latitude - northEast.latitude);
    

    这是我建议您进行的更改。

  • 将米转换为度的除法值 (111319.5) 仅在赤道处准确。

  • 如果您想根据米指定一个区域,而不是手动计算以度为单位的跨度,使用内置的 MKCoordinateRegionMakeWithDistance 会更好也更容易功能:

    CLLocationCoordinate2D center = CLLocationCoordinate2DMake (lat, long);
    CLLocationDistance latMeters = 5000;  //5 km
    CLLocationDistance lonMeters = 5000;  //5 km
    MKCoordinateRegion region 
        = MKCoordinateRegionMakeWithDistance (center, latMeters, lonMeters);
    


此外,在您的情况下,调用 regionThatFits 是不必要的,因为 setRegion 已经对您传递给它的任何区域进行了此操作。 regionThatFits 方法用于当您想知道(无需实际更改区域) map View 会将区域调整为给定特定区域时的内容。

关于ios - MKAnnotation 在点击时没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12173496/

有关ios - MKAnnotation 在点击时没有响应的更多相关文章

  1. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  2. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  3. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  4. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  5. ruby-on-rails - 每次我尝试部署时,我都会得到 - (gcloud.preview.app.deploy) 错误响应 : [4] DEADLINE_EXCEEDED - 2

    我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie

  6. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  7. 没有类的 Ruby 方法? - 2

    大家好!我想知道Ruby中未使用语法ClassName.method_name调用的方法是如何工作的。我头脑中的一些是puts、print、gets、chomp。可以在不使用点运算符的情况下调用这些方法。为什么是这样?他们来自哪里?我怎样才能看到这些方法的完整列表? 最佳答案 Kernel中的所有方法都可用于Object类的所有对象或从Object派生的任何类。您可以使用Kernel.instance_methods列出它们。 关于没有类的Ruby方法?,我们在StackOverflow

  8. ruby-on-rails - Rails 3,嵌套资源,没有路由匹配 [PUT] - 2

    我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle

  9. ruby-on-rails - 有没有办法为 CarrierWave/Fog 设置上传进度指示器? - 2

    我在Rails应用程序中使用CarrierWave/Fog将视频上传到AmazonS3。有没有办法判断上传的进度,让我可以显示上传进度如何? 最佳答案 CarrierWave和Fog本身没有这种功能;你需要一个前端uploader来显示进度。当我不得不解决这个问题时,我使用了jQueryfileupload因为我的堆栈中已经有jQuery。甚至还有apostonCarrierWaveintegration因此您只需按照那里的说明操作即可获得适用于您的应用的进度条。 关于ruby-on-r

  10. ruby - 没有类方法获取 Ruby 类名 - 2

    如何在Ruby中获取BasicObject实例的类名?例如,假设我有这个:classMyObjectSystem我怎样才能使这段代码成功?编辑:我发现Object的实例方法class被定义为returnrb_class_real(CLASS_OF(obj));。有什么方法可以从Ruby中使用它? 最佳答案 我花了一些时间研究irb并想出了这个:classBasicObjectdefclassklass=class这将为任何从BasicObject继承的对象提供一个#class您可以调用的方法。编辑评论中要求的进一步解释:假设你有对象

随机推荐