如何通过代码获取iPhone当前位置?

发布于 2024-08-24 22:37:13 字数 42 浏览 7 评论 0原文

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

How do you get the current location of iPhone through code? I need to track the location using GPS.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

你是年少的欢喜 2024-08-31 22:37:13

开发人员网站上有(一如既往的核心功能)全面的文档,您可能会找到 这个例子很有用;

要记住的要点是;

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

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

3) 返回给您的第一个位置通常是最后缓存的位置,手机将以与之前修复时相同(可能很高)的精度报告此位置。因此,您可能会得到一个声称精确到 10 米的修复,但实际上是在昨天您距离当前位置数英里之外收到的。其座右铭是不要忘记检查时间戳 - 这会告诉您实际收到位置修复的时间。 以下是如何检查时间戳的示例

希望有帮助。

There is (as always for core functionality) comprehensive documentation on the developer site and you may find this example useful;

The key points to remember are;

1) Once you start receiving location updates, they arrive asynchronously and you need to respond to them as and when they come in. Check the accuracy and timestamps to decide if you want to use the location or not.

2) Don't forget to stop receiving updates as soon as you have the location you want (this might not be the first one), otherwise you will drain the battery life.

3) The first location returned to you is often the LAST CACHED one, and the phone will report this location with the same (possibly high) accuracy it had when it got the fix before. So you might get a fix that claims to be accurate to 10 metres but it was actually received yesterday when you were miles away from your current location. The motto of this is DON'T FORGET TO CHECK THE TIMESTAMP - this tells you when the location fix was actually received. Here's an example of how to check the timestamp.

Hope that helps.

您正在寻找“CoreLocation”API。

这是教程

You're looking for the "CoreLocation" API.

Here's a tutorial

屋檐 2024-08-31 22:37:13

CLLocationManager 上的此页面有五个源代码示例

This page on CLLocationManager has five source code examples

自由如风 2024-08-31 22:37:13

下载此示例它将显示您当前的位置

download this example it will show you current location

冰葑 2024-08-31 22:37:13

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

我建议您不要直接使用 CLLocationManager,而是考虑使用 开源组件,例如 INTULocationManager,它将为您处理所有这些工作,并使请求设备当前的一个或多个离散请求变得非常简单地点。

If you just need to get the device's current location, using Core Location directly requires a good deal of code because you must handle the delay until the device's GPS has an accurate fix on the current location, which takes a number of seconds. (The CLLocationManager API appears to be built for apps that need continuous location updates, like turn-by-turn GPS navigation apps.)

Instead of using CLLocationManager directly, I suggest you take a look at using an open source component such as INTULocationManager which will handle all of this work for you and make it trivially simple to request one or more discrete requests for the device's current location.

兮颜 2024-08-31 22:37:13

完整代码如下

1 拖动图
导入框架
一行给出委托

在这段代码的最后

self.yourmapreferencename.setRegion... // 这里的所有代码

import UIKit
导入地图工具包
导入核心位置
类 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)
}

}

full code is here below

1 drag map
import framework
give delegate

in last line of this code self.yourmapreferencename.setRegion...

// all code here

import UIKit
import MapKit
import CoreLocation
class 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)
}

}

金兰素衣 2024-08-31 22:37:13

真正的解决方案可以在这里找到。 Z5 Concepts iOS 开发代码片段< /a>

它只需要一点点编码。

- (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]];
}

The real solution can be found here. Z5 Concepts iOS Development Code Snippet

It just requires a little bit of encoding.

- (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]];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文