在当前位置放下图钉 [英] Drop Pin on current location

查看:148
本文介绍了在当前位置放下图钉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个iOS应用,需要能够在用户当前所在的位置放置图钉!由于某种原因,我一直都在苦苦挣扎!我尝试了以下代码,但遇到错误.

I am building an iOS app and need to be able to place a pin where the user currently is! For some reason I have been having an awful time getting it to work! I tried the following code but was faced with an error.

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

    if (annotation == mapView.userLocation)
    {

        MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
        annView.pinColor = MKPinAnnotationColorRed;
        annView.animatesDrop=TRUE;
        annView.canShowCallout = YES;
        annView.calloutOffset = CGPointMake(-5, 5);
        return annView;
        [annView release];



    }
}

错误是: 控制可能会到达非空函数的结尾.

The error was: Control May Reach End of non-void function.

非常感谢您的帮助! ' 赞赏!

Thank you so much for the help! ' Appreciate it!

推荐答案

以下代码在Xcode iPhone模拟器中可以正常工作:

The following code works fine in the Xcode iPhone simulator:

#import "ViewController.h"
@import MapKit;

@interface ViewController () <MKMapViewDelegate>

@property (weak, nonatomic) IBOutlet MKMapView *mapView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.mapView setDelegate:self];
    [self.mapView setShowsUserLocation:YES];
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {

    // zoom to region containing the user location
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];

    // add the annotation
    MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
    point.coordinate = userLocation.coordinate;
    point.title = @"The Location";
    point.subtitle = @"Sub-title";
    [self.mapView addAnnotation:point];
}

@end

这篇关于在当前位置放下图钉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆