Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用CLLocation&;MapView一起获取坐标并更新地图_Ios_Mapkit_Core Location - Fatal编程技术网

Ios 使用CLLocation&;MapView一起获取坐标并更新地图

Ios 使用CLLocation&;MapView一起获取坐标并更新地图,ios,mapkit,core-location,Ios,Mapkit,Core Location,我有一个应用程序需要做两件事 获取当前位置 将地图视图更新到该位置 我正在使用以下代码 -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { CLLocation *currentLocation = newLocation; if (currentL

我有一个应用程序需要做两件事

  • 获取当前位置
  • 将地图视图更新到该位置
  • 我正在使用以下代码

    -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation     *)newLocation fromLocation:(CLLocation *)oldLocation
    {
        CLLocation *currentLocation = newLocation;
        if (currentLocation != nil) {
        NSLog(@"%.8f", currentLocation.coordinate.longitude);
        NSLog(@"%.8f", currentLocation.coordinate.latitude);
        NSLog(@"%f", currentLocation.speed);
    
        MKCoordinateRegion mapRegion;
        mapRegion.center.longitude = currentLocation.coordinate.longitude;
        mapRegion.center.latitude = currentLocation.coordinate.latitude;
        mapRegion.span.latitudeDelta = 0.03;
        mapRegion.span.longitudeDelta = 0.03;
        [_mapView setRegion:mapRegion animated: YES];
    }
    }
    
    一切都如我所料,除了一件事。当地图视图区域更新时,地图会移动,但接点不会移动。最终,用户pin会离开屏幕,因为地图是按照坐标移动的


    任何帮助都将不胜感激。

    在下面设置“地图视图区域”的位置,您还应使用currentLocation创建一个新注释,并将其添加到“地图视图”中。代码应该是这样的

    -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation     *)newLocation fromLocation:(CLLocation *)oldLocation
    {
        CLLocation *currentLocation = newLocation;
        if (currentLocation != nil) {
        NSLog(@"%.8f", currentLocation.coordinate.longitude);
        NSLog(@"%.8f", currentLocation.coordinate.latitude);
        NSLog(@"%f", currentLocation.speed);
    
        MKCoordinateRegion mapRegion;
        mapRegion.center.longitude = currentLocation.coordinate.longitude;
        mapRegion.center.latitude = currentLocation.coordinate.latitude;
        mapRegion.span.latitudeDelta = 0.03;
        mapRegion.span.longitudeDelta = 0.03;
        [_mapView setRegion:mapRegion animated: YES];
    
        MyAnnotation *ant = [[MyAnnotation alloc] initWithCoordinate:currentLocation.coordinate];
        [_mapView addAnnotation:ant];
    }
    

    在设置_mapView区域的下方,还应使用currentLocation创建一个新注释,并将其添加到_mapView中。代码应该是这样的

    -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation     *)newLocation fromLocation:(CLLocation *)oldLocation
    {
        CLLocation *currentLocation = newLocation;
        if (currentLocation != nil) {
        NSLog(@"%.8f", currentLocation.coordinate.longitude);
        NSLog(@"%.8f", currentLocation.coordinate.latitude);
        NSLog(@"%f", currentLocation.speed);
    
        MKCoordinateRegion mapRegion;
        mapRegion.center.longitude = currentLocation.coordinate.longitude;
        mapRegion.center.latitude = currentLocation.coordinate.latitude;
        mapRegion.span.latitudeDelta = 0.03;
        mapRegion.span.longitudeDelta = 0.03;
        [_mapView setRegion:mapRegion animated: YES];
    
        MyAnnotation *ant = [[MyAnnotation alloc] initWithCoordinate:currentLocation.coordinate];
        [_mapView addAnnotation:ant];
    }