【问题标题】:Go to/zoom to current location function (MapKit)转到/缩放到当前位置功能(MapKit)
【发布时间】:2011-10-24 16:37:37
【问题描述】:

我有一个 mapView,它使用 viewDidLoad 缩放到当前位置:

#define METERS_PER_MILE 1609.344

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];

    mapView.showsUserLocation=TRUE;

    // zoom to  a specific area
    CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude = -28.994167;
    zoomLocation.longitude = 134.866944;

    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 1900*METERS_PER_MILE, 1900*METERS_PER_MILE);
    MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];    

    // make sure the Google water mark is always visible
    mapView.autoresizingMask =
    (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);

    [mapView setRegion:adjustedRegion animated:YES];        

    mapView.delegate=self;

    searchBar.delegate = self;
}

这很好用。我添加了一个搜索栏和一个跳转到特定地址位置的功能。这也很好用。我现在想添加一个按钮来跳回当前位置。请帮帮我好吗?

干杯

【问题讨论】:

    标签: iphone xcode mapkit currentlocation


    【解决方案1】:

    您需要点击该按钮将地图的中心设置为当前位置。像这样说:

    - (IBAction)showCurrentLocation {        
        [mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];
    }
    

    【讨论】:

    • 有没有叫setCenter的函数?
    • [mapView setCenterCoordinate:mapView.userLocation.location.coordinate Animation:YES];
    • @JohannesRudolph 别担心!
    【解决方案2】:

    你也可以试试:

    mapView.userTrackingMode=YES;
    mapView.userTrackingMode=NO;
    

    【讨论】:

    • 只要确保在 viewDidAppear 方法或更高版本中调用它即可。
    • userTrackingMode 不是BOOL。这是一个MKUserTrackingMode 枚举。将其设置为YES/NO“有效”的唯一原因是因为YES 等于MKUserTrackingModeFollow 并且NO 等于MKUserTrackingModeNone。但是userTrackingMode也可以是第三个值MKUserTrackingModeFollowWithHeading
    【解决方案3】:

    您可以将此 IBAction 链接到您的 UIButton, 它会在当前位置移动地图并放大它。

    @IBOutlet weak var mapView: MKMapView!
    
    @IBAction func zoomToUserCurrentLocation(sender: AnyObject) {
        if self.mapView != nil {
            self.mapView.setRegion(MKCoordinateRegionMake(
                self.mapView.userLocation.coordinate, 
                MKCoordinateSpanMake(0.1, 0.1)
            ), animated: true)
        }
    }
    

    MKCoordinateSpan 定义了一个地图区域所跨越的区域,这些值越小,地图缩放越近。

    【讨论】:

      【解决方案4】:
      - (void)showCurrentLocation{
      
          MKMapPoint annotationPoint = MKMapPointForCoordinate(self.mapView.userLocation.coordinate);
          MKMapRect zoomRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.0, 0.0);
          [self.mapView setVisibleMapRect:zoomRect animated:YES];
      }
      

      【讨论】:

        【解决方案5】:

        为了斯威夫特

        在按钮操作yourMKMapView.setUserTrackingMode(.follow, animated: true) 中添加这一行

        确保在viewDidLoad() 中添加yourMKMapView.showsUserLocation = true

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-05-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-06
          • 2014-04-03
          • 2013-08-27
          • 1970-01-01
          相关资源
          最近更新 更多