0

我的应用程序需要的是,通过使用位置服务在我的地图视图上显示我的当前位置,这是我的代码

。H

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface MapDirectoryViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>


@property (weak, nonatomic) IBOutlet MKMapView *myMapView;

- (IBAction)backAction:(id)sender;
@end

.m

@interface MapDirectoryViewController ()

@end

@implementation MapDirectoryViewController
{
    MKCoordinateRegion myRegion;
    CLLocationCoordinate2D center;
    MKCoordinateSpan span;
    NSMutableArray *locations;
    CLLocationCoordinate2D location;
    Annotation *myAnnoation;
}

@synthesize myMapView;

- (void)viewDidLoad {
    [super viewDidLoad];

    myMapView.showsUserLocation = YES;
    [myMapView setShowsUserLocation:YES];

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

}


#delegation

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    //what do i need to do in here?
}

我特别需要的是让蓝点显示在地图上,并在应用程序加载时将该点作为地图的中心,

正如您在我的 viewdidload 中看到的那样,我尝试了很多方法来激活用户当前位置,但它们都不起作用......

而且我在我的实际 iphone6 中意识到另外一件事,一般来说,我的应用程序没有“位置”选项,所以我不能实际去激活它....例如,谷歌地图或 Facebook 应用程序它们都有“位置”选项在一般情况下应用配置...

有什么建议吗?任何代码示例都会非常有帮助,非常感谢。

4

2 回答 2

0

如果您设置位置权限,即

[LocationManager requestWhenInUseAuthorization]

或者

[LocationManager requestAlwaysAuthorization]

然后设置

[self.mapLocation setShowsUserLocation:YES];

并在地图注释委托中编写代码,如下所示:

- (MKAnnotationView *)mapView:(MKMapView *)mapViewIn viewForAnnotation:(id <MKAnnotation>)annotation {
    //Your stuff
     if (annotation == _mapLocation.userLocation) {
        return nil;
     }


}
于 2017-11-25T06:48:48.837 回答
-1

解决方案: 1,编辑Info.Plist的源代码,右键单击>打开为>源代码并添加以下行:

<!-- for requestAlwaysAuthorization -->
<key>NSLocationAlwaysUsageDescription</key>
<string>Explain for what are you using the user location</string>
<!-- for requestWhenInUseAuthorization -->
<key>NSLocationWhenInUseUsageDescription</key>
<string>Explain for what are you using the user location</string>

2、

- (void)viewDidLoad {
    [super viewDidLoad];


    serviceConnector = [[ServiceConnector alloc] init];
    serviceConnector.delegate = self;
    [serviceConnector retrieveDataFromWebservice:ALL_MOSQUE_ADDRESS_JSON_WS];

    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    [self.locationManager requestAlwaysAuthorization];
    [self.locationManager startUpdatingLocation];

    [myMapView setShowsUserLocation:YES];
    [myMapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];

    [myMapView setCenterCoordinate:myMapView.userLocation.location.coordinate animated:YES];
  }


-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    NSLog(@"location delegate called");
}

现在它用蓝点告诉我当前位置,并在 ios 8 中正确调用委派方法

于 2014-12-17T00:40:03.677 回答