MTLocation 操作方法

发布于 2024-11-19 11:08:25 字数 1272 浏览 3 评论 0原文

您好,我刚刚在这里遇到了 MTLocation:https://github.com/darkseed/MTLocation

我想在我的应用程序中使用“定位我”按钮,该按钮位于导航栏中,按下时会将地图移动到当前位置。

作者建议使用这样的代码:

// Configure Location Manager
[MTLocationManager sharedInstance].locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[MTLocationManager sharedInstance].locationManager.distanceFilter = kCLDistanceFilterNone;
[MTLocationManager sharedInstance].locationManager.headingFilter = 5; // 5 Degrees

// create locate-me item
MTLocateMeBarButtonItem *locateMeItem = [[[MTLocateMeBarButtonItem alloc] initWithLocationStatus:MTLocationStatusIdle] autorelease];
// set delegate that is called when mode of Button changes
locateMeItem.delegate = [MTLocationManager sharedInstance];
// add target-action
[locateMeItem addTarget:self action:@selector(myCustomSelector:) forControlEvents:UIControlEventTouchUpInside];
// disable heading
locateMeItem.headingEnabled = NO;

// create array with ToolbarItems
NSArray *toolbarItems = [NSArray arrayWithObject:locateMeItem];
// set toolbar items
[self.toolbar setItems:toolbarItems animated:NO];

这非常适合在工具栏中显示“定位我”按钮,但是我们如何才能使此工作通过 GPS 显示当前位置?我想我们必须实现 myCustomSelector 方法,但我不知道如何实现。有什么帮助吗?

Hi I just came across MTLocation here: https://github.com/darkseed/MTLocation.

I want to use a locate me button in my app which will be located in navigation bar and when pressed will move the map to current location.

The author proposes to use the code like this:

// Configure Location Manager
[MTLocationManager sharedInstance].locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[MTLocationManager sharedInstance].locationManager.distanceFilter = kCLDistanceFilterNone;
[MTLocationManager sharedInstance].locationManager.headingFilter = 5; // 5 Degrees

// create locate-me item
MTLocateMeBarButtonItem *locateMeItem = [[[MTLocateMeBarButtonItem alloc] initWithLocationStatus:MTLocationStatusIdle] autorelease];
// set delegate that is called when mode of Button changes
locateMeItem.delegate = [MTLocationManager sharedInstance];
// add target-action
[locateMeItem addTarget:self action:@selector(myCustomSelector:) forControlEvents:UIControlEventTouchUpInside];
// disable heading
locateMeItem.headingEnabled = NO;

// create array with ToolbarItems
NSArray *toolbarItems = [NSArray arrayWithObject:locateMeItem];
// set toolbar items
[self.toolbar setItems:toolbarItems animated:NO];

which is great to show a locate me button in toolbar but how can we make this work to show current location through gps? I guess tha we have to implement myCustomSelector method but I do not know how. Any help?

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

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

发布评论

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

评论(1

心舞飞扬 2024-11-26 11:08:26

确保您已经

self.mapView.showsUserLocation = YES;

实现 myCustomSelector:,例如像这样:

- (void) myCustomSelector:(MTLocateMeBarButtonItem*) button {
    [self.mapView setCenterCoordinate:self.mapView.userLocation.location.coordinate];
}

您需要在不同的按钮模式下处理适当的功能(无论 MTLocateMeBarButton 是空闲还是其他),但这应该给您一个方向。

Make sure that you have

self.mapView.showsUserLocation = YES;

And then implement myCustomSelector:, for example like this:

- (void) myCustomSelector:(MTLocateMeBarButtonItem*) button {
    [self.mapView setCenterCoordinate:self.mapView.userLocation.location.coordinate];
}

You'll need to take care of a proper functionality at different button modes (whether MTLocateMeBarButton is Idle, or something else), but that should give you a direction.

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