这是我的实施文件:
#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错误。
答案 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];