How to show current user location on map in iphone or ios devices?

1 comment
Get current user location and display it on map in ios devices . Here i am giving you complete steps to develop it and source code so you can easily understand it. In ios developing we have mapkit framwork which will give your solution.

Step 1:
As I told you that you have to add mapkit framework in ye you snapshot for our project. Here i already giving you snap to add mapkit framework in project.
Step 2:
After Adding framework you need to add header file in your ViewController.h file like
#import <MapKit/MapKit.h> 
 Now you need to add mapview object in your view. In right side bottom it is showing all objects just dragged mapview from there and put it in to your view.
Step 3:
After it you have to create outlet for mapview like this snap. for it you have to select mapview and press ctrl key and dragline to header file then it will show you complete pop up box like this give an id and connect it.
@property (weak, nonatomic) IBOutlet MKMapView *mapview;

Step 4:
 Now you can see that here .h and .m file snap. in viewcontroller.h file you have to just add  delegate

<MKMapViewDelegate> add it after @interface ViewController : UIViewController like this.
@interface ViewController : UIViewController<MKMapViewDelegate>
after it in viewcontroller.m file add this code snippest

  mapview.delegate = self;
    if(![mapview showsUserLocation])
    {
     
        mapview.showsUserLocation = YES;
    }
using this code snippest it will ask for enabling to give current user location.
 CLLocationCoordinate2D location = [[[self.mapview userLocation] location] coordinate];
 
    NSLog(@"Location found from Map: %f %f",location.latitude,location.longitude); 
For getting Longitude and Latitude of current user Location.


Step 5:
Now run your application from left panel upper side then it will give you this pop up menu.
Here it will ask for permission to get user current location. When you pressed ok than it will allow you. 

 Step 6:
Some times it will not show you current user location for it in menu you have to set particular location for simulator . when you want device current location you have to test on it otherwise it will not show you any location. here it is showing fixed location.
Step 7:
Here this snap will give you complete code of both files.


Code for this application..

ViewController.h

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapview;
@end
 ViewController.m
#import "ViewController.h"
#import <MapKit/MapKit.h>
@interface ViewController ()
@end
@implementation ViewController
@synthesize mapview;
- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
mapview.delegate = self;
if(![mapview showsUserLocation])
{
mapview.showsUserLocation = YES;
}
    CLLocationCoordinate2D location = [[[self.mapview userLocation] location] coordinate];
 
    NSLog(@"Location found from Map: %f %f",location.latitude,location.longitude);
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

I hope you are clear with this still you need any help just leave comment on it or send me mail on devang9978@gmail.com. I will give you possible help..

Devang Patel(www.techstudy.net)
Iphone Developer

1 comment:

  1. create personal, professional, community maps and share on social network, blogs and sites on community walk

    ReplyDelete