尝试在 Mapkit 上显示用户位置,这是有史以来最烦人的事情。有人伸出援手吗?

发布于 2024-10-04 18:53:08 字数 1397 浏览 3 评论 0原文

我正在关注本教程(http ://icodeblog.com/2009/12/21/introduction-to-mapkit-in-iphone-os-3-0/)向应用程序添加mapkit和注释。但是,我在用户位置方面遇到了很大的困难。我是 xcode 新手,所以不太确定下一步该做什么。我尝试过托尼的选择:

step one: add the CoreLocation framework to the project.

Step two: add this function to the iCodeMapViewController.m:


- (void)setCurrentLocation:(CLLocation *)location {
MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}};
region.center = location.coordinate;
region.span.longitudeDelta = 0.15f;
region.span.latitudeDelta = 0.15f;
[self.mapView setRegion:region animated:YES];
}

step three: add this code to the ViewForAnnotation Method:


if (annotation != mapView.userLocation) {
//the rest of the ViewForAnnotation code goes here
}else{
CLLocation *location = [[CLLocation alloc]
initWithLatitude:annotation.coordinate.latitude
longitude:annotation.coordinate.longitude];
[self setCurrentLocation:location];
} 

但是当我去构建时,它不喜欢它。

我也尝试过这个选项:

    -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation
{
 if ([annotation isKindOfClass:MKUserLocation.class]) return nil;
        //rest of code
}

蓝点显示,我的自定义注释显示,但当我尝试滚动表格时应用程序崩溃。调试器没有提供任何帮助,但会在此语句处停止。

有人可以帮忙吗?也有代码示例吗?我认为这篇文章的答案可能对许多也在与 Mapkit 作斗争的人有用。

干杯

I'm following this tutorial (http://icodeblog.com/2009/12/21/introduction-to-mapkit-in-iphone-os-3-0/) on adding mapkit and annotations to an application. However, i'm seriously struggling with the User Location. I'm new to xcode so not quite sure what to do next. I have tried Tony's option:

step one: add the CoreLocation framework to the project.

Step two: add this function to the iCodeMapViewController.m:


- (void)setCurrentLocation:(CLLocation *)location {
MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}};
region.center = location.coordinate;
region.span.longitudeDelta = 0.15f;
region.span.latitudeDelta = 0.15f;
[self.mapView setRegion:region animated:YES];
}

step three: add this code to the ViewForAnnotation Method:


if (annotation != mapView.userLocation) {
//the rest of the ViewForAnnotation code goes here
}else{
CLLocation *location = [[CLLocation alloc]
initWithLatitude:annotation.coordinate.latitude
longitude:annotation.coordinate.longitude];
[self setCurrentLocation:location];
} 

But when i go to build, it doesn't like it.

I've also tried this option:

    -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation
{
 if ([annotation isKindOfClass:MKUserLocation.class]) return nil;
        //rest of code
}

The blue dot shows, my custom annotations show but the app crashes when i try and scroll through the table. The debugger gives no help but does stop on this statement.

Can someone please help? With code examples too? i think the answer to this post might be useful to a number of people also struggling with the mapkit.

Cheers

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

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

发布评论

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

评论(2

來不及說愛妳 2024-10-11 18:53:08

我有同样的问题,但我设法解决了。
在 cellForRowAtIndexPath 中,我这样做了:


NSMutableArray *annotations = [[NSMutableArray alloc] init];
    if(indexPath.section == 0)
    {
        for(MinuAsukohad *annotation in [mapView annotations])
        {
            if(![annotation isKindOfClass:[MKUserLocation class]])
            {  
            if([annotation annotationType] == MinuAsukohadTypeInterest)
                {
                [annotations addObject:annotation];
                }
            }
        }
        cell.textLabel.text = [[annotations objectAtIndex:indexPath.row] title];
    }

您只需对所有部分重复它即可。

I had the same problem, but I managed to solve it.
In cellForRowAtIndexPath I did this:


NSMutableArray *annotations = [[NSMutableArray alloc] init];
    if(indexPath.section == 0)
    {
        for(MinuAsukohad *annotation in [mapView annotations])
        {
            if(![annotation isKindOfClass:[MKUserLocation class]])
            {  
            if([annotation annotationType] == MinuAsukohadTypeInterest)
                {
                [annotations addObject:annotation];
                }
            }
        }
        cell.textLabel.text = [[annotations objectAtIndex:indexPath.row] title];
    }

You just have to repeat it for all the sections.

静待花开 2024-10-11 18:53:08

听起来您正在尝试将当前位置作为表中的单元格之一...查看您的控制台并在崩溃发生时向我们提供输出。

Sounds like you are trying to include your current location as one of the cells in the table ... look at your console and give us the output when the crash happens.

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