Maps




CS175

Chris Pollett

Nov 26, 2014

Outline

Introduction

Determining Location in HTML 5

HTML Mapping

Google Maps

Google Map

A Google Map with Pin

Open Street Map

Open Street Map Screen Shot

Open Street Map Example

Maps in iPhone Native

MapCallouts Screenshot

MapCallout App Screenshot

Building the App

MapViewController.m

MapViewController.h is standard boiler plate, the implementation file has all the specifics:

DetailViewController

DetailViewController.h is boilerplate, the implementation file loads the image of the Golden Gate breidge and draw its on the screen.

#import "DetailViewController.h"

@implementation DetailViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // fit the our popover size to match our image size
    UIImageView *imageView = (UIImageView *)self.view;
    
    // this will determine the appropriate size of our popover
    self.preferredContentSize = CGSizeMake(imageView.image.size.width, imageView.image.size.height);
    self.title = @"Golden Gate";
    
    // for this view controller we want a black style nav bar
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
}

@end

SF Annotation

To control the text next to our pins we use Annotations. For San Francisco the header looks like:

@interface SFAnnotation : NSObject 

+ (MKAnnotationView *)createViewAnnotationForMapView:(MKMapView *)mapView 
       annotation:(id )annotation;
@end

The implementation looks like:

#import "SFAnnotation.h"

@implementation SFAnnotation 

- (CLLocationCoordinate2D)coordinate
{
    CLLocationCoordinate2D theCoordinate;
    theCoordinate.latitude = 37.786996;
    theCoordinate.longitude = -122.419281;
    return theCoordinate; 
}

- (NSString *)title
{
    return @"San Francisco";
}

// optional
- (NSString *)subtitle
{
    return @"Founded: June 29, 1776";
}

+ (MKAnnotationView *)createViewAnnotationForMapView:(MKMapView *)mapView annotation:(id )annotation
{
    MKAnnotationView *returnedAnnotationView =
        [mapView dequeueReusableAnnotationViewWithIdentifier:NSStringFromClass([SFAnnotation class])];
    if (returnedAnnotationView == nil)
    {
        returnedAnnotationView =
            [[MKAnnotationView alloc] initWithAnnotation:annotation
                                         reuseIdentifier:NSStringFromClass([SFAnnotation class])];

        returnedAnnotationView.canShowCallout = YES;
  
        // offset the flag annotation so that the flag pole rests on the map coordinate
        returnedAnnotationView.centerOffset = CGPointMake( returnedAnnotationView.centerOffset.x + 
           returnedAnnotationView.image.size.width/2, returnedAnnotationView.centerOffset.y - returnedAnnotationView.image.size.height/2 );
    }
    else
    {
        returnedAnnotationView.annotation = annotation;
    }
    return returnedAnnotationView;
}

@end

Bridge Annotation

The header looks like:

#import <MapKit/MapKit.h>

@interface BridgeAnnotation : NSObject 

+ (MKAnnotationView *)createViewAnnotationForMapView:(MKMapView *)mapView 
     annotation:(id )annotation;
@end

The implementation looks like:

#import "BridgeAnnotation.h"

@implementation BridgeAnnotation

- (CLLocationCoordinate2D)coordinate
{
    CLLocationCoordinate2D theCoordinate;
    theCoordinate.latitude = 37.810000;
    theCoordinate.longitude = -122.477450;
    return theCoordinate; 
}

// required if you set the MKPinAnnotationView's "canShowCallout" property to YES
- (NSString *)title
{
    return @"Golden Gate Bridge";
}

// optional
- (NSString *)subtitle
{
    return @"Opened: May 27, 1937";
}

+ (MKAnnotationView *)createViewAnnotationForMapView:(MKMapView *)mapView annotation:(id )annotation
{
    // try to dequeue an existing pin view first
    MKAnnotationView *returnedAnnotationView =
        [mapView dequeueReusableAnnotationViewWithIdentifier:NSStringFromClass([BridgeAnnotation class])];
    if (returnedAnnotationView == nil)
    {
        returnedAnnotationView =
            [[MKPinAnnotationView alloc] initWithAnnotation:annotation
                                            reuseIdentifier:NSStringFromClass([BridgeAnnotation class])];

        ((MKPinAnnotationView *)returnedAnnotationView).pinColor = MKPinAnnotationColorPurple;
        ((MKPinAnnotationView *)returnedAnnotationView).animatesDrop = YES;
        ((MKPinAnnotationView *)returnedAnnotationView).canShowCallout = YES;
    }
    
    return returnedAnnotationView;
}

@end