我已经在地图视图上编了几个注释,效果很好。但是,由于某些原因,当我在设备上单击我的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
答案 0 :(得分:0)
这将是在模拟器上运行应用程序的一种表现形式(因为用户的位置不会像在设备上那样改变)。你试过在设备上运行吗?很可能(除非你还有其他事情发生),你会得到很多didUpdateUserLocation
次点击,因此你不会遇到与模拟器上相同的用户体验问题。
如果失败,您还可以viewDidLoad
设置地图的region
。也许使用Core Location的CLLocationManager
。
我怀疑你没有在设备上运行它,因为当我解释didUpdateUserLocation
时,每次用户的位置发生变化时,这都会添加另一个注释。这在设备上尤其成问题,因为随着GPS的升温,您最终会得到一系列的位置变化,因为它变得越来越准确(通常会减少horizontalAccuracy
。
我认为你真的不想在你的地图上看到十几个“你在这里”的注释。您可能希望确保删除任何先前的位置(或者仅依靠在IB中打开“显示用户位置”时设置的MKMapView
默认用户注释或设置showsUserLocation
属性)。
另请注意,您可以将userTrackingMode
设置为MKUserTrackingModeFollow
,并自动为您设置区域(假设(a)用户已授予您权限;以及(b)用户已足够小区/ WiFi / GPS信号)。