swift -> get latitude and longitude

The first is to obtain the original coordinates, longitude and latitude of the GPS module through the system CoreLocation.

Reference: http://www.hangge.com/blog/cache/detail_783.html

import UIKit
import CoreLocation

class MyLocation: UIViewController,CLLocationManagerDelegate{
    
    let locationMana:CLLocationManager = CLLocationManager();
    
    override func viewDidLoad() {
        super.viewDidLoad();
        // Do any additional setup after loading the view.
        //Set the location service manager proxy
        locationMana.delegate = self
        //Set the positioning progress
        locationMana.desiredAccuracy = kCLLocationAccuracyBest
 
        if (CLLocationManager.locationServicesEnabled())
        {
            //If you allow the use of location services, enable location service updates
            locationMana.startUpdatingLocation();
            print("Positioning start")
        }
    }   
      //The positioning change is executed, and the new position and the old position can be obtained
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        //Get the latest coordinates
        let currLocation:CLLocation = locations.last!
        label1.text = "经度:\(currLocation.coordinate.longitude)"
        //get latitude
        label2.text = "纬度:\(currLocation.coordinate.latitude)"
        //get altitude
        label3.text = "Altitude:\(currLocation.altitude)"
        //get horizontal precision
        label4.text = "Horizontal Accuracy: \(currLocation.horizontalAccuracy)"
        //get vertical precision
        label5.text = "Vertical Accuracy: \(currLocation.verticalAccuracy)"
        // get the direction
        label6.text = "方向:\(currLocation.course)"
        //get speed
        label7.text = "速度:\(currLocation.speed)"
    }
        
}

 in:

1. Setting of positioning accuracy
The desiredAccuracy attribute of the location service management class CLLocationManager indicates the accuracy, and there are six options as follows:

kCLLocationAccuracyBestForNavigation  : Highest accuracy, generally used for navigation
kCLLocationAccuracyBest  : Best accuracy
kCLLocationAccuracyNearestTenMeters  : Accuracy within 10m
kCLLocationAccuracyHundredMeters  : Accuracy within 100m
kCLLocationAccuracyKilometer  : Accuracy within 1000m
kCLLocationAccuracyThreeKilometers  : Accuracy within 3000m

 

The above is to obtain the original longitude and latitude of GPS, but there is a deviation from the accurate and real longitude and latitude, 3 nouns:

Earth coordinates = original GPS latitude and longitude obtained above

Mars coordinates = calculation result of China's national conditions offset

Baidu coordinates = coordinates of Baidu map

 

The following two methods convert the earth coordinates to Baidu coordinates,

One, through the external library  TQLocationConverter library file see attachment 

Start the transformation after getting the earth coordinates

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

    // Get the latest coordinates

    let currLocation:CLLocation = locations.last!

    

    var locl: CLLocationCoordinate2D = CLLocationCoordinate2DMake (currLocation. coordinate . latitude , currLocation. coordinate . longitude );

    // Earth coordinates to Mars coordinates

    locl = TQLocationConverter . transformFromWGSToGCJ (locl);

    // Mars coordinates to Baidu coordinates

    locl = TQLocationConverter . transformFromGCJToBaidu (locl);

    print ( " \ ( locl. longitude ), \ ( locl. latitude )" );

    

 

}

 

2. Provide interface through Baidu

http://api.map.baidu.com/ag/coord/convert?x=105.384517616982&y=30.8762459390228&from=0&to=4&mode=1

Where X, Y are the latitude and longitude,  from and to represent the coordinate system, 0 represents the earth coordinate, 2 represents the Mars coordinate, and 4 represents the Baidu coordinate

The result is a base64 encode string

[{"error":0,"x":"MTA1LjM5NDcyMjc0MjM2","y":"MzAuODgwMDEyNjQwMzg3"}]

 

Then decode it yourself

 

There are two ways, Baidu's is still a little bit more accurate

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326655750&siteId=291194637