PDA

View Full Version : iOS: How to use Boundings



MeltingElements
11.07.2014, 09:48
Hi,

I found in the Reference site the SKBoundingBox Class Reference but how can I use this when I have multiple pins on the map and center the view in relation to the pins?


Thank you :)

dandronic
11.07.2014, 09:50
Could you provide more details regarding what would you like to be able to do? Maybe SKBoundingBox is not the best solution

MeltingElements
11.07.2014, 09:52
I want to display a map with multiple Annotations. So when the user see the map, it should zoom out/in, so all annotations are shown.

dandronic
11.07.2014, 10:01
We have an example covering custom annotations in our demo project (http://developer.skobbler.com/support#download) (see the iOS demo project), and a chapter (http://developer.skobbler.com/getting-started/ios#sec09) in our Getting Started covering "adding annotations".

Are those sufficient or do you require something more?

MeltingElements
11.07.2014, 10:17
I know how to add annotations. the point is, that I'm showing multiple annotiations in the map. And these are at other points. So when the user is in the Map View the zoom level should be so, that all annotations are shown.

dandronic
11.07.2014, 10:19
Ok - now I thing I understand the question - you would like to zoom the map so that all annotations are visible - is this correct?

MeltingElements
11.07.2014, 10:35
Yes, that's correct :)

MeltingElements
15.07.2014, 09:16
So, you cant help me ?

dandronic
15.07.2014, 16:07
So:
You can achieve your scenario using the SKBoundingBox and the fitBounds:withPadding: method of the SKMapView like so:
//let's assume we have a list of annotations
NSArray *annotationList = @[self.annotation1, self.annotation2, self.annotation3];
//next, we calculate the bounding box of the annotations
//you can do this manually or use the boundingBoxIncludingLocation: method. We'll use boundingBoxIncludingLocation:
//initialize the bounding box to enclose just the first annotation
//iterate over all the annotations and use boundingBoxIncludingLocation: which will expand the box, eventually including all the annotations
SKBoundingBox *box = [SKBoundingBox boundingBoxWithTopLeftCoordinate:self.annotation1. location bottomRightCoordinate:self.annotation1.location];
for (SKAnnotation *annotation in annotationList) {
box = [box boundingBoxIncludingLocation:annotation.location];
}

//use some padding so that the annotation icons at the edge of the box are visible
[self.mapView fitBounds:box withPadding:CGSizeMake(50.0, 50.0)];

MeltingElements
16.07.2014, 09:26
So:
You can achieve your scenario using the SKBoundingBox and the fitBounds:withPadding: method of the SKMapView like so:
//let's assume we have a list of annotations
NSArray *annotationList = @[self.annotation1, self.annotation2, self.annotation3];
//next, we calculate the bounding box of the annotations
//you can do this manually or use the boundingBoxIncludingLocation: method. We'll use boundingBoxIncludingLocation:
//initialize the bounding box to enclose just the first annotation
//iterate over all the annotations and use boundingBoxIncludingLocation: which will expand the box, eventually including all the annotations
SKBoundingBox *box = [SKBoundingBox boundingBoxWithTopLeftCoordinate:self.annotation1. location bottomRightCoordinate:self.annotation1.location];
for (SKAnnotation *annotation in annotationList) {
box = [box boundingBoxIncludingLocation:annotation.location];
}

//use some padding so that the annotation icons at the edge of the box are visible
[self.mapView fitBounds:box withPadding:CGSizeMake(50.0, 50.0)];

boundingBoxIncludingLocation is not shown as a function?

EDIT: Ok it's in the new Version. I will check the code now.

EDIT2: It's not working. [self.mapView fitBounds:box withPadding:CGSizeMake(50.0, 50.0)]; does a strange behaviour. The map zooms like it's in zoomlevel 20, despite zoomlevel = 12.
So.. maybe another solution?

dandronic
22.07.2014, 13:44
We've attached a demo file over at https://www.dropbox.com/s/1un9l9ywexcad55/AnnotationsViewController.m that can be integrated in the demo project and should illustrate how to arrive at your scenario - please take a look and see if this satisfies your needs.

P.S. We could not reproduce the behaviour you've experienced

MeltingElements
28.07.2014, 09:35
Sorry but this is not what I want. Furthermore in this script you commented out the other annotation. So in which way it should illustrate my scenario? There should be multiple annoations and I want that the region center is the middle of e.g. three annoations!

dandronic
18.08.2014, 08:56
The problem regarding "self.mapView fitBounds:box withPadding:CGSizeMake(50.0, 50.0)]; does a strange behaviour. The map zooms like it's in zoomlevel 20, despite zoomlevel = 12. " will be fixed in the next version (September's update)
Meanwhile, in order for it to work it can be called after viewWillAppear.

dandronic
05.09.2014, 10:46
Quick update: this bug was fixed and will be deployed with the 2.2 SDK update (ETA week 37 or week 38, 2014)

high5fly
20.05.2016, 14:41
Hi,

I have a working version of what you want to do, it is just fitting a route instead of annotations. Here is my code, hope it helps you:



@implementation BRVMapPreview

- (void)awakeFromNib {

[super awakeFromNib];
}

- (void)setupSkMapView {

self.skMapView = [[SKMapView alloc] initWithFrame:self.bounds];
self.skMapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.skMapView.mapScaleView.hidden = YES;
self.skMapView.delegate = self;
self.skMapView.settings.rotationEnabled = YES;
self.skMapView.settings.showCurrentPosition = NO;
self.skMapView.settings.followUserPosition = NO;
self.skMapView.settings.showCompass = NO;
self.skMapView.settings.displayMode = SKMapDisplayMode2D;
self.skMapView.settings.headingMode = SKHeadingModeNone;
SKMapZoomLimits zoomLimits;
zoomLimits.mapZoomLimitMin = 7;
zoomLimits.mapZoomLimitMax = 21;
self.skMapView.settings.zoomLimits = zoomLimits;
[self addSubview:self.skMapView];
}

#pragma mark - Helpers
- (void)drawRouteFromArray:(NSArray *)locations {

self.isFit = NO;
[self setupSkMapView];
CLLocation *firstLocation = [locations firstObject];
CLLocation *lastLocation = [locations lastObject];

self.boundaries = [SKBoundingBox boundingBoxWithTopLeftCoordinate:firstLocation.coo rdinate bottomRightCoordinate:lastLocation.coordinate];

for (CLLocation *location in locations) {
self.boundaries = [self.boundaries boundingBoxIncludingLocation:location.coordinate];
}
self.polyline.coordinates = locations;
[self.skMapView addPolyline:self.polyline];

}

- (void)addPOIs:(NSArray *)pois {

for (BRVPOIComponent *poi in pois) {

SKAnnotation *annotation = [SKAnnotation new];
annotation.identifier = (int)poi.poiId + 11;

[self.skMapView addAnnotation:annotation withAnimationSettings:[SKAnimationSettings defaultAnimationSettings]];
}
}


#pragma mark - Getter
- (SKPolyline *)polyline {

if (!_polyline) {
_polyline = [SKPolyline polyline];
_polyline.identifier = 11;
_polyline.borderDotsSize = 0.1;
_polyline.borderDotsSpacingSize = 0.1;
_polyline.fillColor = [UIColor brv_buttonTextBlueWithAlpha:0.5];
_polyline.lineWidth = 16;
}
return _polyline;
}

#pragma mark - SKMapViewDelegate
- (void)mapView:(SKMapView *)mapView didChangeToRegion:(SKCoordinateRegion)region {

if (self.isFit) {

return;
}
else {
[self.skMapView fitBounds:self.boundaries withPadding:CGSizeMake(20.0, 20.0)];
self.isFit = YES;
}
}

Adela_Silvia
08.08.2016, 16:13
Thank you high5fly for sharing this!

high5fly
08.08.2016, 16:14
Hi Adele,

I have to share the updated version after updating to 3.0.

KR,
Miro