Skip to content

Instantly share code, notes, and snippets.

@dlinsin
Created May 3, 2010
Embed
What would you like to do?
//
// RouteController.m
// synyx
//
// Created by David Linsin on 10/12/09.
// Copyright 2010 Synyx GmbH & Co. KG. All rights reserved.
//
#import "RouteController.h"
@interface RouteController (hidden)
- (CLLocationCoordinate2D) synyxLocation;
- (void)centerMapAroundAnnotations;
@end
@implementation RouteController
@synthesize mapView;
@synthesize synyx;
//NSString *synyxLoc = @"Karlstrasse 68, 76137 Karlsruhe, Germany";
// Use this for testing on the device if you are at synyx
//NSString *synyxLoc = @"Frankfurt, Germany";
// Use the following for testing, because simulator is in Cupertino
NSString *synyxLoc = @"San Francisco";
- (void)viewDidLoad {
@try {
CLLocationCoordinate2D location = [self synyxLocation];
//location.latitude = 49.002178399999998;
//location.longitude = 8.3941583000000008;
self.synyx = [[AddressAnnotation alloc] initWith:location];
[self.synyx setTitle:@"Synyx GmbH & Co. KG"];
[self.synyx setSubtitle:synyxLoc];
[mapView addAnnotation:synyx];
}
@catch (NSException * e) {
NSLog(@"exception!!!!!");
}
}
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView {
[mapView selectAnnotation:self.synyx animated:YES];
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Hinweis"
message:@"Zu Synyx GmbH & Co. KG\nin Google Maps routen?"
delegate:self
cancelButtonTitle:@"Nein"
otherButtonTitles: @"Ja", nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
MKUserLocation *loc = [self.mapView userLocation];
double latitude = loc.location.coordinate.latitude;
NSString *lat = [NSString stringWithFormat:@"%f",latitude];
double longitude = loc.location.coordinate.longitude;
NSString *lon = [NSString stringWithFormat:@"%f",longitude];
NSString *base = @"http://maps.google.com/maps?";
NSString *dest = [@"daddr=" stringByAppendingString:synyxLoc];
NSString *src = [[[@"saddr=" stringByAppendingString:lat] stringByAppendingString:@","] stringByAppendingString:lon];
NSString *all = [[[base stringByAppendingString:dest] stringByAppendingString:@"&"] stringByAppendingString:src];
NSString *url = [all stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if ([annotation isKindOfClass:[AddressAnnotation class]]) {
MKAnnotationView *annotationView = nil;
static NSString *StartPinIdentifier = @"StartPinIdentifier";
MKPinAnnotationView *startPin = [annotationView dequeueReusableCellWithIdentifier:StartPinIdentifier];
if (startPin == nil) {
startPin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:StartPinIdentifier] autorelease];
startPin.animatesDrop = YES;
startPin.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
startPin.canShowCallout = YES;
startPin.enabled = YES;
}
annotationView = startPin;
return annotationView;
}
return nil;
}
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
[self centerMapAroundAnnotations];
}
- (void)centerMapAroundAnnotations {
// if we have no annotations we can skip all of this
if ( [[self.mapView annotations] count] < 2 )
return;
// then run through each annotation in the list to find the
// minimum and maximum latitude and longitude values
CLLocationCoordinate2D min;
CLLocationCoordinate2D max;
BOOL minMaxInitialized = NO;
for ( id<MKAnnotation> a in [self.mapView annotations] ) {
// only use annotations that are of our own custom type
// in the event that the user is browsing from a location far away
// you can omit this if you want the user's location to be included in the region
// if we haven't grabbed the first good value, do so now
if ( !minMaxInitialized )
{
min = a.coordinate;
max = a.coordinate;
minMaxInitialized = YES;
}
else // otherwise compare with the current value
{
min.latitude = MIN( min.latitude, a.coordinate.latitude );
min.longitude = MIN( min.longitude, a.coordinate.longitude );
max.latitude = MAX( max.latitude, a.coordinate.latitude );
max.longitude = MAX( max.longitude, a.coordinate.longitude );
}
}
// Now that we have a min and max lat/lon create locations for the
// three points in a right triangle
CLLocation* locSouthWest = [[CLLocation alloc]
initWithLatitude: min.latitude
longitude: min.longitude];
CLLocation* locSouthEast = [[CLLocation alloc]
initWithLatitude: min.latitude
longitude: max.longitude];
CLLocation* locNorthEast = [[CLLocation alloc]
initWithLatitude: max.latitude
longitude: max.longitude];
// Create a region centered at the midpoint of our hypotenuse
CLLocationCoordinate2D regionCenter;
regionCenter.latitude = (min.latitude + max.latitude) / 2.0;
regionCenter.longitude = (min.longitude + max.longitude) / 2.0;
// Use the locations that we just created to calculate the distance
// between each of the points in meters.
CLLocationDistance latMeters = [locSouthEast getDistanceFrom: locNorthEast];
CLLocationDistance lonMeters = [locSouthEast getDistanceFrom: locSouthWest];
MKCoordinateRegion region;
region = MKCoordinateRegionMakeWithDistance( regionCenter, latMeters, lonMeters );
MKCoordinateRegion fitRegion = [self.mapView regionThatFits: region];
[self.mapView setRegion: fitRegion animated: YES];
[locSouthWest release];
[locSouthEast release];
[locNorthEast release];
}
// deprecated not needed since Synyx doesn't move :-)
- (CLLocationCoordinate2D) synyxLocation {
NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv",
[synyxLoc stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
NSArray *listItems = [locationString componentsSeparatedByString:@","];
double latitude = 0.0;
double longitude = 0.0;
if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
// 49.002178399999998
latitude = [[listItems objectAtIndex:2] doubleValue];
// 8.3941583000000008
longitude = [[listItems objectAtIndex:3] doubleValue];
}
else {
//Show error
// TODO show error
}
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
return location;
}
- (void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Fehler"
message:@"Die Karte konnten nicht geladen werden! Es besteht keine Datenverbindung oder der Server kann nicht erreicht werden."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
}
- (void)viewDidUnload {
self.mapView = nil;
self.synyx = nil;
}
- (void)dealloc {
[mapView dealloc];
[synyx dealloc];
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment