
I got some time to play with some more Map Kit stuff today. And I was able to add annotations.
UPDATE: Follow the 3rd part here.
http://blog.objectgraph.com/index.php/2009/04/08/iphone-sdk-30-playing-with-map-kit-part-3/
There are a couple of classes to be familiar with
MKMapView
This is our main class that displays a map. It inherits from UIView, so you can just initialize it and add it to your MainView
MKPinAnnotationView
This view is helpful if you would not like to subclass the MKAnnotationView, It provides a basic pin annotation with interaction enabled
MKReverseGeoCoder
This class provides a placemark via a callback asynchronously given a coordinate
MKPlacemark
A representation of an annotation
Also some delegates are useful
MKMapViewDelegate
This is an important one as whenever you add an annotation to the map, there is a callback method you need to implement to show the View of the annotation.
MKReverseGeocoderDelegate
Used in conjunction with MKReverseGeoCoder to obtain placemarks
Here is my code for the MainViewController.h
1 2 3 4 5 6 7 8 9 10 11 12 13 | #import "FlipsideViewController.h" #import <MapKit/MapKit.h> #import <MapKit/MKAnnotation.h> #import <MapKit/MKReverseGeocoder.h> @interface MainViewController : UIViewController <FlipsideViewControllerDelegate,MKReverseGeocoderDelegate,MKMapViewDelegate> { MKMapView *mapView; MKReverseGeocoder *geoCoder; MKPlacemark *mPlacemark; IBOutlet UISegmentedControl *mapType; } - (IBAction)changeType:(id) sender; @end |
My MainViewController.m code is below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | #import "MainViewController.h" #import "MainView.h" @implementation MainViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { // Custom initialization } return self; } // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { [super viewDidLoad]; mapView=[[MKMapView alloc] initWithFrame:self.view.bounds]; mapView.showsUserLocation=TRUE; mapView.mapType=MKMapTypeStandard; mapView.delegate=self; /*Region and Zoom*/ MKCoordinateRegion region; MKCoordinateSpan span; span.latitudeDelta=0.2; span.longitudeDelta=0.2; CLLocationCoordinate2D location=mapView.userLocation.coordinate; location.latitude=40.814849; location.longitude=-73.622732; region.span=span; region.center=location; /*Geocoder Stuff*/ geoCoder=[[MKReverseGeocoder alloc] initWithCoordinate:location]; geoCoder.delegate=self; [geoCoder start]; [mapView setRegion:region animated:TRUE]; [mapView regionThatFits:region]; [self.view insertSubview:mapView atIndex:0]; } - (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller { [self dismissModalViewControllerAnimated:YES]; } - (IBAction)showInfo { FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil]; controller.delegate = self; controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; [self presentModalViewController:controller animated:YES]; [controller release]; } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } - (void)viewDidUnload { // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (void)dealloc { [super dealloc]; } - (IBAction)changeType:(id)sender{ if(mapType.selectedSegmentIndex==0){ mapView.mapType=MKMapTypeStandard; } else if (mapType.selectedSegmentIndex==1){ mapView.mapType=MKMapTypeSatellite; } else if (mapType.selectedSegmentIndex==2){ mapView.mapType=MKMapTypeHybrid; } } - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error{ NSLog(@"Reverse Geocoder Errored"); } - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark{ NSLog(@"Reverse Geocoder completed"); mPlacemark=placemark; [mapView addAnnotation:placemark]; } - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{ MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"]; annView.animatesDrop=TRUE; return annView; } @end |
Related posts:
- iPhone SDK 3.0 – Playing with Map Kit – Part 3 This is the 3 part of me Playing with Map...
- iPhone SDK 3.0 – Playing with Map Kit I started looking at the Map Kit API for developing...
- iPhone SDK 3.0 – Playing with Game Kit – Part 1 My first series of articles explaining how to use Game...
- bug in FireFox? Look at the following code. <html> <head><title>Bug in Mozilla?</title></head> <script...
- iPhone Development and a sample Objective C Program Kiichi and myself are learning how to program in Objective...
Related posts brought to you by Yet Another Related Posts Plugin.
























46 Responses
Am I actually able to draw a polyline on the map ?
Thanks
Hi Gavi,
I’m trying to draw a poly line from one point to another but I’m unsure if I’m actually able to do it. Could you please let me know if it’s doable.
Cheers
@Ali: I will try to write a Part 3 of this article to see how to draw poly lines.
@Ali: After looking at documentation, it looks like at this time there is no way to draw a polyline like in the javascript maps API.
The only thing you can do is add annotations
This would be so stupid mate!! I had a look at the framework too but couldn’t find a method in any of the header files!!! Can’t we actually use any native google maps classes ? like the GPolyLine etc. ?
@Ali: Yes, I know.. I hope they update them before the final version
Hi Gavi,
Thanks for the great post. It did helped me a lot….Thanks once again. After going through your tutorial I am able to integrate maps and place a pin. Now I am actually trying to point multiple locations at a time…
It would be great if you can explain how it can be done.
Cheers
nice intro. it’s weird that the views for the annotations are taken care with a delegate method. In order to have pins with different pictures I had to set the title of the annotation and assign the views based on that.
I’m willing to work on the polyline thing next if anyone is interested to pump it out together. Also, anyone thought about how draggable markers could be implemented? Seems like overriding touchesmoved/began doesn’t do anything.
nice article. What method would I use to get the coordinates for my current location?
Any references docs on this?
Thanks!
@Jordon,
Please check Part 3 of this series. I am using Core Location to get Coordinates
http://blog.objectgraph.com/index.php/2009/04/08/iphone-sdk-30-playing-with-map-kit-part-3/
Hi gavi,
your tutorial has given me some knowledge of how to use maps api, but i am not able to get the below two classes.
“MKReverseGeoCoder” “MKPlacemark”
and i want the list of locations once the user enters the zipcode , how is this possible, if any one can help me out then it is greatly appreciable.
thanks in advance.
Hi! Thanks for the examples! They helped me a lot : )
But the only thing is that I can’t set a other then the Apple headquarters. The app that I’m developing is for the Netherlands. How do I get the simulator’s location to the Netherlands.
I don’t want to install a beta OS on my only iPhone, and I don’t have money for an extra development device : (
Hi Gavi
I went through you tutorial
It was easy to understand.
But my problem is I am not able to see a pin on location I specified in code.
Neither map is zooming to that location……..
Can you please help me in this?
Thanks…………….
Your Comments
i am not able to find the MKReverseGeoCoder class in the api , can anyone help me out .
great article!! cheers for that. just a question tho – im confused about the code that references #import “FlipsideViewController.h” , was this file created in an earlier article ?
cheers
I am curious, what exactly is in your MainView.h and MainView.c files?
How does changeType called? Am I missing something?
How do i add multiple annotations to a single map view ….I am not able to add annotations….please help me…
[...] First off, you’re no doubt aware from posts like this that much of the Maps.app functionality is exposed to any developer through MapKit.framework in OS [...]
Thanks for this POST.
Can you help plz here.
i imported mapkit(i mean framework)
Your first part ran successfully wen comes to 2 part it is showing errors.
I got it, it works in sdk3.0 beta only right?
Yeah i am downloading that…
Hi..i am making a maps related application….
i would really appreciate if you could fwd me the source code for” MAP KIT Part 2 ” …i dnt want the parking thing in it….also i want the bounds ….hw do i calculate them….
thanks
Great article, helped me a lot in developing my first iPhone application. Thanks a lot for such great work.
Rajkumar:
you can use the addAnnotation or addAnnotations method for adding multiple annottions. some sample code.
Annotation *annotation = [[[Annotation alloc] initWithCoordinate:coordinate markerID:markerid] autorelease];
annotation.currentTitle = @”Some Title”;
[mapView addAnnotation:annotation];
bansidhar
hi Gavi, thanks for this tutorial ,but i have question:
is there any code to make the pin(MKPinAnnotationView) to move when the user moved or touch?
Is it possible to cast from string to CLLocationDegree?
I have a NSString currenLatitude =@”52.502498″
CLLocationCoordinate2D coords1;
coords1.latitude=[currentLatitude doubleValue];
doesn´t work coords1.latitude will be -4,23232432432423e
How can i get the correct Values from the String?
hi Gavi, thanks for this tutorial ,but i have question:
I want annotation on my current location and the latitudes and longitudes for that location.
i tried part 3 but i was not able to solve the issue.
was having some issues had the mapview as an IBOutlet where the annotation was not showing up. Works after I added this:
annView.canShowCallout = YES;
in the fuction:
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id ) annotation{
How is showInfo called? or is it ever called?! Why is it there?
Same with changeType?!
@Doane,
showInfo is from the template code when you select a project type of Utility App.
Hello gavi
its really good tutorial.
but could explain anything about “FlipsideViewController”
is it because you have created utility application or something different
Mkmapview is not auto rotating to landscape for me. My app has tabbar with shouldautorotate implemented correctly in my map controller. Any comments?
how can we show all marker on map.
Hello,
I have created all the markers on Map. I have a problem. I want to zoom in zoom out all markers on depending upon number of marker. If marker is very few in number then it zoom out according to it end if its a large in number then its zoom out according to it.
Thanks
haha I didn’t realize that the location used in the example was a Long Island, NY location. Living on the Island I saw the location come on on my device when I was testing and thought I was doing something wrong. Racked my brains for 30 minutes! lol Great read btw!
For those asking about “FlipsideViewController”, “MainView” or pretty much anything else where it seems like magical code was left out of the tutorial, make sure that when you create a new project it is of the type “Utility Application”. This will help you understand what’s going on.
Thanks so much for sharing this, gavi. You rock.
Thank you so much for this article.
But I’ve got a small probleme. n wifi connection it works fine, but when I am in 3G, my app crashes. It seems that it comes from MKReverseGeocoder… does anyone got an idea ?
Hi Gavi,
We are not getting following three classes in Mapkit. We have kalway 10.5.3 with sdk 3.0 installed. Please let me know why these classes are not there. we need it badly.
MKPlacemark.h
MKReverseGeocoder.h
MKUserLocation.h
Hi Gavi,
One basic doubt.. I have to show a bunch of places whose address is known to me and I dont know their lattitude and longitude..
How do i proceed in this case?
Thanks in advance
Gaurav
CLLocationCoordinate2D location=mapView.userLocation.coordinate; //Why do this ?
location.latitude=40.814849; //If you are doing these two lines anyway ?
location.longitude=-73.622732;
MKCoordinateRegion region;
region.center=location;
how can i draw a graph in iPhone application.
pls HELP ME..!!
in iPhone app, how can i output the CSV file for some data
please tell me if there is some good tutorials.
10 q.
how can i draw a graph in iPhone App on the given data?
pls HELP ME..!!
Annotation message was not showing…
Solution-> Modify the function as shown below…
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id ) annotation{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@”currentloc”];
annView.animatesDrop=TRUE;
annView.canShowCallout = YES;
return annView;
}
Hi gavi
may i know
1.how to add the text over the image,and append it as a image
2.how to pass the browsed images from one view to the next views,
3.how to use the soap webservices
im born indian dont have that much money ($500) to get trained ,so kindly help me out in this problem through mail…please
Thanks in Advance
all the best ,may sai bless all
Thanks for your posts. They are very useful.