Thursday, 25 June 2015

Map View

ViewController.h

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

@interface MapViewController : UIViewController<CLLocationManagerDelegate,MKMapViewDelegate,MKAnnotation>
{
    IBOutlet MKMapView *MpData;
    CLLocationManager *locationManager;
}
@end


ViewController.m

#import "MapViewController.h"

@interface MapViewController ()
{
    MKPinAnnotationView *pinView;
    CLLocation *currentLocation ;
    MKPointAnnotation *myAnnotation;
    UISegmentedControl *segmentedControl;
}
@end

@implementation MapViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];
   
    NSArray *itemArray = [NSArray arrayWithObjects: @"Red", @"Green", @"Purple", nil];
    segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
    segmentedControl.frame = CGRectMake(35, 70, 250, 35);
    segmentedControl.segmentedControlStyle =UISegmentedControlStylePlain;
    [segmentedControl addTarget:self action:@selector(MySegmentControlAction:) forControlEvents: UIControlEventValueChanged];
    segmentedControl.selectedSegmentIndex = 0;
    [self.view addSubview:segmentedControl];
  
   // [self.view addSubview:scroll];
     locationManager = [[CLLocationManager alloc] init];
    [self CurrentLocation];
  
    myAnnotation = [[MKPointAnnotation alloc] init];
    myAnnotation.coordinate = CLLocationCoordinate2DMake(currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
   
    myAnnotation.title = @"Hi.Pin Demo";
    myAnnotation.subtitle = @"Change Pin Color ";
    myAnnotation.coordinate=MpData.centerCoordinate;
    [MpData addAnnotation:myAnnotation];
   
   
    // Do any additional setup after loading the view.
}
- (void)MySegmentControlAction:(UISegmentedControl *)segment
{
    //MKPinAnnotationView *result = [[MKPinAnnotationView alloc] initWithAnnotation:myAnnotation reuseIdentifier:Nil];
    //[myAnnotation setCoordinate:currentLocation.coordinate];
    if(segment.selectedSegmentIndex == 0)
    {
        //result.backgroundColor=[UIColor redColor];
        pinView.pinColor=MKPinAnnotationColorRed;

    }
    else if(segment.selectedSegmentIndex==1)
    {
        //result.backgroundColor=[UIColor greenColor];
        pinView.pinColor=MKPinAnnotationColorGreen;
    }
    else if (segment.selectedSegmentIndex==2)
    {
       // result.backgroundColor=[UIColor blueColor];
         pinView.pinColor=MKPinAnnotationColorPurple;
    }
}

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotationPoint
{
    static NSString *annotationIdentifier = @"annotationIdentifier";
   
    pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotationPoint reuseIdentifier:annotationIdentifier];
    pinView.pinColor = MKPinAnnotationColorPurple;
    return pinView;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    currentLocation = newLocation;
   
   /* if (currentLocation != nil) {
        longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
        latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
    }*/
   
}
- (void)CurrentLocation
{
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
   
    [locationManager startUpdatingLocation];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

No comments:

Post a Comment