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

  显示原文与译文双语对照的内容
0 0

这是我的实现文件:


#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

First didUpdateUserLocation方法是在/called.I 中找到一个目标,并在/longdest.Both 中添加一个目标,该目标的坐标将从源 coordinates(latsource,longsource) 到目标 coordinates(latest,longdest)的映射。 因为我是noob开发新手,所以我可能做了一些错误。

时间: 原作者:

0 0

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

如果使用一个具有委托方法的类,并且要使用它们,每次应该设置它的委托=,

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

在标题中:


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

确保在 IterfaceBuilder 中连接了 IBOutlet MKMapView *myMapView

在实现 file: 中


@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];

原作者:
...