Places On Map using Json
1) First of all Set Storyboard Like This :
- In First View Only Navigation Controller is here.
- In Second View There are it is simple use For Splash Screen .
Here in secon View Controller Only Image is set.
- In Third View Controller There are One text Field and Button is Here.
- In 4th View Controoler Table View and Map View is Taken.
2) Add Two Framework For Use of MapView :
- CoreLocation.Framework
- MapKit.Framework
3) write This Coding in Different View Controller
Which is Given Below :
* First View Controller (For Splash Screen) :
Splash Screen.h :
#import <UIKit/UIKit.h>
#import "ViewController.h"
@interface SplashScreen : UIViewController
@property(strong,nonatomic)ViewController *viewobj;
@end
Splash Screen.m :
#import "SplashScreen.h"
@interface SplashScreen ()
@end
@implementation SplashScreen
@synthesize viewobj;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(aftercomplate) userInfo:nil repeats:NO];
}
-(void)aftercomplate
{
UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
viewobj = [story instantiateViewControllerWithIdentifier:@"ViewController"];
[self.navigationController pushViewController:viewobj animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
* Second View Controller (For Seach) :
ViewController.h :
#import <UIKit/UIKit.h>
#import "SecViewController.h"
@interface ViewController : UIViewController
@property(strong,nonatomic)IBOutlet UITextField *txtname;
@property(strong,nonatomic)SecViewController *secobj;
-(IBAction)next:(id)sender;
@end
ViewController.m :
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize txtname,secobj;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(IBAction)next:(id)sender
{
NSString *stringdata = txtname.text;
UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
secobj = [story instantiateViewControllerWithIdentifier:@"SecViewController"];
secobj.string = stringdata;
[self.navigationController pushViewController:secobj animated:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
* Third View Controller (For Data Show & Map) :
SecViewController.h :
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface SecViewController : UIViewController<CLLocationManagerDelegate>
@property(strong,nonatomic)IBOutlet UITableView *tblobj;
@property(strong,nonatomic)IBOutlet MKMapView *mapobj;
@property(strong,nonatomic)NSMutableArray *arrobj;
@property(strong,nonatomic)NSString *string;
@property(strong,nonatomic)CLLocationManager *manager;
-(IBAction)showMap:(id)sender;
@end
SecViewController.m :
#import "SecViewController.h"
#import "Container.h"
#import "mycell.h"
#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 320.0f
#define CELL_CONTENT_MARGIN 10.0f
@interface SecViewController ()
{
int temp;
int newrow;
}
@end
@implementation SecViewController
@synthesize arrobj,tblobj,string,mapobj,manager;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
temp=0;
arrobj = [[NSMutableArray alloc] init];
[self getdata];
}
-(void)getdata
{
NSHTTPURLResponse *responce = nil;
NSString *urlstring = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@",string];
NSURL *urldata = [NSURL URLWithString:[urlstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *urlrequest = [[NSURLRequest alloc]initWithURL:urldata];
NSData *mydata = [NSURLConnection sendSynchronousRequest:urlrequest returningResponse:&responce error:nil];
NSMutableDictionary *maindic = [NSJSONSerialization JSONObjectWithData:mydata options:NSJSONReadingMutableContainers error:nil];
NSMutableArray *resultarr = [maindic objectForKey:@"results"];
for (int i = 0; i < resultarr.count; i++)
{
Container *contain = [[Container alloc] init];
NSMutableDictionary *subdic = [resultarr objectAtIndex:i];
NSString *stradd = [subdic objectForKey:@"formatted_address"];
contain.stradd = stradd;
NSMutableDictionary *geodic =[subdic objectForKey:@"geometry"];
NSMutableDictionary *boundsdic = [geodic objectForKey:@"bounds"];
NSMutableDictionary *northdic = [boundsdic objectForKey:@"northeast"];
NSString *latstr = [northdic objectForKey:@"lat"];
contain.strlat = latstr;
NSString *lngstr = [northdic objectForKey:@"lng"];
contain.strlng = lngstr ;
[arrobj addObject:contain];
}
[tblobj reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int rowDesplay=0;
if(arrobj.count>0)
{
rowDesplay=3;
}
return rowDesplay;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Container *data = [arrobj objectAtIndex:0];
NSString *strLat=[NSString stringWithFormat:@"%@",data.strlat];
NSString *strLong=[NSString stringWithFormat:@"%@",data.strlng];
mycell *cell = [tblobj dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
if(temp==0)
{
cell.lblname.text = data.stradd;
NSLog(@"%@",cell.lblname.text);
}
else if(temp==1)
{
cell.lblname.text =strLat;
NSLog(@"%@",cell.lblname.text);
}
else if(temp==2)
{
cell.lblname.text =strLong;
NSLog(@"%@",cell.lblname.text);
}
temp++;
[cell.lblname setLineBreakMode:NSLineBreakByCharWrapping];
[cell.lblname setMinimumFontSize:FONT_SIZE];
[cell.lblname setNumberOfLines:0];
[cell.lblname setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[cell.lblname setTag:1];
[[cell.lblname layer] setBorderWidth:2.0f];
NSString *text = data.stradd ;
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
if (!cell.lblname)
cell.lblname = (UILabel*)[cell viewWithTag:1];
// [cell.lblname setText:text];
[cell.lblname setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
Container *obj=[arrobj objectAtIndex:0];
NSString *text = obj.stradd ;
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN * 2);
}
-(IBAction)showMap:(id)sender
{
Container *shrobj=[arrobj objectAtIndex:0];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake([shrobj.strlat floatValue],[shrobj.strlng floatValue]) , 10000, 10000);
[mapobj setRegion:region animated:YES];
MKPointAnnotation *annotetion = [[MKPointAnnotation alloc]init ];
annotetion.coordinate = CLLocationCoordinate2DMake([shrobj.strlat floatValue], [shrobj.strlng floatValue]);
annotetion.title= shrobj.stradd ;
[mapobj addAnnotation:annotetion];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
* Custom Cell (Data on Cell) :
mycell.h :
#import <UIKit/UIKit.h>
@interface mycell : UITableViewCell
@property(strong,nonatomic)IBOutlet UILabel *lblname;
@end
mycell.m :
#import "mycell.h"
@implementation mycell
@synthesize lblname;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
* NSObject Class (For Data Store) :
container.h :
#import <Foundation/Foundation.h>
@interface Container : NSObject
@property(strong,nonatomic)NSString *stradd;
@property(strong,nonatomic)NSString *strlat;
@property(strong,nonatomic)NSString *strlng;
@end
container.m :
#import "Container.h"
@implementation Container
@synthesize stradd,strlat,strlng;
@end

No comments:
Post a Comment