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

屏幕截图

但是,在所有设备上,注释的位置都不正确。 它们都位于纬度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开发的新手,所以我现在很沮丧。

干杯托马斯!

  ask by MUNSpirit translate from so

本文未有回复,本站智能推荐: