在设备上的lat / long(0,0)处的地图上的注释,但是位于模拟器中

时间:2016-05-24 18:18:48

标签: ios annotations mapkit core-location simulator

在我的第一个iOS应用中,我有一张地图显示了多个地点的注释。它在模拟器中完美运行,如下所示:

Screenshot

但是在所有设备上,注释都没有找到正确的位置。它们都位于纬度0和经度0。

你有什么想法吗?

这是我的源代码:

MapViewController.m

//
//  MapViewController.m
//

#import "MapViewController.h"
#import "AppDelegate.h"
#import "Location.h"
#import <MapKit/MapKit.h>
#import "Reachability.h"
#import "MyAnnotation.h"


@interface MapViewController ()

@end

@implementation MapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.mapView.delegate = self;

    //User location
    self.locationManager = [[CLLocationManager alloc]init];
    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [self.locationManager requestWhenInUseAuthorization];
    }
    [self.locationManager startUpdatingLocation];


    [self loadOfflineMap];

    [self addAnnotations:[(AppDelegate *)[[UIApplication sharedApplication] delegate] locations]];

    //Set initial region
    MKCoordinateRegion region = [self.mapView regionThatFits:MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake(53.868223, 10.689060), 3000, 3000)];

    //Set initial locatoin if one is set
    if(self.initialLocationName != nil){
        for (id<MKAnnotation> annotation in [self.mapView annotations]) {
            if([[annotation title] isEqualToString:self.initialLocationName]){
                [self.mapView selectAnnotation:annotation animated:YES];
                region = [self.mapView regionThatFits:MKCoordinateRegionMakeWithDistance([annotation coordinate], 500, 500)];
            }
         }
    }
    [self.mapView setRegion:region animated:YES];

    //Layout stuff
    self.locationName.font = [UIFont fontWithName:@"CenturyGothic" size:self.locationName.font.pointSize];
    self.locationAddress.font = [UIFont fontWithName:@"CenturyGothic" size:self.locationAddress.font.pointSize];

}

-(void)addAnnotations:(NSMutableArray *)locations{
    self.locationNameToAnnotation = [[NSMutableDictionary alloc] init];
    for(Location *location in locations){

        MyAnnotation *annotation = [[MyAnnotation alloc] initWithTitle:location.name AndCoordinate:CLLocationCoordinate2DMake([location.latitude doubleValue], [location.longitude doubleValue])];
        [self.locationNameToAnnotation setObject:annotation forKey:location.name];
        [self.mapView addAnnotation:annotation];
    }
}

-(void)mapView:(MKMapView * )mapView didSelectAnnotationView:(MKAnnotationView * )view{

    for(NSString *locationName in self.locationNameToAnnotation){
        if([locationName isEqualToString:view.annotation.title]){
            Location *location = [Location getLocationFromLocations:[(AppDelegate *)[[UIApplication sharedApplication] delegate] locations] byName:locationName];
            self.locationName.text = location.name;
            self.locationAddress.text = location.address;
        }
    }
    self.locationInfoView.hidden = NO;

    [self.mapView setCenterCoordinate:view.annotation.coordinate animated:YES];
}

-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view{
    self.locationInfoView.hidden = YES;
}

-(void)loadOfflineMap{
    self.mapView.delegate = self;

    NSString *baseURL = [[[NSBundle mainBundle] bundleURL] absoluteString];
    NSString *template = [baseURL stringByAppendingString:@"{x}-{y}.jpg"];
    self.overlay = [[MKTileOverlay alloc] initWithURLTemplate:template];
    self.overlay.canReplaceMapContent = YES;
    self.overlay.minimumZ = 12;
    self.overlay.maximumZ = 19;
    [self.mapView addOverlay:self.overlay level:MKOverlayLevelAboveLabels];
}

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
    return [[MKTileOverlayRenderer alloc] initWithTileOverlay:overlay];
}

- (IBAction)mapTypeChanged:(UISegmentedControl *)sender {
    if(sender.selectedSegmentIndex == 1){
        if([self checkIntetnetConnection] == YES){
            [self.mapView removeOverlay:self.overlay];
        }else{
            sender.selectedSegmentIndex = 0;
        }

    }else if(sender.selectedSegmentIndex == 0){
        [self.mapView addOverlay:self.overlay level:MKOverlayLevelAboveLabels];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)checkIntetnetConnection{
    Reachability *networkReachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];
    if (networkStatus == NotReachable) {
        //NSLog(@"There IS NO internet connection");
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Internet" message:@"Sorry, please turn on your internet to access the online map" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        return NO;
    } else {
        //NSLog(@"There IS internet connection");
        return YES;
    }
}

@end

MyAnnotation.m

//
//  MyAnnotation.m
// 

#import "MyAnnotation.h"

@implementation MyAnnotation
@synthesize coordinate=_coordinate;
@synthesize title=_title;

-(id) initWithTitle:(NSString *) title AndCoordinate:(CLLocationCoordinate2D)coordinate

{

    self = [super init];

    _title = title;

    _coordinate = coordinate;

    return self;

}

@end

编辑1

位置数组从JSON解析,工作正常:

-(void)createLocations:(NSDictionary *)jsonLocations{
    self.locations = [[NSMutableArray alloc] init];
    for(NSDictionary *jsonLocation in jsonLocations){
        Location *location = [[Location alloc] init];
        [location setName:jsonLocation[@"name"]];
        [location setId:jsonLocation[@"id"]];
        [location setLatitude:[self getNumberFromString:jsonLocation[@"latitude"]]];
        [location setLongitude:[self getNumberFromString:jsonLocation[@"longitude"]]];
        [location setZoomlevel:jsonLocation[@"zoomlevel"]];
        [location setAddress:jsonLocation[@"address"]];
        [self.locations addObject:location];
    }
}

我非常感谢所有的想法。可能这是一个简单的问题,但由于我是iOS开发的新手,我现在非常沮丧。

干杯托马斯!

0 个答案:

没有答案