1

This is my implementation file :

#import "mapViewController.h"
@interface mapViewController ()
@end
@implementation mapViewController
@synthesize mapView,source,dest,latdest,latsource,longdest,longsource;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        dest=@"delhi";
    // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    CLGeocoder *geocoder1 = [[CLGeocoder alloc] init];
    [geocoder1 geocodeAddressString:source
                 completionHandler:^(NSArray* placemarks, NSError* error)
    {
        for (CLPlacemark* aPlacemark in placemarks)
        {
            CLLocationCoordinate2D coordinate;
            coordinate.latitude = aPlacemark.location.coordinate.latitude;
            latsource=&coordinate.latitude;
            coordinate.longitude = aPlacemark.location.coordinate.longitude;
            longsource=&coordinate.longitude;
            MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
            [annotation setCoordinate:(coordinate)];
            [annotation setTitle:source];
            annotation.subtitle = @"I'm here!!!";
            [self.mapView addAnnotation:annotation];
        }
    }];
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    MKCoordinateRegion region =MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
    MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
    point.coordinate = userLocation.coordinate;
    point.title = @"Where am I?";
    point.subtitle = @"I'm here!!!";    
    [self.mapView addAnnotation:point];
    [self.view addSubview:self.mapView];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

This is my header file :

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface mapViewController : UIViewController <MKMapViewDelegate>
@property (strong, nonatomic) IBOutlet MKMapView *mapView;
@property(strong,nonatomic) NSString *source,*dest;
@property(nonatomic) CLLocationDegrees *latsource,*longsource;
@property(nonatomic) CLLocationDegrees *latdest,*longdest;
@end

First i want to know why didUpdateUserLocation method is never called.I also want to know the code to add a destination whose coordinates are stored in latdest and longdest.Both of them will get their values from static variable "dest" which has the value "delhi" in it .My final aim is to trace a route on the map from source coordinates(latsource,longsource) to destination coordinates(latest,longdest). I am new to ios development so i might have done some noob mistakes.

Pradeep Mittal
  • 595
  • 1
  • 8
  • 19

1 Answers1

0

didUpdateUserLocation method won't be called if you not set mapView.delegate = self;

If you use a class, which has delegate methods, and you want to use them, everytime you should set its delegate = self

Don't use the same name as in delegates: mapView

in header:

@property (weak, nonatomic) IBOutlet MKMapView *myMapView;

be sure you connected the IBOutlet MKMapView *myMapView in IterfaceBuilder

in implementation file:

@synthesize myMapView;

in your - (void)viewDidLoad

myMapView.delegate = self;

you have to correct all self.mapView to myMapView

You made a really big mistake:

if you use IBOutlet you have to add on InterfaceBuilder , and connect it. or you can create everything from code:

@property (weak, nonatomic) MKMapView *myMapView;
myMapView = [[MKMapView alloc] initWithFrame:CGRectMake(0,0,320,480)];

then add the screen with

 [self.view addSubview:myMapView];
Magyar Miklós
  • 4,182
  • 2
  • 24
  • 42