ios 在 MapView 中显示用户位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19357941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:19:41  来源:igfitidea点击:

Show User Location in MapView

iosios7mkmapviewcllocation

提问by Christian Seiler

I'm writing an App with a mapView showing some Annotations.

我正在编写一个带有显示一些注释的 mapView 的应用程序。

Now I want to show the User's current location in the mapView. I get the current Location using this code:

现在我想在 mapView 中显示用户的当前位置。我使用此代码获取当前位置:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)location

I get the details of the current location in the Log

我在日志中获取当前位置的详细信息

2013-10-14 12:03:34.291 AppName[13200:a0b] (
"<+7.35000000,+47.32000000> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/14/13, 12:03:34 PM Central European Summer Time")

Now I don't get the location into the mapView as this blue pulsing dot.

现在我没有将位置作为这个蓝色脉冲点输入到 mapView 中。

Please give some hints.

请给一些提示。

回答by incmiko

You can enable the userlocation by this:

您可以通过以下方式启用用户定位:

mapView.showsUserLocation = YES;

if you want to center on this location:

如果你想以这个位置为中心:

[mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];

if you are using:

如果您正在使用:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{

if ([annotation isKindOfClass:[MKUserLocation class]]) {
    return nil;
}

// etc...


}

回答by saswanb

In iOS10 I could not get the blue dot for the user location to show until adding the key NSLocationWhenInUseUsageDescription to my Info.plist, with a string description of how location info would be used in the app.

在 iOS10 中,在将键 NSLocationWhenInUseUsageDescription 添加到我的 Info.plist 之前,我无法显示用户位置的蓝点,并带有一个关于如何在应用程序中使用位置信息的字符串描述。

回答by Arek

In xCode 9 you could do it with storyboard file

在 xCode 9 中,您可以使用情节提要文件

enter image description here

在此处输入图片说明

回答by Nico S.

I ran into the same because I removed my type check code for AnnotationViews because I didn't need that anymore. So I made a Swift version of @incmiko's answer.

我遇到了同样的问题,因为我删除了 AnnotationViews 的类型检查代码,因为我不再需要它了。所以我制作了@incmiko 答案的 Swift 版本。

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    guard !annotation.isKind(of: MKUserLocation.self) else { return nil }

    //create annotation view

    return MKAnnotationView()
}