我在我的应用程序中使用MapKit,并通过以下方式显示用户位置:
我想用用户位置的坐标设置region.center.latitude和region.center.longitude,该怎么做?
[mapview setShowUserLocation:YES];
我想用用户位置的坐标设置region.center.latitude和region.center.longitude,该怎么做?
[mapview setShowUserLocation:YES];
这是我的答案,我尝试了这样做,它可以工作:
//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 {<br>
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];
我认为这个回答了你的问题:
(使用mapView.userLocation.location.coordinate.latitude
和mapView.userLocation.location.coordinate.longitude属性
)
然后将这些坐标注入到你的MKMapView实例的
-(void)setCenterCoordinate:(CLLocationCoordinate2D)coordinate animated:(BOOL)animated
使用委托:
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.location.coordinate,
500, 500);
[mapView setRegion:region animated:YES];
}
没有委托:
[self.mapView setuserTrackingMode:MKUserTrackingModeFollow];