fit to zoom in maps on iphone - iphone

I am currently working on google maps.Using the GPS, I obtain the user's lat long information and hit a web service to obtain the nearest 5 stores available. I am able to plot the pins for it but the problem is how to fit the 5 pins to a maximum zoom scale ? I mean how does one obtain the latitude and longitude delta of the span in order to zoom as well as fit all the pins at the same time?

Try the following...
-(void)zoomToFitMapAnnotations:(MKMapView*)mapView
{
if([mapView.annotations count] == 0)
return;
CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;
CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;
for(DisplayMap* annotation in 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;
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1;
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1;
region = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];
}

There is a good post on how to do this here
The finished code I used to achieve this is here...
-(void)zoomToFitMapAnnotations:(MKMapView*)mv
{
if([mv.annotations count] == 0)
return;
CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;
CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;
for(MapViewAnnotate* annotation in mv.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;
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1; // Add a little extra space on the sides
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1; // Add a little extra space on the sides
region = [mv regionThatFits:region];
[mv setRegion:region animated:YES];
}
This assumes you're using annotation objects in from an annotation class similar to this (stored in an array)....
MapViewAnnotate.h
#interface MapViewAnnotate : NSObject <MKAnnotation>
{
NSString *title;
NSString *subtitle;
CLLocationCoordinate2D coordinate;
}
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *subtitle;
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t subtitle:(NSString *)s;
#end
MapViewAnnotate.m
#import "PropertyMapViewAnnotate.h"
#implementation MapViewAnnotate
#synthesize coordinate, title, subtitle;
- (id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t subtitle:(NSString *)s
{
[super init];
coordinate = c;
[self setTitle:t];
[self setSubtitle:s];
return self;
}
- (void)dealloc
{
[title release];
[subtitle release];
[super dealloc];
}
#end

Related

Setting region in MKMapView

I have 10 - 15 lat long to show in map at a time. How should I set region so that I can see all the annotations on the map.
create class as below. create two class: ViewController is class of UIViewController and MapViewAnnotation is class of NSObject. I have created that two class as below. bind mapview in XIB of ViewController and set delegate.
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "MapViewAnnotation.h"
#interface ViewController : UIViewController<MKMapViewDelegate>{
IBOutlet MKMapView* mapView;
NSMutableArray *arrayLocation;
}
#end
#import "ViewController.h"
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
arrayLocation = [[NSMutableArray alloc]init];
NSMutableArray *arrAnnotations = [[NSMutableArray alloc]init];
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setValue:#"32.774125" forKey:#"latitude"];
[dict setValue:#"-117.240658" forKey:#"longitude"];
[dict setValue:#"Bars & Restaurants 1" forKey:#"title"];
[arrayLocation addObject:dict];
dict = nil;
dict = [[NSMutableDictionary alloc]init];
[dict setValue:#"32.784526" forKey:#"latitude"];
[dict setValue:#"-117.240985" forKey:#"longitude"];
[dict setValue:#"Bars & Restaurants 2" forKey:#"title"];
[arrayLocation addObject:dict];
dict = nil;
dict = [[NSMutableDictionary alloc]init];
[dict setValue:#"32.773477" forKey:#"latitude"];
[dict setValue:#"-117.241144" forKey:#"longitude"];
[dict setValue:#"Bars & Restaurants 3" forKey:#"title"];
[arrayLocation addObject:dict];
dict = nil;
dict = [[NSMutableDictionary alloc]init];
[dict setValue:#"32.775301" forKey:#"latitude"];
[dict setValue:#"-117.238893" forKey:#"longitude"];
[dict setValue:#"Bars & Restaurants 4" forKey:#"title"];
[arrayLocation addObject:dict];
dict = nil;
for(int i=0;i<[arrayLocation count];i++)
{
CLLocationCoordinate2D location;
location.latitude = [[[arrayLocation objectAtIndex:i] objectForKey:#"latitude"] doubleValue];
location.longitude = [[[arrayLocation objectAtIndex:i] objectForKey:#"longitude"] doubleValue];
MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:[[arrayLocation objectAtIndex:i] objectForKey:#"title"] Coordinate:location andIndex:i];
[arrAnnotations addObject:newAnnotation];
}
[mapView addAnnotations:arrAnnotations];
mapView.region = [MapViewAnnotation regionForAnnotations:arrAnnotations];
}
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
if (annotation == mapView.userLocation)
{
return nil;
}
MKPinAnnotationView *pin = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier: #"restMap"];
if (pin == nil)
{
pin = [[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: #"restMap"];
}
else
{
pin.annotation = annotation;
}
pin.pinColor = MKPinAnnotationColorRed;
pin.animatesDrop = NO;
pin.canShowCallout=TRUE;
UIButton *btn=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
MapViewAnnotation *temp = (MapViewAnnotation *)pin.annotation;
btn.tag=temp.index;
pin.rightCalloutAccessoryView=btn;
[btn addTarget:self action:#selector(openDetail:) forControlEvents:UIControlEventTouchUpInside];
return pin;
}
#end
Implementation of MapViewAnnotation class
#import <MapKit/MapKit.h>
#interface MapViewAnnotation : NSObject <MKAnnotation , MKMapViewDelegate>{
NSString *title;
int index;
CLLocationCoordinate2D coordinate;
}
#property (nonatomic, copy) NSString *title;
#property (nonatomic, readwrite) int index;
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d;
- (id)initWithTitle:(NSString *)ttl Coordinate:(CLLocationCoordinate2D)c2d andIndex:(int)intIndex;
+(MKCoordinateRegion) regionForAnnotations:(NSArray*) annotations ;
#end
#import "MapViewAnnotation.h"
#implementation MapViewAnnotation
#synthesize title, coordinate,index;
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d {
self = [super init];
title = ttl;
coordinate = c2d;
return self;
}
- (id)initWithTitle:(NSString *)ttl Coordinate:(CLLocationCoordinate2D)c2d andIndex:(int)intIndex
{
self = [super init];
title = ttl;
coordinate = c2d;
index = intIndex;
return self;
}
+(MKCoordinateRegion) regionForAnnotations:(NSArray*) annotations
{
NSAssert(annotations!=nil, #"annotations was nil");
NSAssert([annotations count]!=0, #"annotations was empty");
double minLat=360.0f, maxLat=-360.0f;
double minLon=360.0f, maxLon=-360.0f;
for (id<MKAnnotation> vu in annotations) {
if ( vu.coordinate.latitude < minLat ) minLat = vu.coordinate.latitude;
if ( vu.coordinate.latitude > maxLat ) maxLat = vu.coordinate.latitude;
if ( vu.coordinate.longitude < minLon ) minLon = vu.coordinate.longitude;
if ( vu.coordinate.longitude > maxLon ) maxLon = vu.coordinate.longitude;
}
CLLocationCoordinate2D center = CLLocationCoordinate2DMake((minLat+maxLat)/2.0, (minLon+maxLon)/2.0);
MKCoordinateSpan span = MKCoordinateSpanMake(maxLat-minLat, maxLon-minLon);
MKCoordinateRegion region = MKCoordinateRegionMake (center, span);
return region;
}
#end
If anyone is looking for a Swift 3 solution, I've written this quick extension that works very well for me :)
public extension MKMapView {
public static func visibleRect(for coords: [CLLocationCoordinate2D]) -> MKMapRect {
return coords.reduce(MKMapRectNull) { outRect, coord in
let point = MKMapPointForCoordinate(coord)
let rect = MKMapRectMake(point.x, point.y, 0.1, 0.1)
let union = MKMapRectUnion(rect, outRect)
return union
}
}
public func fitCoordinates(_ coords: [CLLocationCoordinate2D],
animated: Bool = true,
insets: UIEdgeInsets = UIEdgeInsets(top: 25, left: 25, bottom: 25, right: 25)) {
let rect = MKMapView.visibleRect(for: coords)
self.setVisibleMapRect(rect, edgePadding: insets, animated: animated)
}
}
Which allows to quickly do:
map.fitCoordinates([userLocation, location1, location2])
Hope this helps anyone!
Gist here: https://gist.github.com/freak4pc/1748322a1b8bba23b6de3f47cb00c3ea
try like this may be it helps you,
MKCoordinateSpan span = MKCoordinateSpanMake(52.0f,80.0001f);
CLLocationCoordinate2D coordinate = {8.9999, 18.2812};
MKCoordinateRegion region = {coordinate, span};
MKCoordinateRegion regionThatFits = [self.mapView regionThatFits:region];
NSLog(#"Fit Region %f %f", regionThatFits.center.latitude, regionThatFits.center.longitude);
[self.mapView setRegion:regionThatFits animated:YES];
replace your coordinates and all the values respectively.
For Show All Annotation use this method, i Used this Method in same issue as your. it worked for me.
-(void)zoomToFitMapAnnotations:(MKMapView*)mapView
{
if([mapView.annotations count] == 0)
return;
CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;
CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;
for(MYAnnotation* annotation in mapView.annotations)// here MYAnnotation is custom annotation call.
{
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);
}
NSLog(#"A%f, B%f, C%f, D%f,", topLeftCoord.latitude, topLeftCoord.longitude, bottomRightCoord.latitude, bottomRightCoord.longitude);
MKCoordinateRegion region;
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1;
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1;
region = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];
}
call this method after set all Place(Annotation) on Map View, such like..
[self zoomToFitMapAnnotations:self.mapView];

Forward geocoding and zoom to fit multiple locations

The following is the code I have used to load mapview.
- (void)getLatLongCoordinates:(NSString*) addressStr firstNameTitle:(NSString*) firstNamesTitle lastNameTitle:(NSString*) lastNamesTitle {
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
[geocoder geocodeAddressString:addressStr completionHandler:^(NSArray *placemarks, NSError *error) {
self.placemarksArray = placemarks;
CLPlacemark *placeInfo = [placemarks objectAtIndex:0];
mapCenter.latitude = placeInfo.location.coordinate.latitude;
mapCenter.longitude = placeInfo.location.coordinate.latitude;
[annotationPoint setCoordinate:mapCenter];
[annotationPointsArray addObject:annotationPoint];
addAnnotation = [[[MyAddressAnnotation alloc] initWithCoordinate:mapCenter title:firstNamesTitle SubTitle:lastNamesTitle ]autorelease];
addAnnotation.firstNameTitle = firstNamesTitle;
addAnnotation.lastNameTitle = lastNamesTitle;
[mapView addAnnotation:addAnnotation];
}];
}
I need to call the the method [self zoomToFitMapAnnotations:mapView withArray:annotationPointsArray];
to fit all the contacts in the mapview at the maximum zoom level. Till the previous implementations I have been successfully using the following code. But since I am using blocks now, I am a bit confused about when to call this method. I need to obtain all the location coordinates before I call this method and pass the array to it.
-(void)zoomToFitMapAnnotations:(MKMapView*)mapViews withArray:(NSArray*)anAnnotationArray
{
if([mapViews.annotations count] == 0) return;
CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;
CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;
for(MKPointAnnotation* annotation in anAnnotationArray)
{
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;
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1;
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1;
region = [mapViews regionThatFits:region];
[mapView setRegion:region animated:NO];
}
Where is the right place in the above piece of code to call the above method or do I need to find some other way to do this.
Edit: I avoided using the mapCenter and accessed the co-ordinates directly from placeInfo in the getLatLongCoordinates method and I am getting it right and the issue is solved. But I am still bemused, why the first way didn't work.
I use this method in my of my case
generally i call this method after i add annotation in my mapview..so it best way is to call this method after add annotationView to mapVIEW.
use this code zoomToFitMapAnnotations
-(void)zoomToFitMapAnnotations:(MKMapView*)mapView_
{
if([mapView_.annotations count] == 0)
return;
CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;
CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;
for(CKMapAnnotationView* annotation in 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;
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1;
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1;
region = [mapView_ regionThatFits:region];
[mapView_ setRegion:[self validRegion:region] animated:YES];
}
EDIT: to call this function after load annotations
[self zoomToFitMapAnnotations:mapView];

zoomLevel and mapCenter in MKMapView

I'm creating a Map app in iPhone using MKMapView.
I did successfully find my current location and zoom on that point like this:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.mapView.delegate = self;
self.locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.mapView setShowsUserLocation:YES];
}
-(void) mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
MKAnnotationView *annotationView = [views objectAtIndex:0];
id<MKAnnotation> mp = [annotationView annotation];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 1550, 1550);
[self.mapView setRegion:region animated:YES];
}
The problem that I want to solve is:
I have a location on the map (this code in viewDidLoad):
CLLocationCoordinate2D location = [self getLocationFromAddressString:#"San Francisco, CA, United States"];
// location.latitude = 37.78608;
// location.longitude = -122.407398;
MapViewAnnotation *mapAnnotation = [[MapViewAnnotation alloc] initWithTitle:#"Store location" coordinate:location];
[self.mapView addAnnotation:mapAnnotation];
and this is the mapViewAnnotation:
#implementation MapViewAnnotation
#synthesize title = _title;
#synthesize coordinate = _coordinate;
- (id) initWithTitle:(NSString *) t coordinate:(CLLocationCoordinate2D) c {
self = [super init];
if(self) {
_title = t;
_coordinate = c;
}
return self;
}
#end
I want to have a proper zoomLevel and mapCenter for this location and my current location.
I could do that successfully in Android using MapController.zoomToSpan(). How can I fix it in iPhone Map?
create array of location and after that center mapview with all AnnotationPins
-(void) RoutscenterMap
{
MKCoordinateRegion region;
CLLocationDegrees maxLat = -90;
CLLocationDegrees maxLon = -180;
CLLocationDegrees minLat = 90;
CLLocationDegrees minLon = 180;
for(int idx = 0; idx < arrLocation.count; idx++)// here use your array or points
{
CLLocation* currentLocation = [arrLocation objectAtIndex:idx];
if(currentLocation.coordinate.latitude > maxLat)
maxLat = currentLocation.coordinate.latitude;
if(currentLocation.coordinate.latitude < minLat)
minLat = currentLocation.coordinate.latitude;
if(currentLocation.coordinate.longitude > maxLon)
maxLon = currentLocation.coordinate.longitude;
if(currentLocation.coordinate.longitude < minLon)
minLon = currentLocation.coordinate.longitude;
}
region.center.latitude = (maxLat + minLat) / 2;
region.center.longitude = (maxLon + minLon) / 2;
region.span.latitudeDelta = (maxLat - minLat) * 2;
region.span.longitudeDelta = (maxLon - minLon) * 2;
[mapView setRegion:region animated:YES];
}
also you can add location in array like bellow...
CLLocation *temploc=[[CLLocation alloc]initWithLatitude:latitude longitude:longtitude];
[arrLocation addObject:temploc];
after that use this array to center the map
i hope this help you...
:)
This is the way that I could implement it:
CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;
CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;
for(id<MKAnnotation> annotation in 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;
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1;
// Add a little extra space on the sides
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1;
region = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];

How to zoom a mapview to annotation point by fetching latitude and longitude from dictionary on iphone

i have an application in which i am dropping multiple annotations on the map. But now i want to span my map when these annotations are dropped.This is my code for adding annotation on map.
- (void)viewDidLoad {
[super viewDidLoad];
mapView.mapType = MKMapTypeStandard;
mapView.showsUserLocation = YES;
NSMutableArray *annotations = [[NSMutableArray alloc]init];
[annotations addObjectsFromArray:appDelegate.maparray];
NSLog(#"%#",annotations);
if ([annotations count])
{
for (int i =0; i < [annotations count]; i++)
{
dict = [annotations objectAtIndex:i];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = [[dict objectForKey:#"Latitude"] floatValue];
region.center.longitude = [[dict objectForKey:#"Longitude"] floatValue];
region.span.longitudeDelta = 0.6f;
region.span.latitudeDelta = 0.6f;
Title = [dict objectForKey:#"Title"];
description = [dict objectForKey:#"DescriptionMobile"];
NSLog(#"%#",description);
NSURL *url = [NSURL URLWithString:[dict objectForKey:#"Image"]];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
image = [[UIImage alloc] initWithData:data];
MyAnnotation *ann = [[MyAnnotation alloc] init];
ann.title = Title;
ann.subtitle = description;
[mapView addAnnotation:ann];
}
}
}
How to span my map to my annotation point so that all my annoations are visible and my map also zooms.Thanks
Try this code ,it worked for me
- (void)zoomToFitMapAnnotations:(MKMapView *)mapView {
if ([mapView.annotations count] == 0) return;
CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;
CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;
for(Annotation *annotation in 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;
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1;
// Add a little extra space on the sides
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1;
// Add a little extra space on the sides
region = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];
}
I think you miss this line:
ann.coordinate = region.center;

How can i create zoom effect base on distance from a point

I am making an augmented reality project and want to create a zoom effect in an image: I want that image to scale up when the distance is reduced.
I have distance already.
I have the image.
I have the maximum distance under which the zoom kicks in.
Following code is use for the zooming par as the distance of the pin.
-(void) centerMap
{
MKCoordinateRegion region;
CLLocationDegrees maxLat = -90;
CLLocationDegrees maxLon = -180;
CLLocationDegrees minLat = 120;
CLLocationDegrees minLon = 150;
NSArray *temp=self.AS;
// NSArray *temp=[NSArray arrayWithArray:[NSArray arrayWithArray:[reports objectAtIndex:0]]];
for (int i=0; i<[temp count];i++) {
Place* home = [[[Place alloc] init] autorelease];
home.latitude = [[[temp objectAtIndex:i] valueForKey:#"latitude"]floatValue];
home.longitude =[[[temp objectAtIndex:i] valueForKey:#"longitude"]floatValue];
PlaceMark* from = [[[PlaceMark alloc] initWithPlace:home] autorelease];
CLLocation* currentLocation = (CLLocation*)from ;
if(currentLocation.coordinate.latitude > maxLat)
maxLat = currentLocation.coordinate.latitude;
if(currentLocation.coordinate.latitude < minLat)
minLat = currentLocation.coordinate.latitude;
if(currentLocation.coordinate.longitude > maxLon)
maxLon = currentLocation.coordinate.longitude;
if(currentLocation.coordinate.longitude < minLon)
minLon = currentLocation.coordinate.longitude;
region.center.latitude = (maxLat + minLat) / 2;
region.center.longitude = (maxLon + minLon) / 2;
region.span.latitudeDelta = maxLat - minLat;
region.span.longitudeDelta = maxLon - minLon;
}
[mapView setRegion:region animated:YES];
}
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
if (annotation == mapView.userLocation)
return nil;
MKPinAnnotationView *pin = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier: #"asdf"];
if (pin == nil)
pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier: #"asdf"] autorelease];
else
pin.annotation = annotation;
pin.userInteractionEnabled = YES;
UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[disclosureButton setFrame:CGRectMake(0, 0, 30, 30)];
pin.rightCalloutAccessoryView = disclosureButton;
pin.pinColor = MKPinAnnotationColorRed;
//pin.animatesDrop = YES;
[pin setEnabled:YES];
[pin setCanShowCallout:YES];
return pin;
}
This code may help to create new application,