Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to show current location on MKMapView

Tags:

iphone

I am using MKMapView on my application. i need to show current location on simulator.
is it possible. to show current location on simulator.

like image 729
kiran kumar Avatar asked Oct 20 '10 04:10

kiran kumar


People also ask

How to get current location MapKit?

To get a user's current location, you'll first need to import MapKit and CoreLocation in the top of your view controller. Update Info. plist file : Your app to be able to request the user's current location, you will need to open Info.

How to get user current location in swift?

Get A User's Location Once Only once location authorization can be requested by using the CLLocationManager method requestLocation() . Implement the CLLocationManagerDelegate method locationManager(:, didUpdateLocations:) to handle the requested user location.

What is MapKit in iOS?

MapKit is a powerful API available on iOS devices that makes it easy to display maps, mark locations, enhance with custom data and even draw routes or other shapes on top.


2 Answers

In the simulator, the user's current location is always in Cupertino, California.

If you're using Interface Builder to add your map view, simply check the "Shows User Location" check box in the Attributes Inspector for the map view. (Select the map view and type command-1 to display the attributes inspector.)

If you're adding or manipulating the map view programmatically, set the showsUserLocation property of the map view to YES.


Update: It turns out that this is possible, just not using the built in map view functionality, and it doesn't always work.

Recent versions of the SDK (which have to run on Snow Leopard) can get the location of the machine the simulator is running on using CLLocationManager. You can then use this location to create an annotation to display on the map view. It won't behave like the built in "user's location indicator" (at least not without some work), but it will show the user's current location.

See this post for details of when this technique won't work.

See the "Related sample code" section of the CLLocationManager documentation for sample code that uses CLLocationManager and CLLocationManagerDelegate and then displays the user's location on a map view.

like image 129
Kris Markel Avatar answered Oct 04 '22 21:10

Kris Markel


 self.mapView.delegate =  self;
 self.mapView.showsUserLocation = YES;

This will show the current location in MkMapview.

If you are using Interface Builder,In the Attributes Inspector , we have an option Behaviour which has an option Show User Location, on checking that option will also do the same.

If you are not able to see in simulator,

  1. Open the application in the simulator.
  2. From the Menu Bar select Debug - > Location - > (If "None" option is selected ,change it to "Custom Location") and set location .

With CLLocationManager also we can get the current location, Import Corelocation FrameWork to the project

In .h file

#import <CoreLocation/CoreLocation.h>
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) CLLocation* currentLocation;

In .m file

if ([CLLocationManager locationServicesEnabled] )
    {
        if (self.locationManager == nil )
        {
            self.locationManager = [[CLLocationManager alloc] init];
            self.locationManager.delegate = self;
            self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
            self.locationManager.distanceFilter = kDistanceFilter; //kCLDistanceFilterNone// kDistanceFilter;
        }

        [self.locationManager startUpdatingLocation];
    }

The delegate function :

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    self.currentLocation = [locations lastObject];
   // here we get the current location
}

Hope this answer may help you.

like image 23
Aswathy Bose Avatar answered Oct 04 '22 23:10

Aswathy Bose