快速更新纬度和经度

时间:2017-01-30 20:00:48

标签: swift dictionary location

我想在每次按下按钮时提取和更新纬度和经度并显示用户在初始位置的位置,更新位置非常重要因为它是附近的应用程序,这是我的代码:

var locationManager = CLLocationManager()


@IBAction func update(_ sender: Any) {
    locationManager.startUpdatingLocation()


}
override func viewDidAppear(_ animated: Bool) {

    super.viewDidAppear(animated)

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()



    let url = URL(string: "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=\(latitude),\(longitude)radius=5000&types=gas_station&key=AIzaSyDVyaidSrF37d0M6XrHvosTXK4z-rkoLDg")!

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

    let userLocation: CLLocation = locations[0]

    let latitude = userLocation.coordinate.latitude

    let longitude = userLocation.coordinate.longitude

    let latDelta: CLLocationDegrees = 0.05

    let lonDelta: CLLocationDegrees = 0.05

    let span: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)

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

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


    map.setRegion(region, animated: true)


    locationManager.stopUpdatingLocation()
}

1 个答案:

答案 0 :(得分:0)

你有一个CLLocationManager。您可以随时使用

获取设备的位置
[ec2-user@ip-10-0-2-50 ~]$ mount
proc on /proc type proc (rw,relatime)
/dev/xvda1 on / type ext4 (rw,noatime,data=ordered)
/dev/xvdh on /media/ebs_volume type ext4 (rw,relatime,data=ordered)
...                  

[ec2-user@ip-10-0-2-50 ~]$ docker version
Client:
 Version:      1.12.6
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   7392c3b/1.12.6
 Built:        Fri Jan  6 22:16:21 2017
 OS/Arch:      linux/amd64

Server:
 Version:      1.12.6
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   7392c3b/1.12.6
 Built:        Fri Jan  6 22:16:21 2017
 OS/Arch:      linux/amd64

[ec2-user@ip-10-0-2-50 ~]$ cat /proc/version
Linux version 4.4.41-36.55.amzn1.x86_64 (mockbuild@gobi-build-60008) (gcc version 4.8.3 20140911 (Red Hat 4.8.3-9) (GCC) ) #1 SMP Wed Jan 18 01:03:26 UTC 2017

[ec2-user@ip-10-0-2-50 ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
1436d3223b27        nginx               "/bin/bash"              47 seconds ago      Up 46 seconds       80/tcp, 443/tcp     happy_leakey

[ec2-user@ip-10-0-2-50 ~]$ docker inspect 1436d3223b27
[
    {
        "Id": "1436d3223b270f78654d26682c469f4d7bdd24766c60f4052cb1a3fb60c86c12",
        "Created": "2017-01-30T17:34:12.430135675Z",
        "Path": "/bin/bash",
        ...
        "Image": "sha256:cc1b614067128cd2f5cdafb258b0a4dd25760f14562bcce516c13f760c3b79c4",
        ...
        "Driver": "devicemapper",
        "MountLabel": "",
        ...
        "HostConfig": {
            "Binds": [
                "/media/ebs_volume:/tmp/foo:ro"
            ],
            "ContainerIDFile": "",
            ...
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            ...
        },
        ...
        "Mounts": [
            {
                "Source": "/media/ebs_volume",
                "Destination": "/tmp/foo",
                "Mode": "ro",
                "RW": false,
                "Propagation": "rprivate"
            }
        ],
        "Config": {
            ...
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.11.9-1~jessie"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "Image": "nginx",
            "Volumes": null,
            ...
        },
        ...
    }
]

这是一个CLLocation。我要警告的一件事是,requestAuthorization等等,需要几秒钟,并且该位置将为零,直到它完成并且CLLocationManager已经开始定位。因此,请注意展开选项,并考虑在之前的VC中创建locationManager并请求授权。一个完整的IBAction,它可以获取用户的位置,并将lat和long插入到URL中:

locationManager.location