How to add a "Directions to Here" function with MKmapview? - iphone

So I have set my app to search for the user's location upon pressing on a button.
-(IBAction)getLocation {
mapview.showsUserLocation = YES;}
Then I have set a Map Annotation as well to a certain location upon loading of the map view using this
- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view.
[mapview setMapType:MKMapTypeStandard];
[mapview setZoomEnabled:YES];
[mapview setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, {0.0, 0.0 } };
region.center.latitude = 123;
region.center.longitude = 123;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapview setRegion:region animated:YES];
NewClass *ann = [[NewClass alloc] init];
ann.title = #"Place to go to";
ann.subtitle = #"subtitle";
ann.coordinate = region.center;
[mapview addAnnotation:ann];
}
Is it possible to be able to put a "Directions to Here" button to my annotation using MapKit? Any help would be awesome! Thanks!

To add the callout accessory, you have to follow the instruction from Anna Karenina.
To draw the route itself, you have to first obtain the route, there are plenty of excellent APIs that will give you the route between two given points, even allowing you to set params like if the kind of routes you want to include. Google Directions API is pretty impressive on that regard. You should check it.
Then you have to draw the route itself with MKPolyline. You can check a toy app i put together a few months ago to show how to do this.

Related

MKMapViewprevent user from zooming out past a certain range

I have a lot of pin annotations on the MKMapView in my app, the iPhone gets very slow and unresponsive when a lot of them are in view on the map. I would like the user to be able to zoom, but not out past a certain level, such as 2km squared or something.
Here's what I've got:
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
[[BicycleLDNService sharedService] requestLocationForClient:self];
CLLocationCoordinate2D zoomLocation;
CLLocation *deviceLocation = [[BicycleLDNService sharedService] deviceLocation];
zoomLocation.latitude = deviceLocation.coordinate.latitude;
zoomLocation.longitude = deviceLocation.coordinate.longitude;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*kMetresPerKilometre, 0.5*kMetresPerKilometre);
MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:viewRegion];
[self.mapView setRegion:adjustedRegion animated:YES];
self.mapView.zoomEnabled = YES;
}
Is there some sort of property or delegate method I can employ? Couldn't find anything useful on google or here!
Thanks in advance!
The span defines how much of the map at the given point should be visible and is also how you set the zoom level.
You can access this by using
region.span.latitute=0.5;
region.span.longitude=0.6;
Check the zoom level of the map and then set the zoomEnabled Property NO.
mapView.zoomEnabled=NO;

Showing Specific Region Using MapKit

I want to know is it possible to show only specific region on map not the full world map using Map Kit.
Like if i want to show Asia map in my application then map kit hides remaining part of the map.
To handle the "map kit hides remaining part of the map" requirement, one thing you can do is create a black polygon overlay that covers the whole world with a cutout over Asia (or wherever you like).
For example, where you initialize the map (eg. in viewDidLoad):
CLLocationCoordinate2D asiaCoords[4]
= { {55,60}, {55,150}, {0,150}, {0,60} };
//change or add coordinates (and update count below) as needed
self.asiaOverlay = [MKPolygon polygonWithCoordinates:asiaCoords count:4];
CLLocationCoordinate2D worldCoords[4]
= { {90,-180}, {90,180}, {-90,180}, {-90,-180} };
MKPolygon *worldOverlay
= [MKPolygon polygonWithCoordinates:worldCoords
count:4
interiorPolygons:[NSArray arrayWithObject:asiaOverlay]];
//the array can have more than one "cutout" if needed
[myMapView addOverlay:worldOverlay];
and implement the viewForOverlay delegate method:
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKPolygon class]])
{
MKPolygonView *pv = [[[MKPolygonView alloc] initWithPolygon:overlay] autorelease];
pv.fillColor = [UIColor blackColor];
pv.alpha = 1.0;
return pv;
}
return nil;
}
This looks like this:
If you also want to restrict the user from scrolling beyond Asia or zooming too far out, then you'll need to do that manually as well. One possible way is described in Restrict MKMapView scrolling. Replace theOverlay in that answer with asiaOverlay.
You can specify the region as an MKCoordinateRegion and then tell an MKMapView instance to only show that region using the setRegion and regionThatFits message.
Alternatively you could use the visibleMapRect property instead of the region. This might better fit your needs.
In short read the MKMapView Class Reference document from Apple.
Lifting from some code I've done in the past that assumes a mapView and a given location called locationToShow I used an MKCoordinateRegion.
- (void) goToLocation {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.01;
span.longitudeDelta=0.01;
region.span=span;
region.center=locationToShow;
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
}

GPS location on map in application

I have one application that shows maps. I need to show current position of user and manage to do that with
[mapView setShowsLocation:YES];
But when I zoom In or zoom Out map it needs a lot of time to show me that blue pin again.
Is it normal or I need to put something else to keep that blue pin on screen all time???
Thanks.
[self setCurrentLocation:self._mapView.userLocation.location.coordinate withZoom:1.0];
self._mapView.showsUserLocation = YES;
- (void)setCurrentLocation:(CLLocationCoordinate2D)coord withZoom:(float)zoomLevel {
MKCoordinateRegion region = self._mapView.region;
region.span.latitudeDelta = self.defaultSpanLevel.latitudeDelta*zoomLevel;
region.span.longitudeDelta = self.defaultSpanLevel.longitudeDelta*zoomLevel;
region.center = coord;
[self._mapView setRegion:region animated:YES];
}
You can use this in view will appear Method
Then You can use mapview delegate methods....
region did change animated:YES { and set map's region here.... take
current user location as region centre.. }
This will solve your problem
}

A Reset button and a bookmark button in mapkit in xcode

I am currently working on an application involving mapkit. I would like to add a reset button on the view which resets the view to its default view when you open the program, or better still, the mapkit resets itself when you open and close the app.
The code i have used to set the initial region is as follows:
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 54.049929;
region.center.longitude = -4.54834;
region.span.longitudeDelta = 4.5;
region.span.latitudeDelta = 4.5;
[mapView setRegion:region animated:YES];
Any help would be greatly appreciated.
Store the Location of your map
In .h file
CLLocationCoordinate2D location;
When setting initial Region
location.latitude = 54.049929;
location.longitude = -4.54834;
In your Reset Button
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = location.latitude
region.center.longitude = location.longitude;
region.span.longitudeDelta = 4.5;
region.span.latitudeDelta = 4.5;
[mapView setRegion:region animated:YES];
So, are you trying to figure out how to actually add the button to the view and link it to a method in the code?
The code itself within the method would just be the same as you used for your initial setup, as indicated by BuildSucceded above ...
You should just add a button to the toolbar/navbar(if you have one), and link it to a "resetMap()" method.

MKMapView broken in 3.2.3 / OS4 - Can't set region

In the last version of Xcode, set region worked fine, now in 3.2.3 it doesn't snap to your specified region?
After View did load...
[mapView setMapType:MKMapTypeHybrid];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 41.902245099708516;
region.center.longitude = 12.457906007766724;
region.span.longitudeDelta = 0.04f;
region.span.latitudeDelta = 0.04f;
[mapView setRegion:region animated:YES];
[mapView setDelegate:self];
This is the code that worked fine, now it doesn't snap to the location denoted above, it just shows the world map.
Any help greatly appreciated.
I've done it lots in 3.2.3 and iOS4. I promise it works.
I like MKCoordinateRegionMakeWithDistance().
CLLocationCoordinate2d coord = { 41.902245099708516, 12.457906007766724 };
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 1000, 1000);
[mapView setRegion:[mapView regionThatFits:region] animated:YES];
That "1000, 1000" in the second and third arg are the latitudinal and longitudinal meters of the range component of the region. Passing the region through the mapview's -regionThatFits: method is just good practice.