iPhone MapView Current Location


 

iPhone MapView Current Location

This is a simple MKMapView example in iPhone for the beginners. In the example we'll discuss about how to display a map on UIView.

This is a simple MKMapView example in iPhone for the beginners. In the example we'll discuss about how to display a map on UIView.

iPhone MapView Current Location

This is a simple MKMapView example in iPhone for the beginners. In the example we'll discuss about how to display a map on UIView. You can also display the map in different styles by assigning different mapTypes such as Standard, Satellite and Hybrid.

And apart from it, we can also show the current location on the map. By default on simulator it will show the US as current location but on running the application in iPhone it will give you the correct location.

To get the MKMapView in our application we'll have to include the MapKit.framework in frameworks folder and also required to import the mapkit in header file as given below...

#import <MapKit/MapKit.h>

In the same .h file create the IBOutlet and property for the MKMapView as given below..

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

@interface mapviewViewController : UIViewController {
IBOutlet MKMapView *mapview;

}
@property(nonatomic, retain) IBOutlet MKMapView *mapview;
@end

and in the .m, viewDidLoad method just add the given line of codes..

- (void)viewDidLoad {
[super viewDidLoad];
//mapview.mapType = MKMapTypeSatellite;
//mapview.mapType=MKMapTypeStandard;

//mapType
mapview.mapType=MKMapTypeHybrid;
//will show the current location
mapview.showsUserLocation = YES;

}

do not forget to add the MapView on UIView in interface builder. Also link it to the file owner otherwise it'll not show anything.

On building the application your application should look alike the given image.

Download Code

Ads