Monday 22 June 2015

MapView for iOS - Mark Places using Latitude & Longitude Values

Add the Frameworks: MapKit, CoreLocation

Create a New File for MapViewController & Add the Below Code’s


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

@interface MapViewController : UIViewController <MKMapViewDelegate>

@property (weaknonatomicIBOutlet UISegmentedControl *segment_view;
@property (weaknonatomicIBOutlet MKMapView *map_view;

- (IBAction)change_type:(id)sender;
- (IBAction)Route_Map:(id)sender;

@property (nonatomic,strongNSString *lati;
@property (nonatomic,strongNSString *longi;

@end


MapViewController.m
#import "MapViewController.h"

@interface MapViewController ()
@end

@implementation MapViewController

@synthesize map_view, segment_view;

- (void)viewDidLoad
{
    [super viewDidLoad];
    map_view.delegate = self;
map_view.mapType = MKMapTypeStandard;

    [self showLocate];
}


// Zoom Out & Shows full map
-(void)zoomOut
{
    // step.1 create co-ordianate-span as follows
    MKCoordinateSpan span = {.latitudeDelta = 180, .longitudeDelta = 360};
    
    // step.2 crate region as follows
    MKCoordinateRegion region = MKCoordinateRegionMake(CLLocationCoordinate2DMake(0.0000f0.0000f), span);
    
    // step.3 zoom using region to map as follows
    [map_view setRegion:region animated:YES];
}


- (void)showLocate
{
    //Set the Location
    self.lati = @"12.0000";
    self.longi = @"76.0000";
    
    for (int i = 0; i < 3; i++)
    {
        MKLocalSearchRequest *request = [[MKLocalSearchRequest allocinit];
        NSString *str = [NSString stringWithFormat:@"%f,%f",12.000+i,76.000+i];
        NSLog(@"%@",str);
        request.naturalLanguageQuery = str;
        request.region = map_view.region;
        
        NSMutableArray *_matchingItems = [[NSMutableArray allocinit];
        
        MKLocalSearch *search =
        [[MKLocalSearch alloc]initWithRequest:request];
        
        [search startWithCompletionHandler:^(MKLocalSearchResponse
                                         *response, NSError *error) {
        if (response.mapItems.count == 0)
            NSLog(@"No Matches");
        else
            for (MKMapItem *item in response.mapItems)
            {
                [_matchingItems addObject:item];
                MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
                annotation.coordinate = item.placemark.coordinate;
                //set the Title for Annotation
                annotation.title = store_model.str_area_name;
                annotation.subtitle = store_model.str_address;
                [map_view addAnnotation:annotation];
                CLLocationCoordinate2D ll = CLLocationCoordinate2DMake([self.lati doubleValue], [self.longi doubleValue]);
                MKCoordinateSpan span = MKCoordinateSpanMake(0.010.01);
                MKCoordinateRegion region = MKCoordinateRegionMake(ll, span);
                [map_view setRegion:[map_view regionThatFits:region] animated:YES];
                [map_view setCenterCoordinate:ll animated:YES];
                [map_view selectAnnotation:annotation animated:YES];
            }
    }];
    
    [self performSelector:@selector(zoomInToMyLocation)withObject:nil afterDelay:2];
    }
}


-(void)zoomInToMyLocation
{
    MKCoordinateRegion region = { {0.00.0 }, { 0.00.0 } };
    region.center.latitude = [self.lati floatValue];
    region.center.longitude = [self.longi floatValue];
    region.span.longitudeDelta = 0.01f;
    region.span.latitudeDelta = 0.01f;
    [map_view setRegion:region animated:YES];
    
    [map_view setDelegate:self];
}


//Custom Annotations for iOS - If u want the Custom Pin add this code
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
    {
        return nil;
    }
    else
    {
        static NSString * const identifier = @"MyCustomAnnotation";
        MKAnnotationView* annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        
        if (annotationView)
        {
            annotationView.annotation = annotation;
        }
        else
        {
            annotationView = [[MKAnnotationView allocinitWithAnnotation:annotation reuseIdentifier:identifier];
        }
        // set your annotationView properties
        annotationView.image = [UIImage imageNamed:@"pin_red.png"];
        annotationView.centerOffset = CGPointMake(0, -10);
        annotationView.canShowCallout = YES;
        
        //if you add QuartzCore to your project, you can set shadows for your image
        [annotationView.layer setShadowColor:[UIColor blackColor].CGColor];
        [annotationView.layer setShadowOpacity:1.0f];
        [annotationView.layer setShadowRadius:5.0f];
        [annotationView.layer setShadowOffset:CGSizeMake(00)];
        //[annotationView setBackgroundColor:[UIColor whiteColor]];
        
        return annotationView;
    }
    return nil;
}


- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    //[mapView deselectAnnotation:view.annotation animated:YES];

//SplashScreenViewController *infoView = [[SplashScreenViewController alloc]initWithNibName:@"SplashScreenViewController" bundle:nil];
//[self.navigationController pushViewController:infoView animated:YES];
    
    if ([view.annotation.title isEqualToString@"40.724200,74.172600"])
    {
        NSLog(@"loaded");

    }
    else if ([view.annotation.title isEqualToString@"12.000000,76.000000"])
    {
        NSLog(@"loaded");
    }
}


- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    NSLog(@"%@",userLocation);
}


- (IBAction)change_type:(id)sender
{
    if(segment_view.selectedSegmentIndex == 0)
    {
        map_view.mapType = MKMapTypeStandard;
    }
    else if(segment_view.selectedSegmentIndex == 1)
    {
        map_view.mapType = MKMapTypeSatellite;
    }
    else if(segment_view.selectedSegmentIndex == 2)
    {
        map_view.mapType = MKMapTypeHybrid;
    }

}

No comments: