1

这是我的实现文件:

#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

首先,我想知道为什么从不调用 didUpdateUserLocation 方法。我还想知道添加目的地的代码,该目的地的坐标存储在 latdest 和 longdest 中。它们都将从静态变量“dest”中获取值“ delhi”。我的最终目标是在地图上追踪从源坐标(latsource,longsource)到目标坐标(latest,longdest)的路线。我是 ios 开发的新手,所以我可能犯了一些菜鸟错误。

4

1 回答 1

0

didUpdateUserLocation如果您未设置,则不会调用方法mapView.delegate = self;

如果您使用具有委托方法的类,并且想要使用它们,则每次都应设置其委托 = self

不要使用与委托相同的名称:mapView

在标题中:

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

确保你连接IBOutlet MKMapView *myMapViewIterfaceBuilder

在实现文件中:

@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];
于 2013-09-21T13:49:00.933 回答