0

I have an ios application built to use mapkit and corelocation. the application works with no error but the map doesnt pin point my current location in xcode simulator and when run on an actual device it does not show the land scape and other things. just a blank map with a single road and it cannot be zoomed in or out. below is my code

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    //IF we have coordinate from manager
    //let location = locations.last as CLLocation
    if let location = locationManager.location?.coordinate{

        userLocation = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)

        let region = MKCoordinateRegion(center: userLocation!, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
        mapView.setRegion(region, animated: true)

         mapView.removeAnnotations(mapView.annotations)


        let annotation = MKPointAnnotation()
        annotation.coordinate = userLocation!
        annotation.title = "Me!"
        mapView.addAnnotation(annotation)


    }
}

additional codes would be supplied on request

King
  • 1,885
  • 3
  • 27
  • 84

2 Answers2

0

Here, this Class that I use to show a Pin to location -

import UIKit
import  MapKit
class ContactViewController: UIViewController,MKMapViewDelegate,CLLocationManagerDelegate {

@IBOutlet var viewBG1: UIView!
@IBOutlet var mapview: MKMapView!
override func showPinLocation() {

    let annotation = MKPointAnnotation()
    annotation.title = "Title"
    var coordinate = CLLocationCoordinate2D()
    coordinate.latitude = 28.702466
    coordinate.longitude = 77.1016314
    annotation.coordinate = coordinate

    var region = MKCoordinateRegion()
    var span = MKCoordinateSpan()

    span.latitudeDelta = 0.002;     // 0.0 is min value u van provide for zooming
    span.longitudeDelta = 0.002
    region.span=span;
    region.center = coordinate;     // to locate to the center
    self.mapview.addAnnotation(annotation)
    self.mapview.setRegion(region, animated: true)
    self.mapview.regionThatFits(region)
    // Do any additional setup after loading the view.
}
PPL
  • 6,357
  • 1
  • 11
  • 30
Diva
  • 129
  • 8
0

The simulator is doing what it is supposed to. You can change the location from the debug menu:

enter image description here

If you're using a storyboard, go to your map view and look for these things to edit your view:

enter image description here

And

enter image description here

You can set the map to hybrid and get streets and satellite, you can also make your mapView scalable and many other things.

If you want to set the type of view programmatically, you can implement a segmented control and do this:

@IBAction func segmentedControlAction(sender: UISegmentedControl!) {
    switch (sender.selectedSegmentIndex) {
        case 0:
            mapView.mapType = .Standard
        case 1:
            mapView.mapType = .Satellite
        default:
            mapView.mapType = .Hybrid
    }
}

And to programmatically set the UI :

self.mapView.zoomEnabled = true;
self.mapView.scrollEnabled = true;
self.mapView.userInteractionEnabled = true;
Jake
  • 2,126
  • 1
  • 10
  • 23