Multiple overlays vs continous redrawing im MKMapView - ios

I am drawing users location path on MKMapView live and I am wondering what's better approach.
Currently I am gathering all location data in location manager and I am redrawing whole overlay every second.
- (void)showLines {
NSArray *locations = [[SHLocationManager sharedInstance] locations];
CLLocationCoordinate2D *pointsCoordinate = (CLLocationCoordinate2D *)malloc(sizeof(CLLocationCoordinate2D) * locations.count);
[locations enumerateObjectsUsingBlock:^(CLLocation *location, NSUInteger idx, BOOL *stop) {
pointsCoordinate[idx] = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);
}];
MKPolyline *polyline = [MKPolyline polylineWithCoordinates:pointsCoordinate count:locations.count];
free(pointsCoordinate);
[self.mapView removeOverlays:self.mapView.overlays];
[self.mapView addOverlay:polyline];
}
- (MKPolylineRenderer *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay{
MKPolylineRenderer *polylineView = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
polylineView.strokeColor = [UIColor colorWithRed:0.17 green:0.57 blue:0.85 alpha:1];
polylineView.lineWidth = 8.0;
polylineView.alpha = 1;
return polylineView;
}
Is it better to draw a line for every pair of point's I get from location manager?
It would create number of overlays - don't know how it would affect performance.
What's better in your opinion?

Related

Recording the movement of the user in 10 meter increments and displaying it as a line in MKMapView doesn’t work

I‘m a beginner trying to solve a problem in which I have to display the location path of the user every ten meters after the user hits ‚start‘.
I‘ve set up the location manager. The location is being updated, the problem is that it doesn't show a visual path between the starting point and the current user location.
How do I show the visual path?
I tried to calculate the route with MKDirectionsRequest and -Response and it worked well with two given locations, which in this case I don't have though. I also tried the MKMapPoints but to be honest it isn't the point of our homework, in that it would make it unnecessarily complicated.
Here is my current state:
- (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
CLLocation *newLocation;
newLocation = [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(newLocation.coordinate.latitude, newLocation.coordinate.longitude) altitude:kCLLocationAccuracyNearestTenMeters horizontalAccuracy:kCLLocationAccuracyNearestTenMeters verticalAccuracy:newLocation.verticalAccuracy timestamp:newLocation.timestamp];
newLocation = [locations lastObject];
if (newLocation.horizontalAccuracy < 0) {
return;
}
NSInteger count = [locations count];
if (count > 1) {
CLLocationCoordinate2D coordinates[count];
for (NSInteger i = 0; i < count; i++) {
coordinates[i] = [(CLLocation *)locations[i] coordinate];
}
MKPolyline *polyline = [MKPolyline polylineWithCoordinates:coordinates count:count];
[self.mapView addOverlay:polyline level:MKOverlayLevelAboveRoads];
}
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.wegAnnotation.coordinate, 200, 200);
[mapView setRegion:region];
[manager stopUpdatingLocation];
self.myLocationManager = nil;
}
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
renderer.strokeColor = [UIColor blueColor];
renderer.lineWidth = 5.0;
return renderer;
}

Remove Multiple Polygon On google Map Objective C

I am developing Iphone app and using Google map.Draw Multiple Polygon on google Map. but than deleting the polygon on map that deleting the last polygon not delete all draw polygon on google map.please help thanks in advance.
code..
// Draw Polygon
- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate {
if (buttonClicked == true) {
NSLog(#"You tapped at %f,%f", coordinate.latitude, coordinate.longitude);
_latLongDict = #{#"lat":#(coordinate.latitude), #"long":#(coordinate.longitude)};
[_clickCoordinate addObject:_latLongDict];
NSLog(#"%#",_clickCoordinate);
_path = [GMSMutablePath path];
CLLocationCoordinate2D event;
for (NSDictionary *dic in _clickCoordinate) {
event.latitude = [[dic valueForKey:#"lat"] floatValue];
event.longitude = [[dic valueForKey:#"long"] floatValue];
[_path addCoordinate:event];
}
_polygun = [GMSPolygon polygonWithPath:_path];
_polygun.fillColor = [UIColor colorWithRed:0.25 green:0 blue:0 alpha:0.05];
_polygun.strokeColor = [UIColor blackColor];
_polygun.strokeWidth = 2;
_polygun.map = mapView;
}
// Delete Polygon
- (IBAction)cancelButton:(id)sender {
for (int i = 0; i < _path.count; i++) {
_polygun.map = nil;
[_clickCoordinate removeAllObjects];
[_path removeAllCoordinates];
NSLog(#"%#",_clickCoordinate);
}
}

Trying to implement a polyline but it doesn't appear

I am trying to get a polyline which should be there in my track application. I have gone through various questions and websites regarding this but I can't really guess where I am going wrong. It will be great if someone can help me out? :)
My Code -->
CLLocation *currentLocation = [locations lastObject];
[linerArray addObject:currentLocation];
CLLocationDegrees lat = currentLocation.coordinate.latitude;
CLLocationDegrees lngt= currentLocation.coordinate.longitude;
CLLocationCoordinate2D lccoordinates = CLLocationCoordinate2DMake(lat, lngt);
NSInteger calculateCoordinates= linerArray.count;
CLLocationCoordinate2D coordinates[calculateCoordinates];
for (NSInteger initialStart= 0; initialStart < calculateCoordinates; initialStart ++) {
CLLocation *lct = [linerArray objectAtIndex:initialStart];
CLLocationCoordinate2D cord2 = lct.coordinate;
coordinates[initialStart] = cord2;
}
//drawing a polyline
MKPolyline *pline = [MKPolyline polylineWithCoordinates:coordinates count:calculateCoordinates];
[self.viewMap addOverlay:pline];
}
//Designing overlay polyline or setting polyine view
-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay{
MKPolylineRenderer *polylineRender = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
polylineRender.lineWidth = 7.0f;
polylineRender.strokeColor = [UIColor blueColor];
return polylineRender;
}

MKDirections not showing route on map in iOS

I am working on one app, I need to show route directions between two coordinates. I have used MKDirections and have passed two coordinates as source and destination, however on mapView its not showing any route or drawing any polyline. Below is my code. In MKDirections its always showing nil. Please let me know what I am doing wrong.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.activityIndicator.hidden = YES;
self.routeDetailsButton.hidden = YES;
self.routeDetailsButton.enabled = NO;
self.mapView.delegate = self;
self.mapView.showsUserLocation = YES;
self.navigationItem.title = #"RouteMaster";
}
#pragma mark - MapKit delegate methods
- (void)mapView:(MKMapView *)aMapView didUpdateUserLocation:(MKUserLocation *)userLocation {
CLLocationCoordinate2D loc = [userLocation.location coordinate];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 1000.0f, 1000.0f);
[self.mapView setRegion:region animated:YES];
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (IBAction)handleRoutePressed:(id)sender {
// We're working
CLLocationCoordinate2D sourceCoords = CLLocationCoordinate2DMake(28.6100, 77.2300);
MKPlacemark *sourcePlacemark = [[MKPlacemark alloc] initWithCoordinate:sourceCoords addressDictionary:nil];
MKMapItem *srcMapItem = [[MKMapItem alloc]initWithPlacemark:sourcePlacemark];
CLLocationCoordinate2D destinationCoords = CLLocationCoordinate2DMake(18.9750, 72.8258);
MKPlacemark *destinationPlacemark = [[MKPlacemark alloc] initWithCoordinate:destinationCoords addressDictionary:nil];
MKMapItem *distMapItem = [[MKMapItem alloc]initWithPlacemark:destinationPlacemark];
MKDirectionsRequest *request = [[MKDirectionsRequest alloc]init];
[request setSource:srcMapItem];
[request setDestination:distMapItem];
MKDirections *direction = [[MKDirections alloc]initWithRequest:request];
[direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
if (error)
NSLog(#"Error %#", error.description);
else
NSLog(#"response = %#",response);
NSArray *arrRoutes = [response routes];
[arrRoutes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
MKRoute *rout = obj;
MKPolyline *line = [rout polyline];
[self.mapView addOverlay:line];
NSLog(#"Rout Name : %#",rout.name);
NSLog(#"Total Distance (in Meters) :%f",rout.distance);
NSArray *steps = [rout steps];
NSLog(#"Total Steps : %lu",(unsigned long)[steps count]);
[steps enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(#"Rout Instruction : %#",[obj instructions]);
NSLog(#"Rout Distance : %f",[obj distance]);
}];
}];
}];
}
#pragma mark - Utility Methods
- (void)plotRouteOnMap:(MKRoute *)route
{
if(_routeOverlay) {
[self.mapView removeOverlay:_routeOverlay];
}
// Update the ivar
_routeOverlay = route.polyline;
// Add it to the map
[self.mapView addOverlay:_routeOverlay];
}
#pragma mark - MKMapViewDelegate methods
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
renderer.strokeColor = [UIColor redColor];
renderer.lineWidth = 4.0;
return renderer;
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay {
if ([overlay isKindOfClass:[MKPolyline class]]) {
MKPolylineView* aView = [[MKPolylineView alloc]initWithPolyline:(MKPolyline*)overlay] ;
aView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.5];
aView.lineWidth = 10;
return aView;
}
return nil;
}
Mostly this error occur if [_mapView setRegion:region] area and polyline route path are not on same region(both should need to display on same screen).
In my case I was searching for USA route path while my region was India. So it wont call to -(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id)overlay; function.
add this method
-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
MKPolylineRenderer * routeLineRenderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
routeLineRenderer.strokeColor = [UIColor blueColor];
routeLineRenderer.lineWidth = 4;
return routeLineRenderer;
}

unable to draw mkpolyline

I have used MKMapView to show map and current user location which is working correctly.
Now I want to draw a polyline as user moves but it is not working I tried follwing code:
for(int i = 0; i < [longarray count]; i++)
{
NSNumber *latt=[latarray objectAtIndex:i];
NSNumber *lonn=[longarray objectAtIndex:i];
sklat =[[NSString stringWithFormat:#"%#",latt]doubleValue];
sklongi =[[NSString stringWithFormat:#"%#",lonn]doubleValue];
CLLocationCoordinate2D coordinate1 = CLLocationCoordinate2DMake(sklat,sklongi);
// break the string down even further to latitude and longitude fields.
MKMapPoint point = MKMapPointForCoordinate(coordinate1);
// if it is the first point, just use them, since we have nothing to compare to yet.
pointsArray[i] = point;
}
self.routeLine = [MKPolyline polylineWithPoints:pointsArray count:[latarray count]];
free(pointsArray);
[mapview addOverlay:self.routeLine];
then i usedd overlay function as
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
MKOverlayView* overlayView = nil;
if(overlay == self.routeLine)
{
routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine] ;
routeLineView.fillColor = [UIColor colorWithRed:0.000 green:5.100 blue:0.100 alpha:1];
routeLineView.strokeColor = [UIColor colorWithRed:0.000 green:5.100 blue:0.100 alpha:1];
routeLineView.lineWidth = 4;
overlayView = routeLineView;
}
return overlayView;
}

Resources