【问题标题】:update marker position in Google Maps in IOS app在 IOS 应用程序中更新谷歌地图中的标记位置
【发布时间】:2016-01-05 22:32:41
【问题描述】:

我需要更新绘制路线上的标记位置,直到它到达目的地。现在这里是访问服务器的代码:

     NSURL  *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=-32.90,151.80&waypoints=-33.30,151.50&alternatives=true",marker.position.latitude,marker.position.longitude]];

在此之后,我只是更新标记超时的位置,我计算了新位置。问题是我如何在给定路线上为我的标记获取新位置。我从我的儿子数据中得到它从上面的 URL 获取。所以这里是获取我的新位置的纬度和经度的代码。

    NSString *latitudeInString = responseObject[@"routes"][0][@"legs"][0][@"steps"][i][@"end_location"][@"lat"];
    CLLocationDegrees latitudeInDegrees = [latitudeInString doubleValue];
    NSString *longitudeInString = responseObject[@"routes"][0][@"legs"][0][@"steps"][i][@"end_location"][@"lng"];
    CLLocationDegrees longitudeInDegrees = [longitudeInString doubleValue];

所有这些代码都在 NSTimer 类的 selector 方法中。完整代码如下:

    -(void)callInOne:(NSTimer*)timer{

NSURL  *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=-32.90,151.80&waypoints=-33.30,151.50&alternatives=true",marker.position.latitude,marker.position.longitude]];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:urlRequest];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation,id responseObject){

    NSString *polyLinePath = responseObject[@"routes"][0][@"overview_polyline"][@"points"];
    GMSPath *path = [GMSPath pathFromEncodedPath:polyLinePath];
    GMSPolyline *line = [GMSPolyline polylineWithPath:path];
    line.spans = @[[GMSStyleSpan spanWithColor:[UIColor blackColor] segments:0.75]];
    line.strokeWidth = 2;
    line.map = mapView_;
    self.view = mapView_;
    NSString *latitudeInString = responseObject[@"routes"][0][@"legs"][0][@"steps"][i][@"end_location"][@"lat"];
    CLLocationDegrees latitudeInDegrees = [latitudeInString doubleValue];
    NSString *longitudeInString = responseObject[@"routes"][0][@"legs"][0][@"steps"][i][@"end_location"][@"lng"];
    CLLocationDegrees longitudeInDegrees = [longitudeInString doubleValue];
    marker.position = CLLocationCoordinate2DMake(latitudeInDegrees, longitudeInDegrees);
    NSLog(@"%d",i);
    i++;
} failure:^(AFHTTPRequestOperation *operation,id responseObject){


    NSLog(@"failure");
}];

[operation start];
}

请注意,我在这里使用的是 AFNetworking。现在的问题是我的标记正在随机更新并且我的代码崩溃了。知道是什么问题吗?错误是这样的:

    Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (7) beyond bounds (7)'.

请大家帮帮我。(关注开头分别提到的那 4 个语句。这就是代码被搞砸的地方)

【问题讨论】:

    标签: ios objective-c google-maps-markers


    【解决方案1】:

    这是解决方案:我需要的是仅更新标记的位置,但我正在更新标记位置,然后再次将该位置发送到服务器。所以我纠正了它,它工作得很好。这是代码为此:

        -(void)GETRequest{
    NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(callInOne:) userInfo:nil repeats:YES];
    NSURL  *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=-32.90,151.80&waypoints=-33.30,151.50&alternatives=true",marker.position.latitude,marker.position.longitude]];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    operation = [[AFHTTPRequestOperation alloc]initWithRequest:urlRequest];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];
    __weak ViewController *_self = self;
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation,id responseObject){
        myResponseObject = responseObject;
        NSString *polyLinePath = responseObject[@"routes"][0][@"overview_polyline"][@"points"];
        GMSPath *path = [GMSPath pathFromEncodedPath:polyLinePath];
        GMSPolyline *line = [GMSPolyline polylineWithPath:path];
        line.spans = @[[GMSStyleSpan spanWithColor:[UIColor blackColor] segments:0.75]];
        line.strokeWidth = 2;
        line.map = mapView_;
        _self.view = mapView_;
    } failure:^(AFHTTPRequestOperation *operation,id responseObject){
    
    
        NSLog(@"failure");
    }];
    
    [operation start];
    }
    

    这里是选择器方法:

        -(void)callInOne:(NSTimer*)timer{
    if(i<=30){
        NSString *latitudeInString = myResponseObject[@"routes"][0][@"legs"][0][@"steps"][i][@"end_location"][@"lat"];
        CLLocationDegrees latitudeInDegrees = [latitudeInString doubleValue];
        NSString *longitudeInString = myResponseObject[@"routes"][0][@"legs"][0][@"steps"][i][@"end_location"][@"lng"];
        CLLocationDegrees longitudeInDegrees = [longitudeInString doubleValue];
        marker.position = CLLocationCoordinate2DMake(latitudeInDegrees, longitudeInDegrees);
    }
    else if(i>31){
        NSString *latitudeInString = myResponseObject[@"routes"][0][@"legs"][1][@"steps"][i][@"end_location"][@"lat"];
        CLLocationDegrees latitudeInDegrees = [latitudeInString doubleValue];
        NSString *longitudeInString = myResponseObject[@"routes"][0][@"legs"][1][@"steps"][i][@"end_location"][@"lng"];
        CLLocationDegrees longitudeInDegrees = [longitudeInString doubleValue];
        marker.position = CLLocationCoordinate2DMake(latitudeInDegrees, longitudeInDegrees);
    
    }
    i++;
    }
    

    之前我在选择器方法中包含所有代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-18
      • 1970-01-01
      • 1970-01-01
      • 2011-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多