I wanna zoom into the user current location when the app starts in the MapKit.
This is my code (in the viewDidLoad function):
Locate=[[CLLocationManager alloc]init];
Locate.delegate=self;
Locate.desiredAccuracy=kCLLocationAccuracyBestForNavigation;
Locate.distanceFilter=kCLDistanceFilterNone;
[Locate startUpdatingLocation];
[super viewDidLoad];
//Region
MKCoordinateRegion myRegion;
//Center
CLLocationCoordinate2D center;
center.latitude=Locate.location.coordinate.latitude;
center.longitude=Locate.location.coordinate.longitude;
//Span
MKCoordinateSpan span;
span.latitudeDelta=0.3f;
span.longitudeDelta=0.3f;
//Set Region
myRegion.center=center;
myRegion.span=span;
[_MyMap setRegion:myRegion animated:YES];
Also I implemented the didUpdateLocation function with the same code as previous.
The problem is that when the user location is changing (when the user is moving) the screen makes zoom at him but I can't move the screen, if I try to move it return to the user location immediately, so I can't see the whole map.
For Zooming the map you have to change the region values means center and span.once see this one Mapview Zoom
in my case i have used this one for moving the map when i click on particular button.
MKCoordinateSpan span = MKCoordinateSpanMake(30.5982f,0.0001f);
CLLocationCoordinate2D coordinate = {36, 90};
MKCoordinateRegion region = {coordinate, span};
MKCoordinateRegion regionThatFits = [self.mapView regionThatFits:region];
[self.mapView setRegion:regionThatFits animated:YES];
I solved this issue by adding touchesMoved function and I stopped updating the Location in this function.
Solved.
Related
I need to create ViewController with MKMapView. When it opens, there are should appear current location and slowly zooming out for 10 seconds (or less, it doesn't matter actually). And when animation completed application should perform segue to the next ViewController. Any ideas?
There is no "direct" method for setting a zoom level. You can, however, simulate this by setting the distance, which causes the image to resize.
CLLocationCoordinate2D noLocation;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(noLocation, 500, 500);
MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:viewRegion];
[self.mapView setRegion:adjustedRegion animated:YES];
self.mapView.showsUserLocation = YES;
You can either loop this, or simply set it twice, first to a BIG area and then the zoomed in area, and let the MapView's default animation do its thing.
I have a getLocation button in my app that gets the user's current location, the button calls the following method:
-(void)setOriginalRegion {
float spanX = 0.217;
float spanY = 0.217;
MKCoordinateRegion region;
region.center.latitude = self.mapView.userLocation.coordinate.latitude;
region.center.longitude = self.mapView.userLocation.coordinate.longitude;
region.span.latitudeDelta = spanX;
region.span.longitudeDelta = spanY;
[self.mapView setRegion:region animated:YES];
}
That works just fine, and when tapped I get my location and a 15mile radius (as set by the delta's)...
To make sure my map loads up in the current user's location, I have added the following in my viewWillAppear method:
[self setOriginalRegion];
But that is starting the map somewhere in the middle of the ocean (approx 100 miles off the coast of Ghana.... This of course is not default, because by default it loads up in with North America. What am I doing wrong here?
As pointed out by #AndreMunis, viewWillAppear is too soon to know user's location, moved my method call to viewDidAppear and code works like a charm!
Please check the below Image. How to Avoid this type of zoom level in iPhone
Now getting Result:
Expected Result:
I don't think there is a way of telling if satellite images would be available at zoom level for given region. Better approach is to set your MKMapView to MKMapTypeStandard and give the user the option of switching to MKMapTypeSatellite in this case you will always have a map details to display on initial presentation of your MKMapView object
You can set the zoom by using MKCoordinateRegion and setting its span latitude & longitude delta as:
MKCoordinateRegion region;
region.center.latitude = {desired lat};
region.center.longitude = {desired lng};
region.span.latitudeDelta = 1;
region.span.longitudeDelta = 1;
region = [mapView regionThatFits:region];
[mapView setRegion:region animated:TRUE];
Vary the span.longitudeDelta and span.latitudeDelta to get the required zoom.
I am using following codes
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake(annView.annotation.coordinate.latitude, annView.annotation.coordinate.longitude - .04), Some KM, Some KM);
MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:viewRegion];
[self.mapView setRegion:adjustedRegion animated:YES];
Otherway around is
MKCoordinateRegionMake(CLLocationCoordinate2DMake(annView.annotation.coordinate.latitude, annView.annotation.coordinate.longitude - .04), ADD SPAN HERE)
Both of these makes the map zoom. How is it possible that I change the Region without any zoom.
Get the current region and just change the center point.
// get current region
MKCoordinateRegion region = self.mapView.region;
// Update the center
region.center = CLLocationCoordinate2DMake(annView.annotation.coordinate.latitude, annView.annotation.coordinate.longitude - .04);
// apply the new region
self.mapView.region = region;
If you have a coordinate to set, use the MKMapView -setCenterCoordinate:animated: method. Animating the position change gives the user a clue about what's happened.
CLLocationCoordinate2D coordinate = annView.annotation.coordinate;
[myMapView setCenterCoordinate:coordinate animated:YES];
Also, no need to make a new coordinate, just use the one already in the annotation view.
I have a function in my view controller with a mapkit that is called when the location changes. I would like set to the map view so that is is centered around the current location and moves with it as it updates.
It works in a sense i.e it tracks along, however it is always zoomed in really far. If I zoom out it snaps back to where it was when it calls the update again after getting a new location.
in from params CLLocation *loc
bool first = TRUE;
if(first){
first=FALSE; // bit of a bodge...if i set this to false it will track the update possition but not at the desired "zoom" its look far to close.
CLLocation *loc = [[CLLocation alloc] initWithLatitude:54.976619 longitude:-1.613118];//newcastle city center.
CLLocationDegrees spanLat = 1000;
CLLocationDegrees spanLng = 1000;
userLocation = MKCoordinateRegionMakeWithDistance(loc.coordinate, spanLat, spanLng);
[mapview setRegion:userLocation animated:TRUE];
}else {
CLLocationDegrees spanLat = [mapview region].span.latitudeDelta;// keep the originals? DOESN'T WORK!!
CLLocationDegrees spanLng = [mapview region].span.longitudeDelta;//
userLocation = MKCoordinateRegionMakeWithDistance(loc.coordinate, spanLat, spanLng);
[mapview setRegion:userLocation animated:TRUE];
}
Just keep setting the center coordinate and don't set the region.
By the way, you do know that you don't need any of that code, right? You can just tell the mapview to track the device's current location. See my book for an example:
http://www.apeth.com/iOSBook/ch34.html#_map_kit_and_current_location