作者:图片哪天能显示 | 来源:互联网 | 2023-06-27 15:24
更新,,,今天,学习了view中的MapKit,实现了基本的地图工具调用。Functions功能pickerView实现滚动选择地点每一个pickerView中的item有一个对应
更新,,,
今天,学习了view中的MapKit,实现了基本的地图工具调用。
Functions 功能
Implements 具体实现
storyBoard
- 按照之前所学,我们知道,要在右下角选择界面组件,这里我们搜索map.
- 选择Map Kit View,拖入主界面
- 找到SegmentedControl拖入界面,两个值分别改为Standard 和 Satellite
- 找到pickerView 拖入界面
完成图如下:
ViewController
- 首先对pickerView初始化,将pickerView 中的每个元素设为城市名
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return cityList.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return cityList[row].name
} func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let selectedCity = cityList[row]
let lat = selectedCity.lat
let lOng= selectedCity.long
let span = MKCoordinateSpanMake(0.075, 0.075)
let region = MKCoordinateRegion(center: CLLocationCoordinate2DMake(lat, long), span: span)
myMap.setRegion(region, animated: true)
}
- 创建城市类,这里使用swift里的struct
import Foundation
struct city {
var name: String
var lat: Double
var long: Double
- 然后,我们需要见一个链表来存储城市, 这里新建4个;
- 新建城市对象以及城市的属性,插入链表
var cityList = [city]() func loadCity() {
let shanghai = city(name: "Shanghai", lat: 31.2304, long: 121.4737)
let LA = city(name: "Los Angeles", lat: 34.0522, long: -118.2437)
let NY = city(name: "New York", lat: 40.7128, long: -74.0059)
let Beijing = city(name: "Beijing", lat: 39.9042, long: 116.4074)
// append cities into list
cityList.append(shanghai)
cityList.append(LA)
cityList.append(NY)
cityList.append(Beijing)
}
- 再对segmentControl设定方法
@IBAction func changeMapView(_ sender: Any) {
switch mapType.selectedSegmentIndex {
case 0:
myMap.mapType = MKMapType.standard
case 1:
myMap.mapType = MKMapType.satellite
default:
myMap.mapType = MKMapType.standard
}
}
好,现在来测试
[image:835C8A90-C849-429E-BB8C-560E2B216962-15699-0000EB0EF90D9A34/Screen Shot 2017-09-17 at 19.32.11.png]
这是洛杉矶的standard地图,
这是北京的satellite地图 ^_^
这就完成了简单的功能demo,后续,还可以加更多城市,甚至连接数据库,搜索出全球的城市,并定位该地得到地图。
Reference
App school for Xcode and iOS 10 Development Free
Copyright © 2017 zhiwei xu. All rights reserved.