-1

I have been using core data to save the user's location for various times in a table view. Now that I can actually get the location details (coordinates mainly) at various times, I would like to plot the coordinates that are available at each time on a MapView (No overlays, just a single point to just point the location at that time). I have the coordinates (one latitude and one longitude value) available, but I would like to know how to plot them on the MapView.

Just to put my question simply, How one can plot the coordinates on a mapview ?

I just browsed through SO and I was not able to find the exact solution that would solve my problem ! Any help will be much appreciated.

Thanks for your time !

Regards,

Raj.

Raj0689
  • 73
  • 1
  • 1
  • 9
  • Have you looked at the sample code from Apple on their website? – Craig May 19 '13 at 01:45
  • Hey Craig ! I actually did look at the core data tutorial for iOS on apple's website. It actually says how to save location data and delete them. But, I am still searching for a close example that deals with core location and Mapkit. That might do the trick.. Thanks :) – Raj0689 May 19 '13 at 10:00
  • 1
    I suspect the bit you're missing is that the thing that is shown on a map is called an "annotation" so if you search for "how to show an annotation on a MKMapView" you'll get loads of results. If they aren't clear enough you should read the section called "Annotating the map" from Apple's documentation (http://developer.apple.com/library/ios/#DOCUMENTATION/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html) – Craig May 19 '13 at 10:31
  • Perfect ! I actually thought there is something else other than MKAnnotation to give the location. But thanks a lot @Craig :) Now I can proceed in this !! That helped ! – Raj0689 May 19 '13 at 10:42

1 Answers1

0

With the help of MKAnnotation, this problem can be solved. Thanks to @Craig and Vishal Kurup !

Create an annotation class as a subclass of NSObject.

in Annotation.h:

@interface Annotation : NSObject <MKAnnotation>

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, assign) NSString *title;
@property (nonatomic, assign) NSString *subtitle;

@end

It should conform to MKAnnotation and the three properties are the ones that are required when conforming to MKAnnotation class. Declare those and synthesize them in the .m file.

In the MapHistoryViewController implementation file, we need to add few lines of codes to view the annotation at the required coordinates.

MapHistoryViewController.m:

@interface MapHistoryViewController (){
    CLLocationCoordinate2D annotationCoord;
    Annotation *annotation;
}

@end

@implementation MapHistoryViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

// To define coordinate for the annotation 
    annotationCoord.latitude = mapHistoryLocation.coordinate.latitude;
    annotationCoord.longitude = mapHistoryLocation.coordinate.longitude;

    annotation = [Annotation alloc];
    annotation.coordinate = annotationCoord;
    annotation.title = streetAnnotation;

    // to display the annotation
    [self.mapHistoryView addAnnotation:annotation];
// where mapHistoryView is my MKMapView object 
}

//You can set the region too, if you want the map to be focused on the coordinates you  have provided
//Hope this will help the people with the same question :) 
Raj0689
  • 73
  • 1
  • 1
  • 9