iOS Mapkit - SetRegion error - ios

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)

Related

How to center current location user avtar in ios

I need to centerlized current location user pic on MKView. But curretnly it is not in center as given in below screen shot.
-
(void)locationManager:(CLLocationManager *)manager didFailWithError: (NSError *)error
{
NSLog(#"didFailWithError: %#", error);
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"didUpdateToLocation: %#", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil)
{
NSLog(#"%#",[NSString stringWithFormat:#"%.8f", currentLocation.coordinate.longitude]);
NSLog(#"%#",[NSString stringWithFormat:#"%.8f", currentLocation.coordinate.latitude]);
// [mkMapView setCenterCoordinate:mkMapView.userLocation.location.coordinate animated:YES];
}
}
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 150, 150);
[mkMapView setRegion:[mkMapView regionThatFits:region] animated:YES];
}
- (void)mapView:(MKMapView *)aMapView didUpdateUserLocation:(MKUserLocation *)aLocation {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.005;
span.longitudeDelta = 0.005;
CLLocationCoordinate2D location;
location.latitude = aLocation .coordinate.latitude;
location.longitude = aLocation .coordinate.longitude;
region.span = span;
region.center = location;
[aMapView setRegion:region animated:YES];
}
MKCoordinateSpan span;
MKCoordinateRegion region;
span.longitudeDelta =0.001;
span.latitudeDelta =0.001;
region.span = span;
region.center.latitude = self.mapVIew.region.center.latitude;
region.center.longitude = self.mapVIew.region.center.longitude;
[self.mapVIew setRegion:region];
Will Help
Use this code,
[yourMapViewName setCenterCoordinate:yourMapViewName.userLocation.location.coordinate animated:YES];
or
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 100, 100);
[yourMapViewName setRegion:[yourMapViewName regionThatFits:region] animated:YES];
}
hope its helpful
Try this, hope will be helpfull.
MKCoordinateRegion region = { { 0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = [yourLatitude doubleValue];//locationManager.location.coordinate.latitude;
region.center.longitude = [yourLongitude doubleValue];// locationManager.location.coordinate.longitude;
region.span.latitudeDelta = 0.005f;
region.span.longitudeDelta = 0.005f;
[yourMapView setRegion:region animated:YES];
Use the Below delegate method, its working for me,
-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
//Get the east and west points on the map so you can calculate the distance (zoom level) of the current map view.
MKMapRect mRect = YourMapViewName.visibleMapRect;
MKMapPoint eastMapPoint = MKMapPointMake(MKMapRectGetMinX(mRect), MKMapRectGetMidY(mRect));
MKMapPoint westMapPoint = MKMapPointMake(MKMapRectGetMaxX(mRect), MKMapRectGetMidY(mRect));
//Set your current distance instance variable.
//Set your current center point on the map instance variable.
coordinate = YourMapViewName.centerCoordinate;
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:YES];
self.locationManager1.distanceFilter = kCLDistanceFilterNone;
self.locationManager1.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager1 startUpdatingLocation];
//View Area
MKCoordinateRegion region = { { 0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude =coordinate.latitude;
region.center.longitude=coordinate.longitude;
region.span.longitudeDelta = 0.005f;
region.span.longitudeDelta = 0.005f;
[YourMapViewName setRegion:region animated:YES];
}
hope its helpful
I got solution . Now my fb pic coming on centre of the map. When i put (-30,-30) coordinate on my profileImageView frame.
profileImageView.frame = CGRectMake(-30, -30, 54, 54);
profileImageView.layer.masksToBounds = YES;
profileImageView.layer.cornerRadius = 27;

How to Specify the Exact Region on the Map in IOS

I have create a zone on the map and save this coordinate and current zoom level in the database. When we edit this zone and open the map in the mapkit with the same coordinate and zoomlevel region on the map shown is different what we have created.
CLLocationCoordinate2D cd= CLLocationCoordinate2DMake(30.724300, 76.723166);
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(cd, 4640, 4565);
[self.mapView setRegion:viewRegion animated:YES];
Using this function I get the region width and height
-(void) getMapDelta:(CLLocationCoordinate2D) coord
{
MKCoordinateSpan span = self.mapView.region.span;
CLLocationCoordinate2D loc = self.mapView.region.center;
//get latitude in meters
CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:(self.mapView.region.center.latitude - span.latitudeDelta * 0.5) longitude:self.mapView.region.center.longitude];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:(self.mapView.region.center.latitude + span.latitudeDelta * 0.5) longitude:self.mapView.region.center.longitude];
//get longitude in meters
CLLocation *loc3 = [[CLLocation alloc] initWithLatitude:self.mapView.region.center.latitude longitude:(self.mapView.region.center.longitude - span.longitudeDelta * 0.5)];
CLLocation *loc4 = [[CLLocation alloc] initWithLatitude:self.mapView.region.center.latitude longitude:(self.mapView.region.center.longitude + span.longitudeDelta * 0.5)];
int metersLatitude = [loc1 distanceFromLocation:loc2];
int metersLongitude = [loc3 distanceFromLocation:loc4];
NSLog(#"Delta> %d / %d", metersLongitude,metersLatitude);
NSLog(#"Coor> %f / %f", coord.latitude,coord.longitude);
}

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;

MapView crashing

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.

Resources