How to open maps with latitude and longitude in iOS

How to open maps with latitude and longitude in iOS
November 29, 2018 2 Comments iOS Development Pushpendra Kumar



If you are here in this post then defiantly you must be looking for the easiest way for opening the Map in iOS. I am going to tell you some simple solution with the complete methodology. Here you will get the very simple code for opening the map programmatically.

At the very first i am telling you that how to point a particular address in map via swift programming language.


func openMapForPlace() {
        let latitude: CLLocationDegrees = 37.2
        let longitude: CLLocationDegrees = 22.9
        
        let regionDistance:CLLocationDistance = 10000
        let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
        let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
        let options = [
            MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
            MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
        ]
        let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
        let mapItem = MKMapItem(placemark: placemark)
        mapItem.name = "Place Name"
        mapItem.openInMaps(launchOptions: options)
    }

Before that you also need to import one package in your project view controller which is as…


import MapKit




After doing that, I am sure that you must have another question your mind. Here, I may be wrong. But now I am telling you how you can provide a default path in between source and destination. So let’s have look on that example.


func openMapWithPath() {
        
let source = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: 28.7041, longitude: 77.1025)))
        source.name = "Delhi INDIA"
        let destination = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: 29.9671, longitude: 77.5510)))
        destination.name = "Saharanpur U.P. INDIA"
        MKMapItem.openMaps(with: [source, destination], launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving])
}

If you want any other tutorial reading MapKit then make your comment.

Good Luck 🙂 Happy Coding


Tags
About The Author
Pushpendra Kumar I am passionate about mobile application development and professional developer at Colour Moon Technologies Pvt Ltd (www.thecolourmoon.com). This website I have made so that I can meet with new challenges and can share here.
Leave Comment
  1. 1

    google

    Hi there would you mind letting me know which webhost you’re
    using? I’ve loaded your blog in 3 completely different browsers and I
    must say this blog loads a lot quicker then most.
    Can you recommend a good hosting provider at a reasonable price?
    Cheers, I appreciate it!

    Reply
  2. 1

    blog3006

    I am sure this paragraph has touched all the internet users,
    its really really fastidious piece of writing on building up new webpage.

    Reply

Leave a reply

Your email address will not be published. Required fields are marked *