MapView crashing - ios

I've been adding MapView into my ViewController. Code as below:
MapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
MKCoordinateRegion region;
region.center.latitude = [[myStore.Location objectAtIndex:0] doubleValue];
region.center.longitude = [[myStore.Location objectAtIndex:1] doubleValue];
MKCoordinateSpan span;
span.latitudeDelta = .0015;
span.longitudeDelta = .0015;
region.span = span;
[MapView setRegion:region animated:YES];
myStore.Location is an array with coordinates. XCode just crashes the app and return the error at [MapView setRegion:region animated:YES]; which i assume that it cannot init the mapview, can anyone help?

i've resolved my problem... the latitude and longitude was being reversed. [myStore.Location objectAtIndex:0] should be the longitude and [myStore.Location objectAtIndex:1] should be the latitude... silly me.

Related

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];
}

Drop Multiple Pins on Maps, Xcode

so I have this code which drops a single Pin in google maps on my iPhone app.
Do you know how I would be able to drop multiple Pins to show the different locations of the business?
#import "NewClass.h"
#implementation ViewController
-(void)viewDidLoad {
[super viewDidLoad];
MKCoordinateRegion region = { {0.0, 0.0}, {0.0,0.0}};
region.center.latitude = 40.707184;
region.center.longitude = -73.998392;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapview setRegion:region animated:YES];
MapPin *ann = [[MapPin alloc] init];
ann.title = #"Brooklyn Bridge";
ann.subtitle = #"New York";
ann.coordinate = region.center;
[mapview addAnnotation:ann];
}
My duplication attempt
MKCoordinateRegion region = { {0.0, 0.0}, {0.0,0.0}};
region.center.latitude = 53.0838491;
region.center.longitude = -2.7998707;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapview setRegion:region animated:YES];
NewClass *ann = [[NewClass alloc] init];
ann.title = #"Making Waves Swim School";
ann.subtitle = #"Clutton, Cheshire";
ann.coordinate = region.center;
[mapview addAnnotation:ann];
MKCoordinateRegion region2 = { {0.0, 0.0}, {0.0,0.0}};
region2.center.latitude = 53.199801;
region2.center.longitude = -2.89744;
region2.span.longitudeDelta = 0.01f;
region2.span.latitudeDelta = 0.01f;
[mapview setRegion:region2 animated:YES];
NewClass *ann2 = [[NewClass alloc] init];
ann2.title = #"Chester Uni";
ann2.subtitle = #"Chester, Cheshire";
ann2.coordinate = region.center;
[mapview addAnnotation:ann2];
ann2.coordinate = region.center;
should be
ann2.coordinate = region2.center;
So that the 2 pins are not having the exact same coordinates.

How to set marker in ios MKMapView [duplicate]

This question already has answers here:
iPhone: Create MKAnnotation
(3 answers)
Closed 8 years ago.
How to set marker in MKMapView
Here is my code to set a latitude and longitude but i dont know how to set marker
please give me solution.
CLLocationCoordinate2D center;
center.latitude = latitude;
center.longitude = longitude;
CLLocationCoordinate2D location = mapView.userLocation.coordinate;
MKCoordinateRegion region;
MKCoordinateSpan span;
location.latitude = center.latitude; //37.250556;
location.longitude = center.longitude; //-96.358333;
span.latitudeDelta = 0.05;
span.longitudeDelta = 0.05;
region.span = span;
region.center = location;
[mapView setRegion:region animated:YES];
[mapView regionThatFits:region];
Useful tutorial: maybelost.com/2011/01/a-basic-mapview-and-annotation-tutorial
// Add the annotation to our map view
MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:#"Buckingham Palace" andCoordinate:location];
[self.mapView addAnnotation:newAnnotation];
[newAnnotation release];

iOS: How do I get zip code from a placemark

I am wondering how I might be able to get the placemark's zip code in iOS. Any tips or suggestions will be deeply appreciated. Here is the code I have so far, I am able to get the city.
- (void)recenterMapToPlacemark:(CLPlacemark *)placemark {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.02;
span.longitudeDelta = 0.02;
region.span = span;
region.center = placemark.location.coordinate;
NSString *city = [placemark locality];
}
Check the documentation here:
https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLPlacemark_class/Reference/Reference.html#//apple_ref/occ/instp/CLPlacemark/postalCode
It is the postalCode property you are looking for.
So in your case it is:
NSString *zipCode = placemark.postalCode;

iOS Mapkit - SetRegion error

Could you point me what is wrong with this code?
-(void) showStoreRegion:(NSInteger)idx
{
//StoreLocation* store = [self.listStore objectAtIndex:idx];
//self.coordinate = CLLocationCoordinate2DMake(store.latitude,store.longitude);
self.coordinate = CLLocationCoordinate2DMake(10.7500,106.6667);
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.001;
span.longitudeDelta = 0.001;
region.span = span;
region.center = self.coordinate;
[theMapView setRegion:region animated:TRUE];
[theMapView regionThatFits:region];
[self addAnns];
}
I got this message "terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid Region '
" when run this code/
You haven't initialised your region propertly. Try this
MKCoordinateSpan span = MKCoordinateSpanMake(0.001,0.001);
MKCoordinateRegion region = MKCoordinateRegionMake(self.coordinate, span)

Resources