尝试放大到其他位置时,mkmapview会自动缩放到当前用户位置

时间:2014-07-16 01:03:29

标签: ios objective-c mkmapview mkannotation mkpointannotation

他正在拼命等待解决方案...我正在显示当前用户位置和一些自定义注释,但是,当我尝试放大到地图时,我的地图会自动重定向到当前位置,默认缩放是因为span.please检查我的代码并纠正我。(当当前位置不显示模拟器和设备时,我可以放大其他注释)

 - (void)viewDidLoad
 {
[super viewDidLoad];

self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
[self loadMultipleAnnotations];
self.map_view.delegate = self;
 }

-(void)locationManager:(CLLocationManager *)manager
 didUpdateToLocation:(CLLocation *)newLocation
      fromLocation:(CLLocation *)oldLocation
 {
CLLocationCoordinate2D coordinate;
coordinate.latitude = newLocation.coordinate.latitude;
coordinate.longitude = newLocation.coordinate.longitude;
MKCoordinateSpan span;
span.latitudeDelta = 5;
span.longitudeDelta = 5;
MKCoordinateRegion region;
region.center = coordinate;
region.span= span;
[self.map_view setRegion:region];
MKPointAnnotation *pointAnnotation = [[MKPointAnnotation alloc]init];
pointAnnotation.coordinate = coordinate;
[self.map_view setRegion:region animated:YES];
double radius = 40000.0;
MKCircle *circle1 = [MKCircle circleWithCenterCoordinate:coordinate radius:radius];
circle1.title = @"Current location Marking";
[self.map_view addOverlay:circle1];
}

 - (MKOverlayView *)mapView:(MKMapView *)map viewForOverlay:(id <MKOverlay>)overlay
 {


if ([overlay isKindOfClass:[MKCircle class]])
{
    MKCircleView *circleView = [[MKCircleView alloc] initWithOverlay:(MKCircle*)overlay];


    circleView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.0];
    circleView.strokeColor = [[UIColor redColor] colorWithAlphaComponent:0.7];
    circleView.lineWidth = 3;

    return circleView;
}
return nil;


 }

 -(void)loadMultipleAnnotations
 {

NSMutableArray *arrLatti = [NSMutableArray arrayWithObjects:@"27.175015",@"28.171391",@"29.169005",@"22.105999",@"17.811456",@"21.453069",@"22.593726",@"38.444985",@"35.603719",@"35.603719",@"36.844461",@"35.889050",@"33.651208",@"38.238180",@"36.862043",@"36.949892",@"37.142803",@"37.71859", nil];
NSMutableArray *arrLong = [NSMutableArray arrayWithObjects:@"78.042155",@"79.037090",@"80.043206",@"75.761719",@"79.804688",@"81.562500",@"79.277344",@"-121.871338",@"-118.641357",@"-120.536499",@"-120.591431",@"-116.334229",@"-116.971436",@"-121.827393",@"-117.784424",@"-119.564209",@"-118.289795",@"-122.299805", nil];

NSMutableArray *arrTitle = [NSMutableArray arrayWithObjects:@"relative1",@"criminal1",@"criminal2",@"criminal3",@"criminal4",@"criminal5",@"criminal6",@"criminal7",@"criminal8",@"criminal9",@"relative2",@"client",@"criminal10",@"relative3",@"criminal11",@"relative4",@"criminal13",@"Offender", nil];

NSMutableArray *arrList = [[NSMutableArray alloc]init];


for (int i= 0; i< [arrLatti count]; i++)
{
    List *list = [[List alloc]init];

    list.latitt = [arrLatti objectAtIndex:i];
    list.log = [arrLong objectAtIndex:i];
    list.title = [arrTitle objectAtIndex:i];

    [arrList addObject:list];



    List *obj = [arrList objectAtIndex:i];

    CLLocationCoordinate2D locationCoordinate;

    locationCoordinate.latitude = [obj.latitt floatValue];
    locationCoordinate.longitude = [obj.log floatValue];


    MKPointAnnotation *pointAnnotation = [[MKPointAnnotation alloc]init];

    pointAnnotation.coordinate =locationCoordinate;
    pointAnnotation.title = obj.title;

    [self.map_view addAnnotation:pointAnnotation];

    MKCoordinateSpan span;
    span.latitudeDelta = 10;
    span.longitudeDelta = 10;

    MKCoordinateRegion region;
    region.center = locationCoordinate;
    region.span = span;

    [self.map_view selectAnnotation:pointAnnotation animated:YES];
    [self.map_view setRegion:region animated:YES];


}


 }
   - (MKAnnotationView *)mapView:(MKMapView *)mv viewForAnnotation:(id <MKAnnotation>)annotation
   {
   if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

static NSString *reuseId = @"reuseid";
MKAnnotationView *av = [_map_view dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (av == nil)
{
    av = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId] ;

}
else
{
    av.annotation = annotation;
}


    if ([annotation.title isEqualToString:@"criminal1"] || [annotation.title isEqualToString:@"Offender"] || [annotation.title isEqualToString:@"criminal2"]|| [annotation.title isEqualToString:@"criminal3"]|| [annotation.title isEqualToString:@"criminal4"]|| [annotation.title isEqualToString:@"criminal5"]|| [annotation.title isEqualToString:@"criminal6"]|| [annotation.title isEqualToString:@"criminal7"]|| [annotation.title isEqualToString:@"criminal8"]|| [annotation.title isEqualToString:@"criminal9"]|| [annotation.title isEqualToString:@"criminal10"]|| [annotation.title isEqualToString:@"criminal11"]|| [annotation.title isEqualToString:@"criminal12"]|| [annotation.title isEqualToString:@"criminal13"]|| [annotation.title isEqualToString:@"criminal14"]|| [annotation.title isEqualToString:@"criminal15"])
{
    av.image = [UIImage imageNamed:@"marker.png"];
    av.centerOffset = CGPointMake(0.0f, -16.5f);
}
else
{
    av.image = [UIImage imageNamed:@"markerBlue.png"];
    av.centerOffset = CGPointMake(0.0f, -24.5f);
}

av.canShowCallout = YES;

return av;


  }

2 个答案:

答案 0 :(得分:0)

问题在于-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation功能中的这一行。 iOS每次获取新位置时都会设置地图视图以查看它...

[self.map_view setRegion:region animated:YES];

...并添加另一个圆圈而不删除前一个圆圈。你为什么不设置self.map_view.showsuserLocation = true?它不会移动地图视图,但会在地图上绘制一个漂亮的蓝点和圆圈。

答案 1 :(得分:0)

通过下面的代码我来了我的问题..现在它看起来我已经解决了我的问题

  - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
  {
if ( !location )
{

    CLLocationCoordinate2D locationCoordinate;
    locationCoordinate.latitude = userLocation.coordinate.latitude;
    locationCoordinate.longitude = userLocation.coordinate.longitude;

    location = userLocation.location;

    MKCoordinateRegion region;
    region.center = mapView.userLocation.coordinate;
    region.span = MKCoordinateSpanMake(0.1, 0.1);

    region = [mapView regionThatFits:region];
    [mapView setRegion:region animated:YES];

    MKPointAnnotation *pointAnnotation = [[MKPointAnnotation alloc]init];

    pointAnnotation.coordinate =locationCoordinate;


    [self.map_View setCenterCoordinate:locationCoordinate animated:YES];

}
}