I have a normal mapView, which can tell the users location, but when users location is located my map doesn't suddenly zoom in to the users location like Apples maps application.
Does anybody know how to implement this feature!
Implement the didUpdateUserLocation delegate method:
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
MKCoordinateRegion region;
region.center = userLocation.location.coordinate;
MKCoordinateSpan span = { 1.0, 1.0 };
region.span = span;
[mapView setRegion:region animated:YES];
}
Make sure your map view's delegate is set.
Related
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views {
for(MKAnnotationView *annotationView in views) {
if(annotationView.annotation == mv.userLocation) {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.002;
span.longitudeDelta=0.002;
CLLocationCoordinate2D location=mv.userLocation.coordinate;
region.span=span;
region.center=location;
[mv setRegion:region animated:TRUE];
[mv regionThatFits:region];
}
}
Hello all. I have tried searching other posts and websites. In essence I want to duplicate the 'Current location' BUTTON.
Using the above code, I was able to zoom into the users current location. This however is not flawless, sometimes the GPS updates a little late or thinks I am somewhere else, zooms to that location but then the blue dot will shift somewhere off screen to where I really am.
I was wondering is there a way I can add a 'Button', exactly like maps on the iphone. It doesn't need to gain a new zoom etc, simply move to the new updated locaiton. My main source of code would be almost replica to here.
I appreciate any help.
Create a custom annotation with the same graphic. This object must implement MKAnnotationView, and you have to return the custom view for it in the method mapView:viewForAnnotation: of the MKMapViewDelegate. If you want the exact graphic, use the project UIKit-Artwork-Extractor to get it.
Save a reference to the annotation view, subscribe to core location updates, and update the position calling something like:
- (void) animateView:(UIView *)view toPosition:(NSValue*)value
{
CGPoint newCenter = [value CGPointValue];
[UIView animateWithDuration:0.5
animations:^{
view.center = newCenter;
}
completion:^(BOOL finished){ }];
}
Then set the center of the map on the annotation:
- (void)centerMap:(CLLocation *)location {
MKCoordinateRegion region = { { 0.0f, 0.0f }, { 0.0f, 0.0f } };
region.center = location.coordinate;
region.span.longitudeDelta = 0.15f;
region.span.latitudeDelta = 0.15f;
[self.mapView setRegion:region animated:YES];
}
I have a MKMapView and in the Map View "Show User Location" is set. The question if the app should use my location, I say yes. Then I see the blue bullet and I can zoom to the current location.
I read many other posts about this, but nothing solve the problem, that the user location won't zoom in automatically.
I want to have a zoom on startup if the user allows access the location, otherwise a defined coordinate should zoom in. (after, is the use allows the location, it can be updated, but should not set the center to the user location everytime I get updates on the location).
What are the steps to implement this behavior? I tried for example this: How do I zoom an MKMapView to the users current location without CLLocationManager? with the KVO but it does not work...
I hope someone has an idea?
Best Regards, Tim
Have you tried the delegate method mapView:didUpdateUserLocation:?
I used something like this in my code:
In the .h file:
#property (nonatomic, retain) CLLocation* initialLocation;
And in the .m file:
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
if ( !initialLocation )
{
self.initialLocation = userLocation.location;
MKCoordinateRegion region;
region.center = mapView.userLocation.coordinate;
region.span = MKCoordinateSpanMake(0.1, 0.1);
region = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];
}
}
you can do it like this in your viewDidLoad write this code
self.mapDetail.showsUserLocation = YES;
[self.mapDetail.userLocation addObserver:self
forKeyPath:#"location"
options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
context:nil];
and this method will do the task
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
MKCoordinateRegion region;
region.center = self.mapDetail.userLocation.coordinate;
MKCoordinateSpan span;
span.latitudeDelta = 1; // Change these values to change the zoom
span.longitudeDelta = 1;
region.span = span;
[self.mapDetail setRegion:region animated:YES];
[self.mapDetail.userLocation removeObserver:self forKeyPath:#"location"];
}
I implemented the following MKMapView method, which runs after annotations are added. I have my MKMapView map (parishMap) set to "Shows user location" in Interface Builder, and upon loading the mapview, the blue dot always appears within a second or so on the mapview.
Here's the code I have in my Map View controller implementation:
// Center map on user location (initially)
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
for(MKAnnotationView *annotationView in views) {
if(annotationView.annotation == parishMap.userLocation) {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.03;
span.longitudeDelta=0.03;
CLLocationCoordinate2D location=parishMap.userLocation.coordinate;
location = parishMap.userLocation.location.coordinate;
region.span=span;
region.center=location;
[parishMap setRegion:region animated:TRUE];
[parishMap regionThatFits:region];
}
}
}
When I open the mapview on my iPhone 4, the mapview always (and almost immediately) moves to center on the userLocation dot. However, when I open the mapview on an iPhone 3Gs or iPod Touch (2010), it doesn't center on the userLocation, but just stays at the default zoom/region I set for the map view.
Any ideas as to why this code won't work for non-iPhone 4 devices (they're all running 4.2.x latest iOS)?
This is probably an issue with the amount of time it takes for the GPS to lock a position on these different devices.
I'd recommend using CLLocationManager and its delegate methods to center the map.
You can start updating location by creating an instance of CLLocationManager, setting its delegate property to self and then implementing:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
Where you can center your map accordingly based on the latest location update.
have you tried using this code to center the map:
[mapView setCenterCoordinate:location animated:YES];
I'm not sure why it would work for iPhone 4 and not the others, but this is the code I use to adjust the region:
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(location, 0.03, 0.03);
MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];
[mapView setRegion:adjustedRegion animated:YES];
Try it that way and see how that works. The way you're doing the regionThatFits method, that's actually not supposed to be how the method is used. You can try commenting out that last line of your code, it should make no difference. Anyway, try it with the method I just gave and let me know if you have any questions.
I'm using map kit and showing user's location using "showsUserLocation"
I"m using following code to zoom to user's location, but not zooming. Its zooming to some location in Africa, though the user location on map is showing correct.
MKCoordinateRegion newRegion;
MKUserLocation* usrLocation = mapView.userLocation;
newRegion.center.latitude = usrLocation.location.coordinate.latitude;
newRegion.center.longitude = usrLocation.location.coordinate.longitude;
newRegion.span.latitudeDelta = 20.0;
newRegion.span.longitudeDelta = 28.0;
[self.mapView setRegion:newRegion animated:YES];
Why is user's location correctly showing and not zooming properly. Can some one correct me please?
mapView.userLocation is set to a location off the coast of Africa (0,0) when first instantiated. As a result, I do something like the following, which causes the zoom to occur when the annotation appears. Of course, this is just an example:
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views {
for(MKAnnotationView *annotationView in views) {
if(annotationView.annotation == mv.userLocation) {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.1;
span.longitudeDelta=0.1;
CLLocationCoordinate2D location=mv.userLocation.coordinate;
region.span=span;
region.center=location;
[mv setRegion:region animated:TRUE];
[mv regionThatFits:region];
}
}
}
you can try:
mapView.userTrackingMode=YES;
mapView.userTrackingMode=NO;
UserTrackingMode will zoom to user Location.
Have you verified that the location returned by mapView.userLocation isn't at 0, 0?
how can I zoom the map on my userLocation automatically in my app?
I have the following code to zomm in the map but i must zoom on the userLocation and the following code zooms always to africa?
MKCoordinateRegion zoomIn = mapView.region;
zoomIn.span.latitudeDelta *= 0.2;
zoomIn.span.longitudeDelta *= 0.2;
zoomIn.center.latitude = mapView.userLocation.location.coordinate.latitude;
zoomIn.center.longitude = mapView.userLocation.location.coordinate.longitude;
[mapView setRegion:zoomIn animated:YES];
OK i solved the problem, with the following delegate Method:
-(void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views{
for(MKAnnotationView *annotationView in views) {
if(annotationView.annotation == mv.userLocation){
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.9;
span.longitudeDelta=0.9;
CLLocationCoordinate2D location =mv.userLocation.coordinate;
location = mv.userLocation.location.coordinate;
region.span = span;
region.center = location;
[mv setRegion:region animated:TRUE];
[mv regionThatFits:region];
}
}
}
thanks alot mate, this helped me greatly. Really wanted to vote but cant because of my rep level.
I also found that using:
mapView.userLocation.location.coordinate.latitude;
always zoomed me to africa, and never centered me onto my current location.
But by using the delegate method:
-(void)mapView:(MKMapView *)myMapView didAddAnnotationViews:(NSArray *)views {
I was able to solve this successfully. What I discovered is that when using mapView.userLocation.location.coordinate.latitude; in the viewDidLoad method wasn't triggering the correct user coords on startup of the view, hence it was displaying the wrong coords.