0

He is desperately waiting for the solution... I am displaying the current user location and some custom annotations, but, when I am trying to zoom in into the map my map automatically redirecting to the current location with default zoom that is because of the span.please check my code and correct me.(i can able to zoom in other annotations when current location not displaying both simulator and device )

 - (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 Answers2

0

The trouble is this line in your -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation function. Every time iOS gets a new location it will set the map view to look at it...

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

…and add another circle without removing the previous one. Why don't you just set self.map_view.showsuserLocation = true? It won't move the map view, but it will draw a nice blue dot and a circle where ever the device is on the map.

Craig
  • 8,093
  • 8
  • 42
  • 74
0

through below code i over come my problem .. now its looks me i have solved my issue

  - (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];

}
}
  • The first time `didUpdateUserLocation` gets called it may not have gotten a proper GPS location yet, so always check that it is useable (i.e. != nil) before proceeding. And if you want to ignore the updates after you've received one (which you shouldn't because the user might be moving while using your app), you should just stop the locationManager from updating `[self.locationManager stopUpdatingLocation];` – Craig Jul 16 '14 at 19:02