I am implementing Google map, I need to clear map data (all Polylines + markers) after every some on seconds (30-50 seconds). As we know that we can do it with [yourMap clear];
But when I do this, then Google map little animates (just after called clear method), and then it show new data on Map, I don't want this little animation. NOTE: I am calling this method drawPolylineOnMap_ShowDriverCurrentLoc every after 20-50 seconds to show latest data on map.
-(void)drawPolylineOnMap_ShowDriverCurrentLoc :(NSDictionary*)dicResponse
{
// clear current map
[mapViewGMS clear];
NSString *driver_coordinates= [dicResponse valueForKey:@"one"];
NSArray *itemsDriver = [driver_coordinates componentsSeparatedByString:@","]; //take the one array for split the string
NSString *sourceLongi=[itemsDriver objectAtIndex:0]; //shows Description
NSString *sourceLati=[itemsDriver objectAtIndex:1]; //
NSString *clientLoc= [dicResponse valueForKey:@"two"];
NSArray *itemsClient = [clientLoc componentsSeparatedByString:@","]; //take the one array for split the string
NSString *destLongi=[itemsClient objectAtIndex:0]; //shows Description
NSString *destLati=[itemsClient objectAtIndex:1]; //Shows Data
CLLocation *myOrigin=[[CLLocation alloc]initWithLatitude:[sourceLati floatValue] longitude:[sourceLongi floatValue]];
CLLocation *myDestination=[[CLLocation alloc]initWithLatitude:[destLati floatValue] longitude:[destLongi floatValue]];
[self fetchPolylineWithOriginForDriverCurrLoc:myOrigin destination:myDestination completionHandler:^(GMSPolyline *polyline)
{
dispatch_async(dispatch_get_main_queue(), ^{
if(polyline) {
polyline.map = mapViewGMS;
}
});
}];
}
-(void)fetchPolylineWithOriginForDriverCurrLoc:(CLLocation *)origin destination:(CLLocation *)destination completionHandler:(void (^)(GMSPolyline *))completionHandler
{
NSString *destLati= [NSString stringWithFormat:@"%f", destination.coordinate.latitude];
NSString *destLongi= [NSString stringWithFormat:@"%f", destination.coordinate.longitude];
NSString *originString = [NSString stringWithFormat:@"%f,%f", origin.coordinate.latitude, origin.coordinate.longitude];
NSString *destinationString = [NSString stringWithFormat:@"%f,%f", destination.coordinate.latitude, destination.coordinate.longitude];
NSString *directionsAPI = @"https://maps.googleapis.com/maps/api/directions/json?";
NSString *directionsUrlString = [NSString stringWithFormat:@"%@&origin=%@&destination=%@&mode=driving", directionsAPI, originString, destinationString];
NSURL *directionsUrl = [NSURL URLWithString:directionsUrlString];
NSURLSessionDataTask *fetchDirectionsTask = [[NSURLSession sharedSession] dataTaskWithURL:directionsUrl completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if(error)
{
if(completionHandler)
completionHandler(nil);
return;
}
NSArray *routesArray = [json objectForKey:@"routes"];
__block GMSPolyline *polyline = nil;
if ([routesArray count] > 0)
{
NSDictionary *routeDict = [routesArray objectAtIndex:0];
NSDictionary *routeOverviewPolyline = [routeDict objectForKey:@"overview_polyline"];
NSString *points = [routeOverviewPolyline objectForKey:@"points"];
dispatch_async(dispatch_get_main_queue(), ^{
GMSPath *path = [GMSPath pathFromEncodedPath:points];
polyline = [GMSPolyline polylineWithPath:path];
polyline.strokeColor = [UIColor redColor];//[UIColor colorWithRed:156/255.0f green:52/255.0f blue:104/255.0f alpha:1.0];
polyline.strokeWidth = 3.1f;
polyline.map = mapViewGMS ;
// add marker on source location
CLLocationCoordinate2D source = CLLocationCoordinate2DMake(origin.coordinate.latitude,origin.coordinate.longitude );
GMSMarker *markerSource = [GMSMarker markerWithPosition:source];
// markerSource.title = [NSString stringWithFormat:@"Estimated Start Trip at %@",globalTripStartTime];
markerSource.icon=[UIImage imageNamed:@"markerGreenCircle"];
markerSource.appearAnimation=0.2f;
markerSource.map = mapViewGMS;
// add marker on destination location
CLLocationCoordinate2D destiCordin = CLLocationCoordinate2DMake([destLati floatValue],[destLongi floatValue] );
GMSMarker *markerDestination = [GMSMarker markerWithPosition:destiCordin];
//markerDestination.title = [NSString stringWithFormat:@"Estimated End Trip at %@",globalTripEndTime];
markerDestination.icon=[UIImage imageNamed:@"markerRedCircle"];
markerDestination.appearAnimation=0.2f;
markerDestination.map = mapViewGMS;
// Focus map on pick up location, first find center b/w 2 locations
CLLocationCoordinate2D midpointBetweenCoordinate= [self midpointBetweenCoordinate:origin andCoordinate:destination];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:midpointBetweenCoordinate.latitude
longitude:midpointBetweenCoordinate.longitude
zoom:10.5];
mapViewGMS.camera=camera;
});
}
if(completionHandler)
completionHandler(polyline);
}];
[fetchDirectionsTask resume];
}