0

我用一些注释对我的地图视图进行了编码,效果很好。但是,由于某种原因,当我在设备上单击 MapView 时,单击离开,然后返回 MapView,由于某种原因,它不会放大用户的位置(它只是显示整个世界的地图)。有谁知道为什么?请参阅下面的代码:

MapViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface MapViewController : UIViewController  <MKMapViewDelegate> 

    @property (nonatomic, strong) IBOutlet MKMapView *mapView;
    @property (nonatomic, retain) NSMutableArray *dispensaries;
    @property (nonatomic, retain) NSMutableData *data;

@end

MapViewController.m

#import "MapViewController.h"
#import "MapViewAnnotation.h"
#import "JSONKit.h"

@implementation MapViewController
@synthesize mapView;
@synthesize dispensaries;
@synthesize data;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad {

    NSLog(@"Getting Device Locations");

    NSString *hostStr = @"http://stylerepublicmagazine.com/dispensaries.php";
    NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:hostStr]];
    NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
    NSLog(@"server output: %@", serverOutput);
    NSMutableArray *array = [[serverOutput objectFromJSONString] mutableCopy];
    dispensaries = [serverOutput objectFromJSONString];
    NSLog(@"%@", [serverOutput objectFromJSONString]);
    for (NSDictionary *dictionary in array) {
        assert([dictionary respondsToSelector:@selector(objectForKey:)]);

        CLLocationCoordinate2D coord = {[[dictionary objectForKey:@"lat"] doubleValue], [[dictionary objectForKey:@"lng"] doubleValue]};

        MapViewAnnotation *ann = [[MapViewAnnotation alloc] init];
        ann.title = [dictionary objectForKey:@"Name"];
        ann.subtitle = [dictionary objectForKey:@"Address1"];
        ann.coordinate = coord;
        [mapView addAnnotation:ann];

        [mapView setMapType:MKMapTypeStandard];
        [mapView setZoomEnabled:YES];
        [mapView setScrollEnabled:YES];

        self.mapView.delegate = self;
    }
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 2000, 2000);
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];

    MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
    point.coordinate = userLocation.coordinate;
    point.title = @"You Are Here";
    point.subtitle = @"Your current location";

    [self.mapView addAnnotation:point];
}


- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    for (MapViewAnnotation *annotation in self.mapView.annotations) {
        CLLocationCoordinate2D coord = [annotation coordinate];
        CLLocation *annotationLocation = [[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];
        annotation.distance = [newLocation distanceFromLocation:annotationLocation];
    }
}

// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;

// Do any additional setup after loading the view from its nib.

- (void)viewDidUnload
{

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
4

1 回答 1

0

这将是在模拟器上运行应用程序的一种表现(因为用户的位置不会像在设备上那样改变)。您是否尝试过在设备上运行它?很可能(除非你有其他事情发生),你会得到很多didUpdateUserLocation点击,因此你不会遇到与模拟器相同的用户体验问题。

如果失败,您还可能必须viewDidLoad设置地图的region. 也许使用核心位置的CLLocationManager.

我怀疑您没有在设备上运行它,因为当我解释 时didUpdateUserLocation,每次用户的位置更改时,这都会添加另一个注释。这在设备上尤其成问题,因为随着 GPS 升温,随着它变得越来越准确(通常随着horizontalAccuracy.

我假设您真的不想在地图上看到十几个“您在这里”注释。您可能希望确保删除任何先前的位置(或仅依赖MKMapView您在 IB 中打开“显示用户位置”或设置showsUserLocation属性时获得的默认用户注释)。

另请注意,您可以设置userTrackingModeMKUserTrackingModeFollow,系统会自动为您设置区域(假设 (a) 用户已授予您权限;并且 (b) 用户有足够的手机/WiFi/GPS 信号)。

于 2013-09-26T23:01:22.737 回答