简体   繁体   中英

How to set longitude and latitude using textfield

How can I set the longitude and latitude using a textfield? I have tried making my textfield a double but it just seems to crash.

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    mapview.showsUserLocation = YES;

    MKCoordinateRegion region = { {0.0, 0.0}, {0.0,0.0}};
    region.center.latitude = *(myTextField);
    region.center.longitude = *(myTextField1);
    region.span.longitudeDelta = 0.01f;
    region.span.latitudeDelta = 0.01f;
    [mapview setRegion:region animated:YES];


    MapPin *ann = [[MapPin alloc] init];
    ann.title = @"Test";
    ann.subtitle = @"test";
    ann.coordinate = region.center;
    [mapview addAnnotation:ann];  
}

How I can make this work?

MKPointAnnotation *annonation = [[MKPointAnnotation alloc]init];
CLLocationCoordinate2D mycordinate;
mycordinate.latitude = [self.latitude floatValue];
mycordinate.longitude =[self.longitude floatValue];
annonation.coordinate = mycordinate;
[self.mapview addAnnotation:annonation];

In self.latitude store your latitude value and in self.longitude store your longitude value.

Try below Code:

Just get text from textFields and set latitude,longitude float value from text.

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
mapview.showsUserLocation = YES;

MKCoordinateRegion region = { {0.0, 0.0}, {0.0,0.0}};
region.center.latitude = [self.myTextField.text floatValue];
region.center.longitude = [self.myTextField1.text floatValue];
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapview setRegion:region animated:YES];


MapPin *ann = [[MapPin alloc] init];
ann.title = @"Test";
ann.subtitle = @"test";
ann.coordinate = region.center;
[mapview addAnnotation:ann];

}

Convert the latitude,longitude from textFields text to float value since you require float values. Ex.: [textField.text floatValue]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM