Home Tutorial Iphone Examples MapView Example in iPhone

 
 

Share on Google+Share on Google+
MapView Example in iPhone
Posted on: August 9, 2010 at 12:00 AM
Advertisement
The example illustrate how to embed or display a MapView in iPhone SDK UIView based application.

MapView Example in iPhone

The example illustrate how to embed or display a MapView in iPhone SDK UIView based application.

To embed a Map in your iphone application just follow the given steps..

1. Create a new project (ViewBased application )
2. Import the MapKit in header file...
#import <MapKit/MapKit.h>  

also add the MapKit.framework under resources folder from the existing frameworks.

3. In the same viewController's header file call the <MKMapViewDelegate> and define the MKMapView *myMapView; synthesize and release the myMapView object into implementation file.

4. In a viewcontroller.xib file ..add the map view on UIView and connect it with corresponding object in file owner. Further see the code given below….

Header

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface myMapViewController : UIViewController <MKMapViewDelegate> {

IBOutlet MKMapView* myMapView;

}

@property (nonatomic, retain) IBOutlet MKMapView* myMapView;

-(void)displayMYMap;

@end

Implementation

#import "myMapViewController.h"

@implementation myMapViewController

@synthesize myMapView;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];

myMapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
myMapView.delegate=self;

[self.view addSubview:myMapView];
[NSThread detachNewThreadSelector:@selector(displayMYMap) toTarget:self withObject:nil];

}

-(void)displayMYMap
{
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;

CLLocationCoordinate2D location;

location.latitude = 22.569722 ;
location.longitude = 88.369722;


region.span=span;
region.center=location;

[myMapView setRegion:region animated:TRUE];
[myMapView regionThatFits:region];
}

- (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)dealloc {
[super dealloc];
[myMapView release];
}

@end

Build the application to display the map view on UIView. It should look like the given image.

Download code of MapView Example in iPhone

Advertisement

Related Tags for MapView Example in iPhone:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 9, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com