0

How to show user on Map using mapkit in IOS. This code only show the India on the map instead of coordinates defined.

    mapView.delegate = self;
    mapView.showsUserLocation = YES;
    objLocationManager = [[CLLocationManager alloc] init];
    objLocationManager.distanceFilter = kCLDistanceFilterNone;
    objLocationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    objLocationManager.delegate = self;
    #ifdef __IPHONE_8_0
    if(IS_OS_8_OR_LATER) {
        // Use one or the other, not both. Depending on what you put in info.plist
        //[objLocationManager requestWhenInUseAuthorization];
        [objLocationManager requestAlwaysAuthorization];
    }
    #endif
   [objLocationManager startUpdatingLocation];

    mapView.showsUserLocation = YES;
    [mapView setMapType:MKMapTypeStandard];
    [mapView setZoomEnabled:YES];
    [mapView setScrollEnabled:YES];
     mapView.delegate=self;
Cong Tran
  • 1,448
  • 14
  • 30
Chandan Kumar Jha
  • 325
  • 2
  • 4
  • 18

2 Answers2

1

You are starting Location Updates and you will get your current location through it. But how will MKMapview know about the Location Changes?

For MKMap View to adopt Location changes you have to set delegate to MKMapview. That part you have done already, but you forgot the delegate method:

- (void)mapView:(MKMapView *)aMapView didUpdateUserLocation:(MKUserLocation *)aUserLocation {}

This delegate method will send your current location to MapView. That is all you need.

If you want to use the default Delegate method, then you have to place marker on map with a current coordinate received from CLLocation's Update Method.

If you are going to use default delegate method then don't forget to confirm MKMapViewDelegate.

Let us know if it works. I am sure it will. Have a good day.

John Gorenfeld
  • 2,015
  • 1
  • 18
  • 27
Devang Tandel
  • 2,988
  • 1
  • 21
  • 43
  • Yes, I have already used this delegate and mentioned the code their MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800); [self.mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES]; [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES]; this is also not working. – Chandan Kumar Jha Dec 16 '15 at 04:47
  • Log printed in my program is Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first. – Chandan Kumar Jha Dec 16 '15 at 04:50
  • After adding the few lines its show the zoomed location but not show the annotation on map. MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE); [self.mapView setRegion:viewRegion animated:YES]; – Chandan Kumar Jha Dec 16 '15 at 05:11
  • Have you authorised your app for Location updates? You have to add key to ask for location permission in info.plist.. NSLocationWhenInUseUsageDescription NSLocationAlwaysUsageDescription This are two keys you have to use by appropriate location usage in you app. – Devang Tandel Dec 16 '15 at 05:13
  • Yes I have done it in info.plist Adding a notation with a specific coordinates show on the map but only the current location of device is not displayed on the map. – Chandan Kumar Jha Dec 16 '15 at 05:47
  • Sorry, I rechecked NSLocationAlwaysUsageDescription is missing in the info.plist now working fine. Thanks – Chandan Kumar Jha Dec 16 '15 at 05:56
0

Step: 1 - Import

import MapKit
import CoreLocation

Step: 2 - Add Delegate

MKMapViewDelegate,CLLocationManagerDelegate

Step: 3 - Declare

@IBOutlet var mapView: MKMapView!
var locationManager = CLLocationManager()
var myLocation = CLLocation()

Step: 4 - Inside ViewDidLoad

locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled()
    {
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.distanceFilter = kCLDistanceFilterNone
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.startUpdatingLocation()
    }

//MARK: - MapView and Location Manager -
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
    self.myLocation = locations.last!
    print("NewLocation \(locations[0].coordinate.latitude) \(locations[0].coordinate.longitude)")
}

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?
{
    let annotationIdentifier = "AnnotationIdentifier"
    var annotationView: MKAnnotationView?
    if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
        annotationView = dequeuedAnnotationView
        annotationView?.annotation = annotation
    }
    else
    {
        let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
        av.rightCalloutAccessoryView = UIButton(type: .roundedRect)
        annotationView = av
    }

    if let annotationView = annotationView
    {
        annotationView.canShowCallout = true
        annotationView.image = UIImage(named: "Location")
    }
    return annotationView
}

self.mapView.showsUserLocation = true

 let geoCoder = CLGeocoder()
                        geoCoder.geocodeAddressString(address!) { (placemarks, error) in
                            guard
                                let placemarks = placemarks,
                                let location = placemarks.first?.location
                                else
                            {
                                return
                            }

                            let newPin = MKPointAnnotation()
                            let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
                            let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
                            self.mapView.setRegion(region, animated: true)

                            newPin.title = address
                            newPin.coordinate = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
                            self.mapView.addAnnotation(newPin)
                        }

@IBAction func btnDirection(_ sender: Any)
{
    let address = self.dictEventDetails.object(forKey: "address") as? String
    let geoCoder = CLGeocoder()
    geoCoder.geocodeAddressString(address!) { (placemarks, error) in
        guard
            let placemarks = placemarks,
            let location = placemarks.first?.location
            else
        {
            return
        }
        let coordinate = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
        let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: coordinate, addressDictionary:nil))
        mapItem.name = address
        mapItem.openInMaps(launchOptions:nil)
    }
}
Tej Patel
  • 119
  • 2
  • 5