IOS: zoom in mapkit for two annotation point - ios

I want to know if there is a way to have a dynamic zoom when I set in my mapview the blue dot (my position) and another pin...when I load mapview I want to show these two point in the same frame...is it possible? thanks

Yes, it is possible. After adding the second annotation. This sample code should work for any number of annotations:
MKMapRect zoomRect = MKMapRectNull;
NSArray *annotations = [mapView annotations];
for (MKAnnotation *annotation in annotations) {
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
if (MKMapRectIsNull(zoomRect)) {
zoomRect = pointRect;
} else {
zoomRect = MKMapRectUnion(zoomRect, pointRect);
}
}
[mapView setVisibleMapRect:zoomRect animated:YES];

Related

How to show current location and all annotation pin on map together iOS 8

How will I show all added annotation and current location blue dot together on map visible rect?
That user can see all pins and his current location when map will load without dragging the map initially. Code for ios8 and ios7.0.
I am using MKMapKit of apple.
try this
MKMapRect zoomRect = MKMapRectNull;
for (id <MKAnnotation> annotation in mapView.annotations) {
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
if (MKMapRectIsNull(zoomRect)) {
zoomRect = pointRect;
} else {
zoomRect = MKMapRectUnion(zoomRect, pointRect);
}
}
[mapView setVisibleMapRect:zoomRect animated:YES];

Exclude userLocation in MapKit search

I am using MapKit with Xcode 6, and everything I have coded up to this point has worked fine. I have a text field that allows users to input any string to search Apple's map.
However, one of the issues I am having is that when the search results are returned as pins on the map, I would like the zoom to fit ONLY the results, excluding the userLocation icon.
Here is the code I have so far. (I have seen similar code with updates of adding lines to include the userLocation, however the code they say should not contain the userLocation is very similar to what I have already...)
if (response.mapItems.count == 0)
NSLog(#"No results");
else
for (MKMapItem *item in response.mapItems)
{
MKMapRect mr = [self.mapView visibleMapRect];
MKMapPoint pt = MKMapPointForCoordinate([annotation coordinate]);
mr.origin.x = pt.x - mr.size.width *0.5; // 0.5
mr.origin.y = pt.y - mr.size.width * 0.75; //0.75
[self.mapView setVisibleMapRect:mr animated:YES];
MKMapRect zoomRect = MKMapRectNull;
for (id <MKAnnotation> annotation in _mapView.annotations)
{
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 10.4, 10.4);
zoomRect = MKMapRectUnion(zoomRect, pointRect);
}
[_mapView setVisibleMapRect:zoomRect animated:YES];
}
}];
}
Just check to make sure the annotation is not a MKUserLocation object:
MKMapRect zoomRect = MKMapRectNull;
for (id <MKAnnotation> annotation in _mapView.annotations) {
if (![annotation isKindOfClass:[MKUserLocation class]]) {
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 10.4, 10.4);
zoomRect = MKMapRectUnion(zoomRect, pointRect);
}
}
[_mapView setVisibleMapRect:zoomRect animated:YES];
By the way, I think you want to do this after you finish iterating through the map points, not after each and every one. Though, this means that you should double check to make sure you had one or more map points before doing this.

iOS Google Map Dynamic Zoom

I have a Google Map that's plotting a set of locations closest to the current GPS. How do I let the Map automatically zoom to just enough so that I can see the 5 closest locations in the map (but not anything bigger)
This is the implementation using MapKit and MKAnnotations. If you're using the Google Map SDK you just need to change the logic to use GMSMarkers and the related APIs instead.
// Zoom Map to Fit Annotation
MKMapRect zoomRect = MKMapRectNull;
for (id <MKAnnotation> annotation in self.mapView.annotations) {
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1);
if (MKMapRectIsNull(zoomRect)) {
zoomRect = pointRect;
}
else {
zoomRect = MKMapRectUnion(zoomRect, pointRect);
}
}
[self.mapView setVisibleMapRect:zoomRect edgePadding:UIEdgeInsetsMake(0.2, 0.2, 0.2, 0.2) animated:YES];

MKMap zoom to include annotations and region

I am trying to set the zoom level of the map to include just the annotations, but also don't zoom below say a 3 mile radius, but I also don't the annotations touching the side of the screens. Further, I would also like the annotations once zoomed to be within the bottom half self.view (my map takes up the entire screen).
I tried the following two ways and neither works.
// #1 ///// Always zooms to show my annotation in Texas, zoomed out to far I can't even see US states
-(void)zoomToFitMapAnnotations:(MKMapView*)aMapView {
if([aMapView.annotations count] == 0) {
return;
}
CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = -180;
CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;
for (id <MKAnnotation> annotation in self.mapView.annotations){
topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);
bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
}
MKCoordinateRegion region;
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
//Add a little space on both sides
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1;
//Add a little extra space on bith sides
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1;
region = [aMapView regionThatFits:region];
[self.mapView setRegion:region animated:YES];
}
// #2 ///// This works best, but it zooms in to much; right on top of a house, if there is only one annotation, but I want to see the one annotation and a 4 mile radius. So I never want to zoom less than having 4 square miles shown, UNLESS the user manually zooms it.
MKMapRect zoomRect = MKMapRectNull;
for (id <MKAnnotation> annotation in self.mapView.annotations)
{
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1);
zoomRect = MKMapRectUnion(zoomRect, pointRect);
}
[self.mapView setVisibleMapRect:zoomRect edgePadding:UIEdgeInsetsMake(300, 300, 100, 300) animated:YES];
MKMapRect zoomRect = MKMapRectNull;
for (id <MKAnnotation> annotation in self.mapView.annotations) {
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1);
if (MKMapRectIsNull(zoomRect)) {
zoomRect = pointRect;
}else{
zoomRect = MKMapRectUnion(zoomRect, pointRect);
}
}
if (zoomRect.size.width == 0.10) /* for single annotation available in map */
{
zoomRect = MKMapRectMake(zoomRect.origin.x, zoomRect.origin.y, 100000, 100000);
}
[[self mapView] setVisibleMapRect:zoomRect edgePadding:UIEdgeInsetsMake(50, 50, 50, 50) animated:YES];

Showing multiple annotation with user location at center

all
I need to show multiple annotations within a mapview and zoom to show all this annotations.
I have the code fore that
MKMapRect zoomRect = MKMapRectNull;
int i = 0;
for (Post *post in _allPostsToList) {
if (((post.location2D.latitude == 0.0f)&&(post.location2D.longitude == 0.0f))||(!CLLocationCoordinate2DIsValid(post.location2D))) {
i++;
continue;
}
MyAnnotation *annotation = [[MyAnnotation alloc] initWithpost:post];
annotation.tag = i++;
[mapView addAnnotation:annotation];
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
zoomRect = (MKMapRectIsNull(zoomRect))?pointRect:MKMapRectUnion(zoomRect, pointRect);
}
[mapView setVisibleMapRect:zoomRect animated:YES];
it works fine but, I need one more thing with this, Needs user location blue bubble in the center of the map with showing all the above annotation.
How to do this.
I tried this
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
MKMapRect zoomRect = mapView.visibleMapRect;
MKMapPoint annotationPoint = MKMapPointForCoordinate(newLocation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
zoomRect = (MKMapRectIsNull(zoomRect))?pointRect:MKMapRectUnion(zoomRect, pointRect);
[mapView setVisibleMapRect:zoomRect animated:YES];
[manager stopUpdatingLocation];
}
Its working
But its not centering user location
How to dow this. Can anyone solve this problem
In didUpdateToLocation you get the current visible Rect (zoomRect), then if it is not null you're joining it with the pointRect and zooming/panning to that. How would that end up centered on the pointRect? It would only happen if the two of them had exactly the same center.
If you want to be centered on the userlocation but show all the points you could get the maximum distance of the points from the userlocation and create a maprect with a center and a span to match. But if the points were all off in one direction and the userlocation is in the middle of the screen, the other side of the screen is wasted.
Just a bet; what about accessing your mapView's region and try to set the new center:
MKCoordinateRegion region = mapView.region;
region.center = newLocation.coordinates;
[mapView setRegion:region animated:YES];

Resources