将位置更改为坐标,并调用didUpdateUserLocation方法

时间:2013-09-21 13:25:06

标签: iphone ios mapkit geocoding cllocationmanager

这是我的实施文件:

#import "mapViewController.h"
@interface mapViewController ()
@end
@implementation mapViewController
@synthesize mapView,source,dest,latdest,latsource,longdest,longsource;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        dest=@"delhi";
    // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    CLGeocoder *geocoder1 = [[CLGeocoder alloc] init];
    [geocoder1 geocodeAddressString:source
                 completionHandler:^(NSArray* placemarks, NSError* error)
    {
        for (CLPlacemark* aPlacemark in placemarks)
        {
            CLLocationCoordinate2D coordinate;
            coordinate.latitude = aPlacemark.location.coordinate.latitude;
            latsource=&coordinate.latitude;
            coordinate.longitude = aPlacemark.location.coordinate.longitude;
            longsource=&coordinate.longitude;
            MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
            [annotation setCoordinate:(coordinate)];
            [annotation setTitle:source];
            annotation.subtitle = @"I'm here!!!";
            [self.mapView addAnnotation:annotation];
        }
    }];
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    MKCoordinateRegion region =MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
    MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
    point.coordinate = userLocation.coordinate;
    point.title = @"Where am I?";
    point.subtitle = @"I'm here!!!";    
    [self.mapView addAnnotation:point];
    [self.view addSubview:self.mapView];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

这是我的头文件:

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface mapViewController : UIViewController <MKMapViewDelegate>
@property (strong, nonatomic) IBOutlet MKMapView *mapView;
@property(strong,nonatomic) NSString *source,*dest;
@property(nonatomic) CLLocationDegrees *latsource,*longsource;
@property(nonatomic) CLLocationDegrees *latdest,*longdest;
@end

首先我想知道为什么doUpdateUserLocation方法永远不会被调用。我也想知道添加一个目标,其坐标存储在latdest和longdest中的代码。它们中的两个将从静态变量“dest”中获取它们的值其中的值为“delhi”。我的最终目标是从源坐标(latsource,longsource)到目标坐标(最新,最长)跟踪地图上的路线。 我是ios开发的新手,所以我可能会做一些noob错误。

1 个答案:

答案 0 :(得分:0)

如果您未设置didUpdateUserLocation

,则无法调用

mapView.delegate = self;方法

如果你使用一个具有委托方法的类,并且你想使用它们,那么每次你应该设置它的delegate = self

请勿使用与委托相同的名称:mapView

标题中的

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

请务必关联IBOutlet MKMapView *myMapView

中的IterfaceBuilder

在实施文件中:

@synthesize myMapView;

- (void)viewDidLoad

myMapView.delegate = self;

您必须将所有self.mapView更正为myMapView

你犯了一个很大的错误:

如果您使用IBOutlet,则必须添加InterfaceBuilder并进行连接。 或者您可以从代码创建所有内容:

@property (weak, nonatomic) MKMapView *myMapView;
myMapView = [[MKMapView alloc] initWithFrame:CGRectMake(0,0,320,480)];

然后用

添加屏幕
 [self.view addSubview:myMapView];