I have created an app that simply shows the user’s current position on a google map using the MapKit.
However I would now like to go a bit further and add a bit more functionality to the app by allowing the user to plot points on the map themselves.
I somehow believe using CoreLocation will accomplish this by receiving and saving coordinates of where the user selects on the map.
Am I right in saying this? and any ideas on how I would implement this? Links or tutorials would be helpful too, and also any personal experience/ideas in the matter would be great.
I have created an app that implements coreLocation before this app, compiled/ran perfectly.. just didnt update user location and latitude/longitude. the code is shown below for that app:
This is only in the AppDelegate.m which is what does all the work basically.
#import "AppDelegate.h"
#import "ViewController.h"
@implementation AppDelegate
@synthesize window;
@synthesize viewController;
@synthesize locationManager;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
self.locationManager = [[CLLocationManager alloc]init];
if ([CLLocationManager locationServicesEnabled]) {
self.locationManager.delegate = self;
self.locationManager.distanceFilter = 100;
[self.locationManager startUpdatingLocation];
}
}
- (void)applicationWillTerminate:(UIApplication *)application
{
}
#pragma mark CLLocationManagerDelegate Methods
-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
MKCoordinateSpan span;
span.latitudeDelta = 0.2;
span.longitudeDelta = 0.2;
MKCoordinateRegion region;
region.span = span;
region.center = newLocation.coordinate;
[viewController.mapView setRegion:region animated:YES];
viewController.mapView.showsUserLocation = YES;
viewController.latitude.text = [NSString stringWithFormat:@"%f", newLocation.coordinate.latitude];
viewController.latitude.text = [NSString stringWithFormat:@"%f", newLocation.coordinate.longitude];
}
@end
Are you a signed up iOS developer? Apple’s code samples include a few map apps, they should get you started.
Since you’re not having any luck while searching for tutorials online here are some terms that might help
MKAnnotation : What you’d use to mark a location on the map (a pin, a flag, a bit of text)
UIGestureRecognizer : Use one of these to find out when a user has touched the map, look for touch-and-hold events as a good sign they want to mark it because touch-and-drag would be for moving the map around
MKMapPoint : A place on a flat map of earth as used in iOS. Remember that these are different from CLLocationCoordinate2D’s longitude and latitude but you can easily convert between them.