Hi all,
In my App I would like to implement a compass that points to a location from my current location.
I am currently showing my location and another location, I am also getting a magnetic heading and a true heading, so it's nearly all set up and ready to go...
this is my code that gets my location, speed, course, headings:
and this is my code that sets a location on a map
I just need to be able to place an arrow(image) in my ViewController that points to that location from my current location
Can anyone help me here please or point me to a relevant tutorial?
In my App I would like to implement a compass that points to a location from my current location.
I am currently showing my location and another location, I am also getting a magnetic heading and a true heading, so it's nearly all set up and ready to go...
this is my code that gets my location, speed, course, headings:
Code:
- (void) locationManager:(CLLocationManager *)_manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSLog(@"Latitude : %f, Longitude : %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
NSLog(@"Count: %i", _manager.monitoredRegions.count);
CLRegion *region;// = (CLRegion *) _manager.monitoredRegions.anyObject;
int i = 1;
for (region in _manager.monitoredRegions) {
NSLog(@"%i. Lat : %f, Long : %f, Radius: %f", i, region.center.latitude, region.center.longitude, region.radius);
i++;
}
myLat = newLocation.coordinate.latitude;
myLon = newLocation.coordinate.longitude;
myUrl = [NSString stringWithFormat:@"http://maps.google.com/?cbll=%f,%f&cbp=12,20.09,,0,5&layer=c",myLat,myLon];
location1 = [[CLLocation alloc] initWithLatitude: newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
location2 = [[CLLocation alloc] initWithLatitude: placemark.coordinate.latitude longitude:placemark.coordinate.longitude];
distInMeter = [location1 distanceFromLocation:location2]; // which returns in meters
distInMile = 0.000621371192 * distInMeter;
distance.text = [NSString stringWithFormat:@"%.2f (miles)", distInMile];
course = manager.location.course;
thespeed = manager.location.speed;
NSString *courseString = @"";
NSString *speedString = @"";
if(course >=0) courseString = [NSString stringWithFormat:@"course %.2f", course];
if(thespeed >=0) speedString = [NSString stringWithFormat:@"speed %.2f", thespeed];
mySpeed.text = speedString;
if(![@"1" isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:@"tooFast"]]){
[[NSUserDefaults standardUserDefaults] setValue:@"1" forKey:@"tooFast"];
[[NSUserDefaults standardUserDefaults] synchronize];
if(thespeed >= 0.30){
UIAlertView *tooFast = [[UIAlertView alloc]initWithTitle:@"STOP" message:@"You seem to be travelling quite fast, I hope you are not driving" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
[tooFast show];
}
}
}
-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
NSLog(@"New magnetic heading: %f", locationManager.heading.magneticHeading);
NSLog(@"New true heading: %f", locationManager.heading.trueHeading);
}
Code:
- (void)viewDidLoad
{
self.locationManager = [[CLLocationManager alloc]init];
self.accuracy = 10.0;
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
[self.locationManager setDelegate:self];
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[super viewDidLoad];
_mapView.zoomEnabled = YES;
_mapView.scrollEnabled = YES;
_mapView.showsUserLocation = YES;
// Do any additional setup after loading the view.
self.title = pubNameLabel.text = currentPub.rsPubName;
course = thespeed = -1;//invalid values
[self startStandardUpdates];
if ([CLLocationManager regionMonitoringAvailable]) {
[self startRegionMonitoring];
NSLog(@"Region monitoring available");
}
if ([CLLocationManager headingAvailable]){
[locationManager startUpdatingHeading];
NSLog(@"Heading Available **");
}
//Load up the UI
[self setLabels];
AppDelegate *appdelegate = [[UIApplication sharedApplication]delegate];
context = [appdelegate managedObjectContext];
//create a mapview
// We're working
setPath.hidden = YES;
showDirections.hidden = YES;
addFav.hidden = YES;
showFavs.hidden = YES;
streetView.hidden = YES;
callPub.hidden = YES;
[self.activityIndicator startAnimating];
// Make a directions request
MKDirectionsRequest *directionsRequest = [MKDirectionsRequest new];
// Start at our current location
MKMapItem *source = [MKMapItem mapItemForCurrentLocation];
[directionsRequest setSource:source];
[directionsRequest setTransportType:1 << 1];
//create a region
location = currentPub.rsPostCode;
geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:location
completionHandler:^(NSArray* placemarks, NSError* error){
if (placemarks && placemarks.count > 0) {
topResult = [placemarks objectAtIndex:0];
placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
destination = placemark.location;
//self.pubLatlong.text = [NSString stringWithFormat:@"%f, %f", coordinate.latitude, coordinate.longitude];
// Make the destination
CLLocationCoordinate2D coordinate = destination.coordinate;
destinationPlacemark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil];
thisdestination = [[MKMapItem alloc] initWithPlacemark:destinationPlacemark];
[directionsRequest setDestination:thisdestination];
MKDirections *directions = [[MKDirections alloc] initWithRequest:directionsRequest];
[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
// We're done
self.activityIndicator.hidden = YES;
[self.activityIndicator stopAnimating];
setPath.hidden = NO;
showDirections.hidden = NO;
addFav.hidden = NO;
showFavs.hidden = NO;
streetView.hidden = NO;
callPub.hidden = NO;
// Now handle the result
if (error) {
NSLog(@"There was an error getting your directions");
return;
}
MKCoordinateRegion region = _mapView.region;
region.center = placemark.region.center;
region.span.longitudeDelta /= 2000.0;
region.span.latitudeDelta /= 2000.0;
// So there wasn't an error - let's plot those routes
_currentRoute = [response.routes firstObject];
[self plotRouteOnMap:_currentRoute];
[_mapView setRegion:region animated:YES];
[_mapView addAnnotation:placemark];
centre1 = CLLocationCoordinate2DMake(placemark.location.coordinate.latitude, placemark.location.coordinate.longitude);
regionCourante = [[CLRegion alloc] initCircularRegionWithCenter:centre1 radius:2.0 identifier:@"Bingo"];
[manager startMonitoringForRegion:regionCourante];
float theLat = placemark.location.coordinate.latitude;
float theLong = placemark.location.coordinate.longitude;
theUrl = [NSString stringWithFormat:@"http://maps.google.com/?cbll=%f,%f&cbp=12,20.09,,0,5&layer=c",theLat,theLong];
}];
}
}
];
}
Can anyone help me here please or point me to a relevant tutorial?