LoginSignup
1
2

More than 5 years have passed since last update.

【メモ】iPhoneで位置情報を取得してアプリ画面に点グラフ(散布図)として表示する

Last updated at Posted at 2017-05-12

locationManagerで現在地取得

位置情報が変更された時に呼び出されるlocationManagerのコールバック関数「didUpdateLocations」
これが呼ばれるたびに位置情報をChartsの点グラフで描画

ソースコードの一部

ViewController.swift
import Charts
import CoreLocation
//省略

class ViewController: UIViewController, CLLocationManagerDelegate {
    let rect = CGRect(x: 16, y: 344, width: 288, height: 126)
    var chartView: ScatterChartView!
    var mySet: ScatterChartDataSet!
    var dataSets: NSMutableArray = []
    var data: ScatterChartData!
    //省略

    override func viewDidLoad() {
        chartView = ScatterChartView(frame: rect)
        view.addSubview(chartView)
        //省略
    }
    //省略

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        NSLog("%@", "didUpdateLocation \(locations[0].coordinate.longitude),\(locations[0].coordinate.latitude)")
        let location : CLLocation = locations[0]
        let latlng = location.coordinate
        mylongitude = latlng.longitude
        mylatitude = latlng.latitude
        myGPSEntry = [BarChartDataEntry(x: latlng.longitude, y: latlng.latitude)]

        mySet = ScatterChartDataSet(values: myGPSEntry, label: "Me")
        self.mySet.setColor(NSUIColor.red)

        self.dataSets.removeAllObjects()
        self.dataSets.addObjects(from: [self.mySet])

        self.data = ScatterChartData(dataSets: self.dataSets as? [IChartDataSet])

        self.chartView.data = self.data
    }
    //省略

}
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2