Reputation: 1
I am making an app with a map that shows the location that you type in a text field, also if you move around the map it displays on the text field the address of the location that you are looking at.
When I move around the map it display the address in the text field and shows only the street, but I was wondering if it is possible to show the address as is used in Colombia, for example: Carrera 1 # 18A - 12.If you type the address like the example in the text field it works and it founds it in the map, but does not display the address in the text field as I want.
Here is the code I am using.
If somebody can help me i would really appreciate it. Thanks
.h file
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface ViewControllerMapaDestinoCasa : UIViewController < UITextFieldDelegate, MKMapViewDelegate>
@property (strong, nonatomic) CLLocation *ubicacionSeleccionada;
@property (strong, nonatomic) NSMutableDictionary *lugarDiccionario;
@property (strong, nonatomic) IBOutlet MKMapView *mapa;
@property (strong, nonatomic) IBOutlet UITextField *textFieldDireccion;
- (IBAction)botonUbicacionActual:(id)sender;
@end
.m file
#import "ViewControllerMapaDestinoCasa.h"
@interface ViewControllerMapaDestinoCasa ()
@end
@implementation ViewControllerMapaDestinoCasa
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.textFieldDireccion.delegate = self;
self.mapa.delegate = self;
self.lugarDiccionario = [[NSMutableDictionary alloc] init];
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = 4.609886;
zoomLocation.longitude= -74.08205;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 1609.344,1609.344);
[self.mapa setRegion:viewRegion animated:YES];
}
- (IBAction)botonUbicacionActual:(id)sender {
[self updatePlaceDictionary];
[self updateMaps];
}
- (void)updatePlaceDictionary {
[self.lugarDiccionario setValue:self.textFieldDireccion.text forKey:@"Street"];
[self.lugarDiccionario setValue:@"Bogotá" forKey:@"City"];
[self.lugarDiccionario setObject:@"Colombia" forKey:@"Country"];
}
- (void)updateMaps {
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressDictionary:self.lugarDiccionario completionHandler:^(NSArray *placemarks, NSError *error) {
if([placemarks count]) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
CLLocation *location = placemark.location;
CLLocationCoordinate2D coordinate = location.coordinate;
[self.mapa setCenterCoordinate:coordinate animated:YES];
} else {
NSLog(@"error");
}
}];
}
-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
self.ubicacionSeleccionada =
[[CLLocation alloc] initWithLatitude:mapView.centerCoordinate.latitude
longitude:mapView.centerCoordinate.longitude];
[self performSelector:@selector(delayedReverseGeocodeLocation)
withObject:nil
afterDelay:0.3];
}
- (void)delayedReverseGeocodeLocation {
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[self reverseGeocodeLocation];
}
- (void)reverseGeocodeLocation {
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:self.ubicacionSeleccionada completionHandler:^(NSArray *placemarks, NSError *error) {
if(placemarks.count){
NSDictionary *dictionary = [[placemarks objectAtIndex:0] addressDictionary];
[self.textFieldDireccion setText:[dictionary valueForKey:@"Street"]];
}
}];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
@end
Upvotes: 0
Views: 1214
Reputation: 837
Here is the code I use to get the full address, if I understood your question correctly.
[geocoder reverseGeocodeLocation:touchLocation completionHandler:^(NSArray *placemarks, NSError *error) {
if (!error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
// Update annotation with address
self.person.address.state = placemark.administrativeArea;
self.person.address.city = placemark.locality;
NSString *placemarkAddress;
//Accounts for the possibility of the number or street being null
if (placemark.subThoroughfare && placemark.thoroughfare){
placemarkAddress = [NSString stringWithFormat:@"%@ %@",placemark.subThoroughfare, placemark.thoroughfare];
}
else if (placemark.thoroughfare){
placemarkAddress = placemark.thoroughfare;
}
self.person.address.street1 = placemarkAddress;
self.person.address.zip = placemark.postalCode;
}
else {
//Error
}
}];
Upvotes: 1