这是我的实现文件:
#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 开发的新手,所以我可能犯了一些菜鸟错误。