【问题标题】:MapView only shows correct location after 2nd loadMapView 仅在第二次加载后显示正确的位置
【发布时间】:2013-02-02 20:22:14
【问题描述】:

我正在为我的应用程序上的mapview 苦苦挣扎。在打开mapview 的情况下加载屏幕时,地图只会打开到mapviews 的默认位置。但是,当我返回上一个屏幕,然后第二次启动地图时,会显示正确的位置。

显然,这并不理想。

有什么建议吗?

我的代码是:

CLLocation *mapLocation = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
[[self map] setCenterCoordinate:[mapLocation coordinate]];
[[self map] setRegion: MKCoordinateRegionMakeWithDistance([mapLocation coordinate], 1000, 1000)];        
MapAnnotation *annotation = [[MapAnnotation alloc] init];
[annotation setCoordinate:[mapLocation coordinate]];
[[self map] addAnnotation:annotation];

谢谢!

【问题讨论】:

  • 您应该发布加载地图视图的代码,因为此代码可以正常工作。
  • 你什么时候做这个?是的...显示地图视图代码
  • 这段代码在 viewDidLoad 上的 mapViewController 上...对不起,我还在学习。我应该发布什么代码??

标签: iphone objective-c xcode map mkmapview


【解决方案1】:

我这样设置当前位置:
位置.h:

@interface WhereamiViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate, UITextFieldDelegate> {
CLLocationManager *locationManager;    
MKMapView *worldView;
IBOutlet UIActivityIndicatorView *activityIndicator;
IBOutlet UITextField *locationTextField;
}

- (void)findLocation;
- (void)foundLocation:(CLLocation *)loc;
- (IBAction)setMapTyp:(id)sender;

@property MKMapType mapType;
@property (nonatomic, retain) IBOutlet MKMapView *worldView;
@property (nonatomic, readonly) NSDate *currentDate;

@end

位置.m:

- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation {
NSLog(@"%@", newLocation);

NSTimeInterval t = [[newLocation timestamp] timeIntervalSinceNow];
if (t < -180) {
    return;
}

[self foundLocation:newLocation];
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
CLLocationCoordinate2D loc = [userLocation coordinate];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
[worldView setRegion:region animated:YES];
}



- (void)locationManager:(CLLocationManager *)manager
   didFailWithError:(NSError *)error {
NSLog(@"Could not find location: %@", error);
}

- (void)findLocation {
[locationManager startUpdatingLocation];
[activityIndicator startAnimating];
[locationTextField setHidden:YES];
}

- (void)foundLocation:(CLLocation *)loc {
CLLocationCoordinate2D coord = [loc coordinate];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"HH:mm, dd. MMM. yyyy "];

NSDate *now = [[NSDate alloc] init];

NSString *dateString = [format stringFromDate:now];

MyOwnMapPoint *mp = [[MyOwnMapPoint alloc] initWithCoordinate:coord
                                                    title:[locationTextField text]
                                                 subtitle:dateString];
NSLog(@"Die Uhrzeit ist: %@", dateString);
[worldView addAnnotation:mp];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 250, 250);
[worldView setRegion:region animated:YES];

//Reset the UI
[locationTextField setText:@""];
[activityIndicator stopAnimating];
[locationTextField setHidden:NO];
[locationManager stopUpdatingLocation];
}

希望它能给你一个想法!

【讨论】:

  • 对不起,我对目标 c 很陌生,我并不真正理解这一点。
  • 尝试使用mapView:didUpdateUserLocation:在启动时显示您的位置!
  • 是的,我现在明白了。我尝试了一个 NSLog 以确保它被调用......但它不是。我尝试的任何方法都不起作用!我仍然需要按下刷新按钮,它使用与 loadLocation 按钮相同的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-29
  • 2023-04-02
  • 1970-01-01
相关资源
最近更新 更多