【问题标题】:How to get the current location of iPhone through code?如何通过代码获取 iPhone 的当前位置?
【发布时间】:2011-01-28 02:13:33
【问题描述】:

如何通过代码获取 iPhone 的当前位置?我需要使用 GPS 跟踪位置。

【问题讨论】:

    标签: iphone gps location locationmanager


    【解决方案1】:

    开发者网站上有(一如既往的核心功能)综合文档,您可能会发现 this example 很有用;

    要记住的关键点是;

    1) 一旦您开始接收位置更新,它们就会异步到达,您需要在它们到来时做出响应。检查准确性和时间戳以确定您是否要使用该位置。

    2) 不要忘记在获得所需位置后立即停止接收更新(这可能不是第一个),否则会耗尽电池寿命。

    3) 返回给您的第一个位置通常是最后一个缓存的位置,手机会以与之前修复时相同(可能很高)的精度报告此位置。因此,您可能会得到一个声称精确到 10 米的修复程序,但实际上是昨天在您距离当前位置数英里时收到的。座右铭是不要忘记检查时间戳 - 这会告诉您实际收到位置修复的时间。 Here's an example of how to check the timestamp.

    希望对您有所帮助。

    【讨论】:

    • 谢谢罗杰,在你的例子尝试后会抓住你
    • 开发人员文档链接已损坏,因此我修复了它。我必须说,关于缓存位置的第三点是许多开发人员忽略的一点,这绝对是一个好点!为此+1。我正在更新@Roger 的帖子,以包括如何检查位置时间戳。
    【解决方案2】:

    您正在寻找“CoreLocation”API。

    这里是a tutorial

    【讨论】:

      【解决方案3】:

      This page on CLLocationManager有五个源代码示例

      【讨论】:

        【解决方案4】:

        下载这个example它会显示你当前的位置

        【讨论】:

          【解决方案5】:

          如果您只需要获取设备的当前位置,则直接使用 Core Location 需要大量代码,因为您必须处理延迟,直到设备的 GPS 准确定位到当前位置,这需要几秒钟的时间。 (CLLocationManager API 似乎是为需要持续更新位置的应用程序构建的,例如逐向 GPS 导航应用程序。)

          我建议您不要直接使用 CLLocationManager,而是使用 open source component such as INTULocationManager,它将为您处理所有这些工作,并使请求设备当前位置的一个或多个离散请求变得非常简单。

          【讨论】:

            【解决方案6】:

            完整代码如下

            1 拖动地图 导入框架 给委托

            在这段代码的最后一行 self.yourmapreferencename.setRegion...

            //这里的所有代码

            导入 UIKit 导入 MapKit 导入核心位置 类 ViewController: UIViewController, CLLocationManagerDelegate {

            @IBOutlet weak var mapView: MKMapView!
            var locationManager : CLLocationManager!
            
            override func viewDidLoad() {
                super.viewDidLoad()
            
                if CLLocationManager.locationServicesEnabled()
                {
                    locationManager = CLLocationManager()
                    locationManager.delegate = self
                    locationManager.desiredAccuracy = kCLLocationAccuracyBest
                    locationManager.requestAlwaysAuthorization()
                    locationManager.startUpdatingLocation()
            
                }
            

            }

            func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
            {
            
                let location = locations.last! as CLLocation
                let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
                let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
            
                self.mapView.setRegion(region, animated: true)
            }
            

            }

            【讨论】:

              【解决方案7】:

              真正的解决方案可以在这里找到。 Z5 Concepts iOS Development Code Snippet

              它只需要一点编码。

              - (IBAction)directions1_click:(id)sender
              {
                  NSString* address = @"118 Your Address., City, State, ZIPCODE";
                  NSString* currentLocation = @"Current Location";
                  NSString* url = [NSStringstringWithFormat: @"http://maps.google.com/maps?saddr=%@&daddr=%@",[currentLocation stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
                  UIApplication *app = [UIApplicationsharedApplication];
                  [app openURL: [NSURL URLWithString: url]];
              }
              

              【讨论】:

                猜你喜欢
                • 2012-03-10
                • 1970-01-01
                • 2011-11-21
                • 2011-04-05
                • 1970-01-01
                • 2013-10-19
                • 1970-01-01
                相关资源
                最近更新 更多