use*_*801 8 iphone mkmapview ios
我对iPhone的编程很陌生,但我在其中制作了一个只有MKMapView的应用程序.我让它放大到我的位置但我不断更新位置.我的观点是,我希望它停止自动定位我,它仍然可以继续,但我只想让它停止定位.当我向任何方向滑动时,它会迫使我回到我的位置,或者当我散步并更新位置时.我的想法是,我希望应用程序使用一些注释.
- (void) mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
NSLog(@"Found your location!");
MKCoordinateRegion mapregion;
map.showsUserLocation = YES;
mapregion.center.latitude = map.userLocation.coordinate.latitude;
mapregion.center.longitude = map.userLocation.coordinate.longitude;
mapregion.span = MKCoordinateSpanMake(0.005,0.005);
[map setRegion:mapregion animated:YES];
map.userLocation.title = @"You're here";
// map.mapType = MKUserTrackingModeNone;
// (found this on apple developer site, I think this can
// help but I have no clue at all //someone know what you can use this for?)
}
// In case you have the flightmode on...
- (void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error
{
NSLog(@"gick inte att hitta position!");
map.showsUserLocation = YES;
MKCoordinateRegion mapregion;
mapregion.center.latitude =(59.329444);
mapregion.center.longitude =(18.068611);
mapregion.span.latitudeDelta = 0.03;
mapregion.span.longitudeDelta = 0.03;
mapregion = [map regionThatFits:mapregion];
[map setRegion:mapregion animated:TRUE];
}
- (void)viewDidLoad
{
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
map.delegate = self;
}
Run Code Online (Sandbox Code Playgroud)
我真的希望有人知道如何做到这一点......
您的问题是,每次更新用户位置时,您都要设置地图的中心.这是您的标题中的一个解决方案:
BOOL userLocationShown;
Run Code Online (Sandbox Code Playgroud)
然后...
- (void) mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
if(userLocationShown) return;
// ... your code
userLocationShown = YES;
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您不希望将地图置于用户的中心,则可以删除以下行:
mapregion.center.latitude = map.userLocation.coordinate.latitude;
mapregion.center.longitude = map.userLocation.coordinate.longitude;
mapregion.span = MKCoordinateSpanMake(0.005,0.005);
[map setRegion:mapregion animated:YES];
Run Code Online (Sandbox Code Playgroud)
如果设备仍在获取位置更新,即使您忽略它们,它也会运行GPS并烧毁电池.一旦您锁定了他们的位置,或者通过检查他们移动了多少次,或者测量修复的准确性(CLLocation.horizontalAccuracy),您可以简单地关闭地图视图上的位置更新,如下所示:
self.showsUserLocation = NO;
Run Code Online (Sandbox Code Playgroud)
如果您一直在使用CLLocationManager,那就是
[locationManager stopUpdatingLocation];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5372 次 |
| 最近记录: |