联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> Java编程Java编程

日期:2019-05-01 09:53

Assignment 2 - Getting the User’s Actual Location

For this assignment, I’ve suggested that you order the artworks in your table, by distance from the

user - an activity that is standard for an App that displays location based items. But how do you

determine the user’s location (and how, in particular, do we do it in the simulator)?

In your App, make sure that your table and map are installed on your storyboard for your main

view. Make sure that you control drag from the table to the View Controller yellow icon, and make a

datasource and delegate. Do the same from the map (as a delegate).

You’ll also need to control drag from the table and from the map to your view controller source and

make an outlet for each (myTable and myMap).

Apps can’t just know where the user is located (it’s a potential intrusion on the user’s privacy), and

so we have to request that the user allows iOS to tell us.

Select the info.plist from the list of files on the left hand side. You will need to add two additional

items to it. Hover over the up/down arrows by the final item, click the plus and then select “Privacy

- Location When in Use Usage Description” and repeat this with “Privacy - Location Always Usage

Description”. Add a meaningful string describing why you need each of these.

Now we need to update the ViewController to add protocols for the various types of delegation etc.

that we will be adopting. Update your ViewController.swift file to show the following (you can find

the text further in this document):

Xcode will alert you to the fact that you need to provide some methods for these - let it create the

stubs for you. You’ll need to supply the code (see a little further in this document).

We want to get CoreLocation to do stuff for us, so we add CoreLocation to the import list. We also

import the Ashton Building location document (see the “Set up a custom location” link on the

COMP228 web page).

We need to setup CoreLocation so that it will start sending us location messages when the user

moves. We have to write some code to create a locationManager instance, and then set it up with

some reasonable default values. We then have to request the user let us get their location, and if

they confirm it, we will get location messages passed to us (calling a method that we have to

supply). Here’s my full code from ViewController.swift.

//

// ViewController.swift

// UsersLocation

//

// Created by Phil Jimmieson on 10/12/2018.

// Copyright ? 2018 Phil Jimmieson. All rights reserved.

//

import UIKit

import MapKit

import CoreLocation

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, MKMapViewDelegate,

CLLocationManagerDelegate {


var locationManager = CLLocationManager() //create an instance to manage our the user’s location.


@IBOutlet weak var myTable: UITableView!


@IBOutlet weak var myMap: MKMapView!


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return 1

}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "myCell")

cell.textLabel?.text = "testing"

cell.detailTextLabel?.text = "more testing"

return cell

}


override func viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.

locationManager.delegate = self as CLLocationManagerDelegate //we want messages about location

locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation

locationManager.requestWhenInUseAuthorization() //ask the user for permission to get their location

locationManager.startUpdatingLocation() //and start receiving those messages (if we’re allowed to)


}


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

let locationOfUser = locations[0] //get the first location (ignore any others)

let latitude = locationOfUser.coordinate.latitude

let longitude = locationOfUser.coordinate.longitude

let latDelta: CLLocationDegrees = 0.002

let lonDelta: CLLocationDegrees = 0.002

let span = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lonDelta)

let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)

let region = MKCoordinateRegion(center: location, span: span)

self.myMap.setRegion(region, animated: true)

}

}

Don’t forget to set the Map properties correctly:

Finally, you should alt-click on the name of the simulator you’re about to run this on (as you did in lab 8, to

choose the language) - this will open a panel where you can configure various run time options. Select the

Ashton building as the default location.

You should now be able to run this App in the simulator and get prompted to permit it to know your location:

If you click allow, the map should zoom in to the Ashton building.


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp