Progressing apace, trying to learn in and outs of MapKit and annotations.
I've made some good progress, but I'm having problems when I attempt to add location services to the mix.
If I return nil to the
- (MKAnnotationView *)mapView:(MKMapView *)mapView
viewForAnnotation:(id < MKAnnotation >)annotation
method, I get a result VERY close to what I'm looking for, I have my annotation pins, I have my user's location showing in a standard way.
But if I return myPins (with all the parameters set as below), I get the correct behavior on all the pins but the properties are also set for the user's location (it's a red pin, it drops into place every time the view moves or a pin is selected). Annoying.
How does one set the properties for the annotations separately from the location? Is there a way to return multiple MKAnnotationViews? I know I'm missing some basic concept, but would appreciate any help you can give me. Thanks in advance.
Here's my main viewController, in total:
//
// ViewController.m
// mapFun
//
#import "ViewController.h"
#implementation ViewController
#synthesize segmentedControl;
#synthesize mapView,location, myLocations, myAnnotation, region;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView.delegate = self;
//location manager stuff
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
if([CLLocationManager locationServicesEnabled])
{
[self.mapView setShowsUserLocation:YES];
[locationManager startUpdatingHeading];
}
[self loadUpLocations];
//set first zoom center and span, to zoom from to initial zoom
region.center=CLLocationCoordinate2DMake(
33.75070416856952,
-84.37034368515015);
MKCoordinateSpan span;
span.latitudeDelta=.3;
span.longitudeDelta=.3;
region.span=span;
[mapView setRegion:region animated:YES];
}
- (void)viewDidUnload
{
[self setMapView:nil];
[self setSegmentedControl:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
//zoom to initial zoom
[self setInitialZoom];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
//Load up a buncha locations
-(void)loadUpLocations{
myLocations = [LoadObjectsFromFile loadFromFile:#"locations2" ofType:#"plist"];
//loop through and make annotations
for (NSString *loc in myLocations) {
NSDictionary *value =[myLocations objectForKey:loc];
//create instance of MapAnnotations
CLLocationCoordinate2D temp =
CLLocationCoordinate2DMake(
[[value objectForKey:#"latitude"] doubleValue],
[[value objectForKey:#"longitude"] doubleValue]);
myAnnotation = [[MapAnnotations alloc]initWithLocation:temp];
myAnnotation.title = [value objectForKey:#"title"];
myAnnotation.subtitle = [value objectForKey:#"subtitle"];
myAnnotation.info =[value objectForKey:#"info"];
[self.mapView addAnnotation:myAnnotation];
}
}
- (void)setInitialZoom{
// MKCoordinateRegion region;
region.center = CLLocationCoordinate2DMake(33.77, -84.37);
//set level of zoom
MKCoordinateSpan span;
span.latitudeDelta=.04;
span.longitudeDelta=.04;
region.span=span;
[mapView setRegion:region animated:YES];
}
// used when selecting annotations
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
MKPointAnnotation *selectedAnnotation = view.annotation;
NSLog(#"The annotation selected is %#.",selectedAnnotation.title);
//set level of zoom
CLLocationCoordinate2D newCenter =
CLLocationCoordinate2DMake(
selectedAnnotation.coordinate.latitude,
selectedAnnotation.coordinate.longitude);
MKCoordinateSpan span;
span.latitudeDelta=.02;
span.longitudeDelta=.02;
MKCoordinateRegion zoomRegion = MKCoordinateRegionMake(newCenter, span);
[ self.mapView setRegion:zoomRegion animated:YES];
}
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
// [self zoomOutToInitial:nil];
}
- (IBAction)zoomOutToInitial:(id)sender {
[self.mapView setRegion:region animated:YES];
}
- (IBAction)segChanged:(id)sender {
if (self.segmentedControl.selectedSegmentIndex==0) {
[self.mapView setMapType:MKMapTypeStandard];
}
if (self.segmentedControl.selectedSegmentIndex==1) {
[self.mapView setMapType:MKMapTypeSatellite];
}
if (self.segmentedControl.selectedSegmentIndex==2) {
[self.mapView setMapType:MKMapTypeHybrid];
}
}
// This gets called every time an annotation is in the map view
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation
{
NSLog(#"Map view can see annotation %#.",annotation.title);
MKPinAnnotationView *myPins;
if (!myPins) {
myPins= [[MKPinAnnotationView alloc]initWithAnnotation:self.myAnnotation
reuseIdentifier:#"myPins"];
}
myPins.animatesDrop = YES;
myPins.canShowCallout = YES;
myPins.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return nil;
//return myPins;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[manager stopUpdatingHeading];
}
#end
Answering my own question. Thanks anyway.
I finally found the answer in a useful document called "Location Awareness Programming Guide," from Apple.
Here's the corrected MKAnnotationView method:
// This gets called every time an annotation is in the map view
- (MKAnnotationView *)mapView:(MKMapView *)theView
viewForAnnotation:(id < MKAnnotation >)annotation
{
NSLog(#"Map view can see annotation %#.",annotation.title);
//if the annotation is a user location
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
//uses my custom class
if ([annotation isKindOfClass:[MapAnnotations class]]) {
// try for reuse of pins
MKPinAnnotationView *myPins = (MKPinAnnotationView *)
[theView dequeueReusableAnnotationViewWithIdentifier:#"myPins"];
if (!myPins) {
myPins= [[MKPinAnnotationView alloc]initWithAnnotation:annotation
reuseIdentifier:#"myPins"];
myPins.animatesDrop = YES;
myPins.canShowCallout = YES;
myPins.rightCalloutAccessoryView =
[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}else
myPins.annotation = annotation;
return myPins;
}
return nil;
}
The basic concept is to return if and only if the view asked is actually the userlocation view.
You simply test if the Annotation forwarded to you is of userlocation class, like this:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// do your usual stuff
}
Related
I am trying to use some delegate methods in MKMapViewDelegate. Specifically, I want to give the pop out an accessory arrow so that when user touches it, it launches native map application (for full blown mapping functions.)
I think I understand correctly that once the VC is set up as a delegate of itself then the protocol methods get called automatically. In that case do delegate methods get called automatically or do you have to do something else?
Here is method to launch map.
-(void) geoCodeAndMapIt {
NSString* location = #"156 University Ave, Palo Alto, CA 94301";
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:location
completionHandler:^(NSArray* placemarks, NSError* error){
if (placemarks && placemarks.count > 0) {
CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc]
initWithPlacemark:topResult];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(placemark.coordinate, 5000, 5000);//5000 is meters
region.span.longitudeDelta /= 8.0;
region.span.latitudeDelta /= 8.0;
[self.mapView setRegion:region animated:YES];
[self.mapView addAnnotation:placemark];
}
Here is method that should add accessory arrow to callout but is not firing:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"loc"];
annotationView.canShowCallout = YES;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return annotationView;
}
How do I get this method to fire so I get accessory arrow? Thanks for any suggestions.
Why don't you use this method when you click the button in you annotation view this method is fired and you can perform your custom action there.
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
//Do whatever you want to do here.
PropertyLocation *location = (PropertyLocation*)view.annotation;
for(PropertyDTO *property in self.items)
{
if(property.location == location)
{
PropertyDetailVC *detailVC = [PropertyDetailVC propertyDetailVCWithNodeID:property.nodeID];
[self.navigationController pushViewController:detailVC animated:YES];
}
}
}
I am trying to change colour of annotation pin on MKMapView by overriding this below:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
// If it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// Handle any custom annotations.
if ([annotation isKindOfClass:[MKPointAnnotation class]])
{
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:#"CustomPinAnnotationView"];
if (!pinView)
{
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"CustomPinAnnotationView"];
pinView.canShowCallout = YES;
pinView.pinColor = MKPinAnnotationColorGreen;
} else {
pinView.annotation = annotation;
}
return pinView;
}
return nil;
}
Here below are relevant methods:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
}
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
// display map ????
PFGeoPoint *geoPoint = self.detailItem[#"location"];
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(geoPoint.latitude,geoPoint.longitude);
MKPointAnnotation *annotation =[[MKPointAnnotation alloc] init];
annotation.coordinate = coordinate;
// remove previous annotation pin
if([self.mapView.annotations count] == 1) {
[self.mapView removeAnnotation:[self.mapView.annotations objectAtIndex:0]];
}
[self.mapView addAnnotation:annotation];
annotation.title = self.detailItem[#"text"];
MKCoordinateSpan span = {.latitudeDelta = 0.05, .longitudeDelta = 0.05};
MKCoordinateRegion region = {coordinate, span};
[self.mapView setRegion:region];
[self.mapView setCenterCoordinate:self.mapView.region.center animated:NO];
}
}
My problem is pin annotation colour is still shown Red as default not Green as I want. What am I missing here?
Credit to #Anna, add the line below to - (void)configureView
self.mapView.delegate = self;
I'm using the Apple maps. At the moment I have to click on one of my Annotation Pins and the Annotation View is opening, than I can click on this view and something happens. But i want to click on my Annotation Pins and the Map should be zoom in WITHOUT opening the Annotation View first. I tried this, but it doesn't work:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if(someValues > 1){
//If someValues are bigger than one then only zoom in without returning the annotation View
MKCoordinateRegion region = mapView.region;
MKCoordinateSpan span;
span.latitudeDelta = region.span.latitudeDelta/5;
span.longitudeDelta = region.span.longitudeDelta/5;
region.span = span;
region.center = anntotation.coordinate;
[mapView setRegion:region animated:TRUE];
//return 0 returns a default view i know....whats correct?
return 0;
} else {
MKPinAnnotationView *view = nil;
if (annotation != mapView.userLocation)
{
view = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:#"identifier"];
if (nil == view) {
view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"identifier"];
}
[view setPinColor:MKPinAnnotationColorRed];
view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[view setCanShowCallout:YES];
[view setAnimatesDrop:NO];
}
return view;
}
}
Is there any delegate Method i´m missing?
please see the didSelectAnnotationView delegate method to your mapview.
example :
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
if([[view annotation] isKindOfClass:[myMarker class]]) return;
}
And here :
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if([[view annotation] isKindOfClass:[myMarker class]]) return nil;
}
I am trying to display user's current location on the map but for some reason, does not takes my default coordinates.
I have a MKMapView class that called inside a UIViewController.
MapView.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#interface MapView : MKMapView <MKMapViewDelegate, CLLocationManagerDelegate>{
CLLocationManager *locationManager;
}
#end
MapView.m
#import "MapView.h"
#interface MapView ()
#end
#implementation MapView
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
self.delegate = self;
[self addAnnotation:[self userLocation]];
[locationManager startUpdatingLocation];
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"didFailWithError: %#", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:#"Error" message:#"Failed to Get Your Location" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
NSLog(#"didUpdateUserLocation");
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 250, 250);
[self setRegion:[self regionThatFits:region] animated:YES];
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = userLocation.coordinate;
point.title = #"Where am I?";
point.subtitle = #"I'm here!!!";
[self addAnnotation:point];
}
- (MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
NSLog(#"viewForAnnotation");
static NSString *identifier = #"MyAnnotation";
if ([annotation isKindOfClass:[MKUserLocation class]]) {
NSLog(#"Is the user %f, %f", [annotation coordinate].latitude, [annotation coordinate].longitude);
return nil;
}
return nil;
}
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views{
NSLog(#"didAddAnnotationViews");
for (MKAnnotationView *view in views){
if ([[view annotation] isKindOfClass:[MKUserLocation class]]){
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([[view annotation] coordinate] , 250, 250);
[mapView setRegion:region animated:YES];
}
}
}
#end
I am getting the output below once access the map.
viewForAnnotation
Is the user 0.000000, 0.000000
didAddAnnotationViews
I can't understand why my didUpdateUserLocation: never get called even if re-submit custom coordinates.
What I am missing here?
You're using a CLLocationManager and setting it's delegate, but your delegate methods are all methods of MKMapViewDelegate.
You can either set the mapview's delegate, or use the locationManager:didUpdateLocations: CLLocationManagerDelegate method.
If it is important to you that the user have the option to turn his location on and off on the map then Nevan King is correct. However, if you are OK with continually showing the user location then no scripting is necessary. Simply click on your mapView in the Storyboard and on the right (in attributes, I think) check the "shows user location" box.
Im just getting into MapViews on iOS, and want to show a car continuously moving as a blue dot. Would this be considered a map annotation?
Yes. As an example check out the in Simulator Debug > Location > City Bike Ride . It does a slow loop round San Francisco(?)
To listen to updates implement in your Mapview delegate
- (void)mapView:(MKMapView *)amapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
NSLog(#"im here! - %f,%f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
}
and to adjust the annotation implement
- (MKAnnotationView *) mapView:(MKMapView *)amapView viewForAnnotation:(id <MKAnnotation>) annotation{
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
NSLog(#"annotation = %#",((NSObject *)annotation));
MKAnnotationView *annView;
annView = [amapView dequeueReusableAnnotationViewWithIdentifier:#"currentloc"];
if(!annView)
{
annView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"currentloc"] autorelease];
((MKPinAnnotationView *)annView).pinColor = MKPinAnnotationColorGreen;
((MKPinAnnotationView *)annView).animatesDrop=TRUE;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
annView.draggable = YES;
}
return annView;
}
The snip I have put in just goes with the default blue dot with accuracy circle by returning nil for MKUserLocation but your implementation may be different.