ios获取当前位置并使用位置服务显示蓝点

时间:2014-12-16 23:41:43

标签: ios objective-c location mkmapview currentlocation

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

.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应用程序他们都有'位置'一般app config下的选项...

任何建议?任何代码示例都非常有用,非常感谢。

2 个答案:

答案 0 :(得分: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;
     }


}

答案 1 :(得分:-1)

溶液: 1,右键单击&gt;编辑Info.Plist的源代码;打开&gt;源代码并添加以下行:

<!-- 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中正确调用deleagation方法