I'm trying to set approx a 10 mile range around the current location of the user when my mapView loads.
Here is my Objective C code in viewDidLoad:
self.mapView = [[MKMapView alloc] initWithFrame:self.view.frame];
self.mapView.delegate = self;
self.mapView.showsUserLocation = YES;
[self.view addSubview:self.mapView];
//Set initial scope of map
CLLocationDistance mapWidith = 16000;
CLLocationDistance mapHeight = 16000;
CLLocationCoordinate2D userLocation = self.mapView.userLocation.coordinate;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation, mapHeight , mapWidith);
[self.mapView setRegion: region
animated:false];
When I run it's starting with the correct zoom (I think) but randomly centered in the atlantic ocean somewhere. Not sure if I'm accessing the userLocation coordinate correctly but it seems right. Im pretty new to MKMapView so Im struggling a bit.
The userLocation property on mapView isn't populated yet. You should implement the mapView:didUpdateUserLocation: method and after that point you can reliably use that property (or use the location sent to you).
Related
I have a strange issue that started happening recently, and i cannot figure out why.
When I initialise my map with this piece of code, everything works perfectly:
_mapView = [[SKMapView alloc] initWithFrame:CGRectMake( 0.0f, 0.0f, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) )];
[self.view insertSubview:_mapView atIndex:0];
_mapView.settings.rotationEnabled = NO;
_mapView.settings.displayMode = SKMapDisplayMode2D;
[SKRoutingService sharedInstance].mapView = _mapView;
But, when I want to zoom in to a specific region on the map, things go funky and the screen is just blue.
Code:
[self.locationManager startUpdatingLocation];
_lattitude = self.locationManager.location.coordinate.latitude;
_longitude = self.locationManager.location.coordinate.longitude;
SKCoordinateRegion region;
region.center = CLLocationCoordinate2DMake(_latitude, _longitude);
region.zoomLevel = 14;
_mapView.visibleRegion = region;
[self.locationManager stopUpdatingLocation];
Screenshot:
Now, I'd like to mention that the blue screen only appears on the first launch. If i close the app and start it again, the map will be displayed properly (Both in Emulator and on the real device).
I tracked which piece of code is causing this, and it's this one:
_mapView.visibleRegion = region;
How can I fix this?
Probably the location manager has not yet received its position and gives you 0,0 which is somewhere in front of Africas coast. Ocean = blue.
You will need to setup a delegate for the location manager and wait for a positive fix on your position and then display the region in your map. In your code above you immediately request the location after starting the location manager.
Hello i am new to ios and showing an annoatation on map view using lat and long coming from server.
I have one view controller in which i am adding map and showing position of lat and long. But my problem is whenever i am adding annotation map is not focusing on annotattion insted of it everytime i want pinch map view then map is going to at annoatation i dont know why this is happening?
zoomLocation.latitude = latmpa.doubleValue;
zoomLocation.longitude = logmpa.doubleValue;
annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = zoomLocation;
annotationPoint.title = #"masjid....";
[mapView selectAnnotation:annotationPoint animated:YES];
[mapView addAnnotation:annotationPoint];
here is my code adding in viewwillappera
You need to center the map by use the following code.
mapView.centerCoordinate = annotationPoint.coordinate;
Milan's solution does what you need without animation.
If you want to do this with animation, you should use:
[mapView setCenterCoordinate:location animated:YES];
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 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.
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