在IOS应用程序中更新Google Maps中的标记位置 [英] update marker position in Google Maps in IOS app

查看:64
本文介绍了在IOS应用程序中更新Google Maps中的标记位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要更新绘制路线上的标记位置,直到到达目的地为止.这是访问服务器的代码:

I need to update the marker position on the drawn route till it reaches its destination.Now here is the code for accessing server:

     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获取.因此这是获取新职位的纬度和经度的代码.

After this I am just updating the position of marker overtime I calculate the new position.The question arises how I am getting the new position for my marker on the given route.I am getting it from the son data that I'm getting from the above URL.So here is the code for getting the latitude and longitude for my new position.

    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类的选择器方法中.以下是完整的代码:

All this code is inside the selector method of NSTimer class.Here is complete code:

    -(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.现在的问题是我的标记随机更新,代码崩溃了.

Note that I'm using AFNetworking here.Now the problem is that my marker is getting updated randomly and my code crashes.Any idea what is the problem?The error is like:

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

请帮帮我.(专注于开头分别提到的那4条语句,那是代码被搞砸的地方

Please help me out guys.(focus on those 4 statements mentioned separately at the beginning.That is where the code is getting screwed)

推荐答案

这是解决方案:我需要的是仅更新标记的位置,但是我正在更新标记的位置,然后将该位置再次发送到服务器因此,我更正了它并使其完美运行.以下是该代码:

And here is the solution:What I needed was to update the position of marker only but I was updating the marker position and then sending that position to the server again.So I corrected that and it worked perfectly.Here is the code for that:

    -(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++;
}

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

Earlier I was including all the code in the selector method.

这篇关于在IOS应用程序中更新Google Maps中的标记位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆