#import "LocationViewController.h"
#import "DisplayMap.h"
#import "RentalNote_iPhone1AppDelegate.h"
#import "AddViewController.h"
NSString *temp_locality,*locality,*temp_subAdministrativeArea,*subAdministrativeArea;
NSString *temp_countryCode,*countryCode,*postalCode,*temp_address,*temp_address1,*temp_address2;
@implementation LocationViewController
@synthesize mapappDelegate;
- (void) viewDidLoad
{
lm = [[CLLocationManager alloc] init];
lm.delegate = self;
lm.desiredAccuracy = kCLLocationAccuracyBest;
lm.distanceFilter = 1000.0f;
[lm startUpdatingLocation];
mapView.delegate = self;
/* mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
mapView.showsUserLocation=TRUE;
mapView.mapType = MKMapTypeSatellite;
mapView.delegate = self;
[self.view addSubview:mapView];
*/
}
-(IBAction) removeMap: (id) sender
{
[self dismissModalViewControllerAnimated:YES];
}
- (void)mapView:(MKMapView *)mapView1 didUpdateUserLocation:(MKUserLocation *)userLocation
{
CLLocation *whereIAm = userLocation.location;
MKReverseGeocoder *reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:whereIAm.coordinate];
reverseGeocoder.delegate = self;
[reverseGeocoder start];
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
temp_locality = placemark.locality;
locality = [temp_locality stringByAppendingString:@","];
temp_subAdministrativeArea = placemark.subAdministrativeArea;
subAdministrativeArea = [temp_subAdministrativeArea stringByAppendingString:@","];
temp_countryCode = placemark.countryCode;
countryCode = [temp_countryCode stringByAppendingString:@" "];
postalCode = placemark.postalCode;
temp_address = [locality stringByAppendingString:subAdministrativeArea];
temp_address1 = [temp_address stringByAppendingString:countryCode];
temp_address2 = [temp_address1 stringByAppendingString:postalCode];
// [self.view addSubview:tabBarViewController.view];
mapappDelegate = (RentalNote_iPhone1AppDelegate *) [[UIApplication sharedApplication] delegate];
// mapappDelegate.lblCommonAddress.text = temp_address2;
mapappDelegate.lblCommonAddress.text = @"Dipak";
NSLog(@"my ns string = %@", mapappDelegate.lblCommonAddress.text);
// txtAppAddress.text = temp_address2;
}
- (void) locationManager: (CLLocationManager *) manager
didUpdateToLocation: (CLLocation *) newLocation
fromLocation: (CLLocation *) oldLocation
{
CLLocation *whereIAm = newLocation;
MKReverseGeocoder *reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:whereIAm.coordinate];
reverseGeocoder.delegate = self;
[reverseGeocoder start];
int degrees = newLocation.coordinate.latitude;
double decimal = fabs(newLocation.coordinate.latitude - degrees);
int minutes = decimal * 60;
double seconds = decimal * 3600 - minutes * 60;
// NSString *lat = [[NSString alloc] initWithFormat:@"%g",
// newLocation.coordinate.latitude];
// latitudeTextField.text = lat;
NSString *lat = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
degrees, minutes, seconds];
degrees = newLocation.coordinate.longitude;
decimal = fabs(newLocation.coordinate.longitude - degrees);
minutes = decimal * 60;
seconds = decimal * 3600 - minutes * 60;
NSString *lng = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
degrees, minutes, seconds];
// NSString *lng = [[NSString alloc] initWithFormat:@"%g",
// newLocation.coordinate.longitude - degrees];
// longitudeTextField.text = lng;
// NSString *acc = [[NSString alloc] initWithFormat:@"%g",
// newLocation.horizontalAccuracy];
// accuracyTextField.text = acc;
// [acc release];
[lat release];
[lng release];
MKCoordinateSpan span;
span.latitudeDelta=.005;
span.longitudeDelta=.005;
MKCoordinateRegion region;
region.center = newLocation.coordinate;
region.span=span;
DisplayMap *ann = [[DisplayMap alloc] init];
ann.title = @" Kolkata";
ann.subtitle = @"Mahatma Gandhi Road";
ann.coordinate = region.center;
[mapView addAnnotation:ann];
[mapView setRegion:region animated:TRUE];
}
-(void) locationManager: (CLLocationManager *) manager
didFailWithError: (NSError *) error {
NSString *msg = [[NSString alloc]
initWithString:@"Error obtaining location"];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Error"
message:msg
delegate:nil
cancelButtonTitle: @"Done"
otherButtonTitles:nil];
[alert show];
[msg release];
[alert release];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void) dealloc
{
[mapView release];
[lm release];
[super dealloc];
}
@end