Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Mobile-Applications how to do map in iphone?-RV
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Ravi Vora
how to do map in iphone?-RV
1 Answer(s)      3 years ago
Posted in : MobileApplications


In Appdelegate.h

@class MapKitDisplayViewController;

@interface MapKitDisplayAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    MapKitDisplayViewController *viewController;
    UINavigationController *navigationController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet MapKitDisplayViewController *viewController;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

@end

In Appdelegate.m

@synthesize window;
@synthesize viewController;
//@synthesize navigationController;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

    // Add the view controller's view to the window and display.
    //[window addSubview:viewController.view];
    [self.window addSubview:navigationController.view];
    [window makeKeyAndVisible];

    return YES;
}

In mapkitdisplayviewcontroller.h

#import <MapKit/MapKit.h>

@class DisplayMap;

@interface MapKitDisplayViewController : UIViewController <MKMapViewDelegate> {

    int g;

    IBOutlet MKMapView *mapView;

    NSMutableArray *arr;

    NSMutableArray *arr_temp;

    NSMutableArray *final_arr;

    NSMutableArray *final_arr_add;
}

-(void)ButtonPressed:(id)sender;

@property (nonatomic, retain) IBOutlet MKMapView *mapView;

@end

In mapkitdisplayviewcontroller.m

#import "DisplayMap.h"

@implementation MapKitDisplayViewController

@synthesize mapView;

- (void)viewDidLoad {
    [super viewDidLoad];

    g=0;

    arr = [[NSMutableArray alloc]init];

    arr_temp = [[NSMutableArray alloc]init];

    final_arr = [[NSMutableArray alloc]init];

    final_arr_add = [[NSMutableArray alloc]init];

    NSMutableDictionary *dic1 = [[NSMutableDictionary alloc]init];
    [dic1 setObject:@"Germany" forKey:@"city"];
    [dic1 setObject:@"51.165691" forKey:@"lat"];
    [dic1 setObject:@"10.451526" forKey:@"lon"];

    NSMutableDictionary *dic2 = [[NSMutableDictionary alloc]init];
    [dic2 setObject:@"Austria" forKey:@"city"];
    [dic2 setObject:@"47.516231" forKey:@"lat"];
    [dic2 setObject:@"14.550072" forKey:@"lon"];

    NSMutableDictionary *dic3 = [[NSMutableDictionary alloc]init];
    [dic3 setObject:@"Hungary" forKey:@"city"];
    [dic3 setObject:@"47.162494" forKey:@"lat"];
    [dic3 setObject:@"19.503304" forKey:@"lon"];

    NSMutableDictionary *dic4 = [[NSMutableDictionary alloc]init];
    [dic4 setObject:@"Romania" forKey:@"city"];
    [dic4 setObject:@"45.943161" forKey:@"lat"];
    [dic4 setObject:@"24.96676" forKey:@"lon"];

    [final_arr addObject:dic1];
    [final_arr addObject:dic2];
    [final_arr addObject:dic3];
    [final_arr addObject:dic4];

    [arr addObject:@"Germany"];
    [arr addObject:@"Austria"];

    [arr addObject:@"Hungary"];
    [arr addObject:@"Romania"];
    [mapView setMapType:MKMapTypeStandard];
    [mapView setZoomEnabled:YES];
    [mapView setScrollEnabled:YES];
    MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } }; 
    region.center.latitude = 51.165691 ;
    region.center.longitude = 10.451526;
    region.span.longitudeDelta = 0.01f;
    region.span.latitudeDelta = 0.01f;
    [mapView setRegion:region animated:YES]; 

    [mapView setDelegate:self];

    DisplayMap *ann = [[DisplayMap alloc] init]; 
    ann.title = @"Germany";
    ann.subtitle = @""; 
    ann.coordinate = region.center; 
    [mapView addAnnotation:ann];


    MKCoordinateRegion region1 = { {0.0, 0.0 }, { 0.0, 0.0 } }; 
    region1.center.latitude = 47.516231 ;
    region1.center.longitude = 14.550072;
    region1.span.longitudeDelta = 0.01f;
    region1.span.latitudeDelta = 0.01f;

        DisplayMap *ann1 = [[DisplayMap alloc] init]; 
    ann1.title = @"Austria";
    ann1.subtitle = @""; 
    ann1.coordinate = region1.center;
    [mapView addAnnotation:ann1];

        MKCoordinateRegion region2 = { {0.0, 0.0 }, { 0.0, 0.0 } }; 
    region2.center.latitude = 47.162494 ;
    region2.center.longitude = 19.503304;
    region2.span.longitudeDelta = 0.01f;

        DisplayMap *ann2 = [[DisplayMap alloc] init]; 
    ann2.title = @"Hungary";
    ann2.subtitle = @""; 
    ann2.coordinate = region2.center;

        MKCoordinateRegion region3 = { {0.0, 0.0 }, { 0.0, 0.0 } }; 
    region3.center.latitude = 45.943161 ;
    region3.center.longitude = 24.96676;
    region3.span.longitudeDelta = 0.01f;
    region3.span.latitudeDelta = 0.01f;
    [mapView addAnnotation:ann2];
    region2.span.latitudeDelta = 0.01f;

        DisplayMap *ann3 = [[DisplayMap alloc] init]; 
    ann3.title = @"Romania";
    ann3.subtitle = @""; 
    ann3.coordinate = region3.center;
    [mapView addAnnotation:ann3];

        CLLocationCoordinate2D mapCoords[4];

    mapCoords[0].latitude  = 51.165691;
    mapCoords[0].longitude = 10.451526;
    mapCoords[1].latitude  = 47.516231;
    mapCoords[1].longitude = 14.550072;
    mapCoords[2].latitude  = 47.162494;
    mapCoords[2].longitude = 19.503304;
    mapCoords[3].latitude  = 45.943161;
    mapCoords[3].longitude = 24.96676;

       MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:mapCoords count:4];
    [self.mapView insertOverlay:polyLine atIndex:0];}
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
 (id <MKAnnotation>)annotation {
    MKPinAnnotationView *pinView = nil; 
    if(annotation != mapView.userLocation) 
    {
        static NSString *defaultPinID = @"com.invasivecode.pin";
        pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinView == nil ) 
            pinView = [[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
        pinView.pinColor = MKPinAnnotationColorRed; 
        pinView.canShowCallout = YES;
        pinView.animatesDrop = YES;

        NSString *pintitle=[NSString stringWithFormat:@"%@",((MKPinAnnotationView*) pinView).annotation.title];



                if (![arr_temp containsObject:pintitle]) {

                    [arr_temp addObject:pintitle];

                    for(NSString*str in [final_arr valueForKey:@"city"]) {
                        if ([pintitle isEqualToString:str]) {

                            [final_arr_add addObject:[final_arr objectAtIndex:g]];

                UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeCustom];
                [infoButton setFrame:CGRectMake(pinView.bounds.size.width-35,10,25, 27)];
                [infoButton setImage:[UIImage imageNamed:@"DDOrange.png"] forState:UIControlStateNormal];
                pinView.rightCalloutAccessoryView = infoButton;
                infoButton.tag = g;
                g++;
                [infoButton addTarget:self action:@selector(ButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
                }
            }

        }



        } 
    else {
        [mapView.userLocation setTitle:@"I am here"];
    }
    return pinView;
}

-(void)ButtonPressed:(id)sender
{
    UIButton *btn = (UIButton*)sender;
    NSLog(@"%d",btn.tag);

    NSLog(@"%@",[arr objectAtIndex:btn.tag]);


    mapdetail *obj_mapDetail = [[mapdetail alloc]init];

    [obj_mapDetail setArray:[final_arr_add objectAtIndex:btn.tag]];

    [self.navigationController pushViewController:obj_mapDetail animated:YES];
}

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

    MKPolylineView *overlayView = nil;

    if (nil == overlayView) {

        overlayView = [[[MKPolylineView alloc] initWithOverlay:overlay] autorelease];

        overlayView.strokeColor = [UIColor blueColor];
        overlayView.lineWidth   = 5.0;

    }

    return overlayView;
}

In mapdetail.h

@interface mapdetail : UIViewController {

    NSMutableArray *myArr;

}

-(void)setArray:(NSMutableArray*)arr;

@end

IN mapdetail.m

- (void)viewDidLoad {
    [super viewDidLoad];

    NSLog(@"%@",myArr);
}

-(void)setArray:(NSMutableArray*)arr
{
    myArr = arr;
}

Advertisement
View Answers

January 9, 2012 at 12:23 PM


??



Related Tutorials/Questions & Answers:
Advertisements
map
Map
Map
 

 

 

DMCA.com