How to embed a map on the iPhone

Originally posted at http://www.richardhyland.com/diary/2009/10/17/how-to-embed-a-map-on-the-iphone/ (Posterous keeps messing with the code layout, go the original post on my website for the working version!)

 

In the first of a few posts I intend to make I’ll demonstrate some examples using the iPhone SDK.

This example will demonstrate how to embed a map, using the MKMapKit framework inside a UIView using the iPhone SDK. Note you must be using iPhone OS 3.0 or higher for this to work. The ultimate aim is to get a UIView that looks similar to this.

For this example I will assume you are already familiar with navigation and UIViews in the SDK (and so I won’t cover how to get the navigation bar at the top of this screenshot)

MKMapKit

Add the framework

First up you must go to the Frameworks folder of your XCode project and add the existing framework of MKMapKit.

Then inside the header file for your view add

  1. #import <MapKit/MapKit.h>  

Adding MapKit references to the header

Now we must add the mapKit instance to the header as well as the MapKit delegate

  1. @interface MapKitViewController : UIViewController <mkmapviewdelegate> {  
  2.     MKMapView *mapView;  
  3. }  
  4. -(void)displayMap;  
  5. </mkmapviewdelegate>  

Initialize the Map

  1. - (void)viewDidLoad {  
  2.     mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];  
  3.     mapView.delegate=self;  
  4.   
  5.     [self.view addSubview:mapView];  
  6.     [NSThread detachNewThreadSelector:@selector(displayMap) toTarget:self withObject:nil];  
  7. }  
  8.   
  9. -(void)displayMap {  
  10.     MKCoordinateRegion region;  
  11.     MKCoordinateSpan span;  
  12.     span.latitudeDelta=0.2;  
  13.     span.longitudeDelta=0.2;  
  14.   
  15.     CLLocationCoordinate2D location;  
  16.     location.latitude = -35;  
  17.     location.longitude = 146.2381;  
  18.     region.span=span;  
  19.     region.center=location;  
  20.   
  21.     [mapView setRegion:region animated:TRUE];  
  22.     [mapView regionThatFits:region];  
  23. }  
  24.   
  25. - (void)dealloc {  
  26.     [mapView release];  
  27.         [super dealloc];  
  28. }  

This will give us a map fitting the screen with the region and zoom level set to best fit the coordinates given. Note that I have physically defined the coordinates here in this example, you can use something like the Google GeoCode API to convert addresses, etc to coordinates. I won’t cover that here.

Changing the map type

In the example above is a tool bar allowing you to switch map types

Let’s define the toolbar in the header file inside the @implementation

  1. UISegmentedControl *buttonBarSegmentedControl;  

Now inside the main code inside ViewDidLoad we add

  1. buttonBarSegmentedControl = [[UISegmentedControl alloc] initWithItems:  
  2.     [NSArray arrayWithObjects:@"Standard", @"Satellite", @"Hybrid", nil]];  
  3.     [buttonBarSegmentedControl setFrame:CGRectMake(30, 10, 280-30, 30)];  
  4.         buttonBarSegmentedControl.selectedSegmentIndex = 0.0;   // start by showing the normal picker  
  5.     [buttonBarSegmentedControl addTarget:self action:@selector(toggleToolBarChange:) forControlEvents:UIControlEventValueChanged];  
  6.     buttonBarSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;  
  7.     buttonBarSegmentedControl.backgroundColor = [UIColor clearColor];  
  8.     [buttonBarSegmentedControl setAlpha:0.8];  
  9.   
  10.     [self.view addSubview:buttonBarSegmentedControl];  

Then we must add the function for what happens when we tap on the tool bar

  1. - (void)toggleToolBarChange:(id)sender  
  2. {  
  3.     UISegmentedControl *segControl = sender;  
  4.   
  5.     switch (segControl.selectedSegmentIndex)  
  6.     {  
  7.         case 0: // Map  
  8.         {  
  9.             [mapView setMapType:MKMapTypeStandard];  
  10.             break;  
  11.         }  
  12.         case 1: // Satellite  
  13.         {  
  14.             [mapView setMapType:MKMapTypeSatellite];  
  15.             break;  
  16.         }  
  17.         case 2: // Hybrid  
  18.         {  
  19.             [mapView setMapType:MKMapTypeHybrid];  
  20.             break;  
  21.         }  
  22.     }  
  23. }  

In the next example, coming soon, I’ll cover how to add the annotations (the small pins) to the map

 

The nasty side of Twitter

** Caution there is some pretty foul language in this post, even though I've censored it **

For most Twitter is a fantastic communication tool.  The way in which any one can follow anyone else and follow their stream of Tweets is exactly what makes Twitter the success that it is.  Unfortunately this level of openness is not without it's pit falls.

I've been following @KirstieMAllsopp for a while now, in case you don't know who Kirstie is she is British TV presenter and property guru.  After reading a few of her messages, it became clear that Kirstie and other famous celebrity tweeters are the victims of an Internet bullying campaign.

The bullies themselves don't see that their words are bullying but by the very definition of what cyber bullying is, they are.  Now I won't link to any of their accounts here because it'll just give them some sick perversion and draw attention to them directly.

However Kirstie was unable to block them at the time from the mobile website of Twitter and had to wait until she could, consequently a number of people replied to Kirstie offering kind words of support only to have the bullies turn on them and flood their @mentions feed with abuse.

I too have had these messages sent to me and I'll post a few examples now (self censored so as not to offend)

@KirstieMAllsopp f***** block me will ya ya fat do goody scrubber. Yer programme is s*** and ur fat nd ugly lol

@twbrit u think u r so f****** clever e mailing that fat bird off telly well keep ur f***** thoughts to yerself bout my mates or Ill ave u

@richardhyland if u bad mouth @****** or @********* gin u 4 eyed gimp Ill f****** kick ur chav arse this will be ur only warning c***

And that was just three of around 30 tweets from one of these users in around 30 minutes!

Others included

@KirstieMAllsopp fat s*** (via @******) this one is trying to ban you all @****** She is a seriously obese dull bitch

@KirstieMAllsopp kirstie, where on your body is the perfect location for me to s***? your chest? your hair? your eyes?

I think any sane person will agree it's pretty foul stuff and whilst most adults swear at some point, there is simply no excuse or point in their sad little tweets apart from upsetting normal people using a great service like Twitter.

Twitter do provide a method of feedback to harassment, yet a week after this came to my attention, every single one of the accounts is still active.  Is Twitter unable or unwilling to do something about it?

Celebrities have often had to put up with a lot because of their fame, paparazzi, untrue stories, but no one deserves to be abused or bullied in this way whether it be online or in person.

Update: Since I posted this, one of the accounts has now gone, I don't think it was suspended but the user either deleted it or changed his screen name

Update II: Two down now... hopefully more to come

Hi Posterous

This is my first post here, I thought I'd just check it out and see what it was like.

I have my main blog over at http://www.richardhyland.com/diary/ so anything will be posted there first.  There are plenty more posts over there on iPhone SDK tips and tricks, etc over there which I'm afraid to say I can't be bothered to copy and paste into an archive at the moment.

However I've added my last three blog posts from my main site over just as a start.

Computer Game Censorship

I read an article today from The Register entitled GTA maker coughs up $20m for 'hot coffee' sex.

Now if anyone doesn't know the background to this story, here it is.  When Take2 released the game Grand Theft Auto: San Andreas there was a hidden mini game in it that involved the character having sex with his girlfriend.

Now this scene, whilst included in the code, was NOT accessible through the gameplay unless you applied a hack to the game itself.

In the US, typically, 'everyone went mad' slapping an Adults only rating on the game instead of a Mature rating, a game I'll note that had already been given an 18 rating by the BBFC here in the UK.

Take2 also spent 10's of millions pulling all copies off the shelves and then releasing a patched version... only for class-action lawsuits to come rolling in.

Let's put this in perspective shall we? A checklist of what's in GTA games

  • Guns - OK
  • Stealing cars - OK
  • Murder - OK
  • Police killing - OK
  • Organised crime - OK
  • Blowing things up - OK
  • Prostitutes - OK
  • Mini-game for having sex with your girlfriend (whilst both characters were FULLY CLOTHED) - OMG we must pull the game off the shelves and everyone sues us!

Here is my take (and also one I read in the comments of the original article) on the situation.

A child keeps pestering their parent to buy them GTA, so they go and buy it regardless of the rating because 'it's a computer game' and 'all computer games are for kids' aren't they?

News of this game hack hits the news which now brings to their parent's attention that this game is full of sex and violence.

This embarrasses the parent that their bad parenting skills have meant that their little angel is playing a game full of content not designed for them... hence the entire purpose of the RATING SYSTEM!  Obviously the parent can't be to blame, it must be the fault of the developers, so let's sue them!

 

The iTunes AppStore Review Process

Being a small fry iPhone app developer, that being I do so in my own spare time because I like developing apps for the iPhone, I've simply got to comment about the App Store review process as it stands.

I won't make any individual comments about my personal rejections I've had except to say. I have had a number of rejections.

One update got rejected numerous times because apparently if you use a UIWebView where the usermight be able to get to Google and therefore access pictures of <shock>naked people</shock> then you have to rate your App as 17+.  Let us ignore the fact that they could just close your app and go to mobile Safari shall we, it would be by far easier?

Another one I reason I've been rejected for features not doing something Apple think they should, yet that very same feature was actually in the version before it.... still on sale in the App Store.  Inconsistent reviewing doesn't even begin to cover it!

So this leads me on to a blog post I read today from Joe Hewitt.  For anyone who doesn't know who Joe is, he is the guy who wrote the amazing Facebook app for the iPhone.

I have only one major complaint with the App Store, and I can state it quite simply: the review process needs to be eliminated completely.

Does that sound scary to you, imagining a world in which any developer can just publish an app to your little touch screen computer without Apple's saintly reviewers scrubbing it of all evil first? Well, it shouldn't, because there is this thing called the World Wide Web which already works that way, and it has served millions and millions of people quite well for a long time now.

Go and read his excellent post now at what I totally agree with.... especially as I have one app that I originally submitted a month ago, awaiting approval!