【发布时间】:2019-05-23 09:47:19
【问题描述】:
我在我的应用程序中使用 MapKit 并显示用户位置
[mapview setShowUserLocation:YES];
我想用userLocation的坐标设置region.center.latitude和region.center.longitude,怎么做?
【问题讨论】:
标签: iphone objective-c mapkit
我在我的应用程序中使用 MapKit 并显示用户位置
[mapview setShowUserLocation:YES];
我想用userLocation的坐标设置region.center.latitude和region.center.longitude,怎么做?
【问题讨论】:
标签: iphone objective-c mapkit
这是我的答案,我尝试了类似的方法及其工作原理:
//inside my ViewDidLOad
locManager = [[CLLocationManager alloc] init];
[locManager setDelegate:self];
[locManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locManager startUpdatingLocation];
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
CLLocationCoordinate2D loc = [newLocation coordinate];
[maptView setCenterCoordiante:loc];
}
【讨论】:
容易多了:
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.location.coordinate, 1500, 1500);
[mapView setRegion:region animated:YES];
}
【讨论】:
由于某种原因,这对我不起作用。它把我带到坐标(0.0000,0.0000)。以下是我的代码,如果您有机会查看的话。感谢您的帮助!
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
mapView.showsUserLocation=TRUE;
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
CLLocationCoordinate2D myCoord = {mapView.userLocation.location.coordinate.latitude,mapView.userLocation.location.coordinate.longitude};
[mapView setCenterCoordinate:myCoord animated:YES];
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapView setRegion:region animated:YES];
[mapView setDelegate:self];
【讨论】:
我认为这回答了您的问题: Returning Mapkit Userlocation Coordinates
(使用 mapView.userLocation.location.coordinate.latitude 和 mapView.userLocation.location.coordinate.longitude properties )
然后将这些坐标注入到
-(void)setCenterCoordinate:(CLLocationCoordinate2D)coordinate animated:(BOOL)animated
您的 MKMapView 实例。
【讨论】:
使用委托:
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.location.coordinate, 500, 500); [mapView setRegion:region animated:YES]; }
没有委托:
[self.mapView setuserTrackingMode:MKUserTrackingModeFollow];
【讨论】: