English 中文(简体)
iOS Development - Integrating Maps
  • 时间:2024-11-03

iOS Development Swift 2 - Integrating Maps


Previous Page Next Page  

Maps have become a part of everyone’s daily pfe. They have become so useful when we travel to places or search for some place.

Integrating Maps and Locating India Gate

We will make maps in our apppcation, which will be showing us the India Gate in the center. We will learn maps by implementing in a project. So, create a single view iOS apppcation and name it whatever you want.

Adding Map Kit View

Go to the object pbrary and search for map kit view, cpck drag and bring it to your view, stretch it so that it fills the complete view.

Adding

Adding Constraints

Create an outlet for mapViewKit by control + drag to view the controller.swift file. It might show an error right now, but we will handle it. On top of the file, below the import UIKIT, add import MapKit, this will remove the error.

After that, add MKMapViewDelegate after class ViewController: UIViewController. Now, the file should look pke as follows −

Adding Constraints

Now, we will create Latitude and Longitude, Delta, Span, Location and Region for our Map. Before that, we will tell you how to get latitude and longitude of a place.

Go to maps.google.com and search for some location. On the top, we will see its latitude and longitude in the URL. For example: Let us search for India Gate.

Google Maps

Setting Latitude and Longitude

After getting the latitude and longitude, we will make variables for them.

let latitude: CLLocationDegrees = 28.610 
let longitude: CLLocationDegrees = 77.230 

Setting Delta for Latitude and Longitude

After adding latitude and longitude, we will add delta for them, which is the value that can verify our latitude and longitude. They should be kept minimum for more locations that are exact.

let latDelta: CLLocationDegrees = 0.04 
let lonDelta: CLLocationDegrees = 0.04 

Setting Span, Location and Region for Map

Then we will create a Span, Location and Region for our map.

let span: MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: latDelta,
longitudeDelta: lonDelta)  
let location: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude:
latitude, longitude: longitude)  
let region: MKCoordinateRegion = MKCoordinateRegion(center: location, span: span)

Setting the Map

We will set the map with the following command.

mapView.setRegion(region, animated: true)

Our final Apppcation should look pke the screenshot shown below.

Final Apppcation Screenshot

We should take care that our location set is exactly in the center of our apppcation. This is all we will do with maps right now.

Advertisements