Take user to location when they get their location - mkmapview

I have code that gets user location and displays it on the mapView, but they have to zoom out and find themselves on the map, what I want is when the button is pressed it automatically takes them to where they are. Heres my code so far:
- (void)viewDidLoad
{
[super viewDidLoad];
MKCoordinateRegion bombersRegion = { {0.0,0.0}, {0.0,0.0} };
bombersRegion.center.latitude = 42.812548;
bombersRegion.center.longitude = -73.940815;
bombersRegion.span.latitudeDelta = 0.01f;
bombersRegion.span.longitudeDelta = 0.01f;
[bombersMapView setRegion:bombersRegion animated:YES];
MapPin *ann = [[MapPin alloc] init];
ann.title = #"Bombers Burrito Bar";
ann.subtitle = #"447 State Street Schenectady, New York 12305";
ann.coordinate = bombersRegion.center;
[bombersMapView addAnnotation:ann];
}
-(IBAction)getLocation:(id)sender{
bombersMapView.showsUserLocation = YES;
}
Thanks for the help.
I would really appreciate some sample code.

If you want the the map to stay centered on user's location try this
[bombersMapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];

Take a look at this MKMapViewDelegate method. This will give you the users location which you can use to zoom in on. The method you're looking for is mapView:didUpdateUserLocation:.

Related

MKMapView not centering on location iOS8

I have my app on two devices, one on an iOS7 enabled device and another on an iOS8 enabled device. My MKMapView is not center on the users location on the iOS8 device but is on the iOS7.
Here is my code, I have requested the use of the users location on another screen.
-(void)viewDidLoad{
[LocationManager sharedInstance];
CLLocation * location = [[LocationManager sharedInstance] getCurrentLocation];
NSLog(#"Location %f %f",location.coordinate.latitude,location.coordinate.longitude);
MKCoordinateRegion mapRegion;
mapRegion.center = location.coordinate;
mapRegion.span.latitudeDelta = 0.01;
mapRegion.span.longitudeDelta = 0.01;
NSLog(#"Map Region Lat %f",mapRegion.center.latitude);
NSLog(#"Map Region Long %f ",mapRegion.center.longitude);
self.getDirectionsMap.delegate = self;
[self.getDirectionsMap setRegion:mapRegion animated: NO];
self.getDirectionsMap.showsUserLocation=YES;
self.getDirectionsMap.showsPointsOfInterest=YES;
}
My Location Manager object .m
+ (LocationManager *)sharedInstance {
if (nil == kLocationManager) {
kLocationManager = [[LocationManager alloc] init];
}
return kLocationManager;
}
-(CLLocation *)getCurrentLocation{
CLLocation *loc = [_locationManager location];
return loc;
}
- (id)init {
self = [super init];
if (!self) return nil;
/* setup location manager */
_locationManager = [[CLLocationManager alloc] init];
[_locationManager setDelegate:self];
//iOS8 check
if ([_locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]) {
[_locationManager requestWhenInUseAuthorization];
}
[_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[_locationManager setDistanceFilter:DISTANCE_FILTER];
return self;
}
My map successful shows the users position but map is not centered. My location also gets logged correctly with the correct coordinates, and this also matches the logged Map region lat and long.
You viewDidLoad method has not checked that there is actually a location to use yet. If you used a CLLocationManager you could set it going and then it would call the delegate's methods when it has an actual location. I suspect that the iOS device is taking longer to get a GPS reading and it is giving you nil in viewDidLoad.

My mapview does not zoom on the first pass into the view controller

Can anybody help me please? I'm new to xcode and am baffled by what I think is a small problem. For some reason the first time my map is opened from my initial view controller the pin is dropped but the map does not zoom to the correct region even though the latitude and longitude are correct.
When I go back to the first view controller and launch it again the zoom works.
My code is in the view controllers implementation file under viewdidload.
-(void)viewDidLoad {
[super viewDidLoad];
NSLog(#"lat in map %#", eventLat);
NSLog(#"long in map %#", eventLong);
[mapview setMapType:MKMapTypeStandard];
[mapview setZoomEnabled:YES];
[mapview setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, {0.0, 0.0 } };
region.center.latitude = [eventLat doubleValue];
region.center.longitude = [eventLong doubleValue];
region.span.latitudeDelta = 0.01f;
region.span.longitudeDelta = 0.01f;
[mapview setRegion:region animated:YES];
MMAnnotate *ann = [[MMAnnotate alloc] init];
ann.title = #"Test Title";
ann.subtitle = #"Test Subtitle";
ann.coordinate = region.center;
[mapview addAnnotation:ann];
}
You should try doing that in viewDidAppear instead. If you're prepared for it to zoom to that location every time the view appears. viewDidLoad is too early in the mapview cycle.

The MKCoordinateRegion always locked on center Coordinates 0,0?

This is a little strange, but I have been trying to find a solution for 2 days straight, to set the map region but nothing seems to work
Here is my code :
-(void)viewWillAppear:(BOOL)animated{
//[self step1locationupdate];
Maplocation.showsUserLocation=YES;
MKCoordinateRegion mapRegion;
mapRegion.center.longitude=self.currentProduct.shop.longcoordinate;
mapRegion.center.latitude=self.currentProduct.shop.latcoordinate;
NSLog(#"The USer Location are :%f",mapRegion.center.latitude);
mapRegion.span.latitudeDelta= 2;
mapRegion.span.longitudeDelta= 2;
[Maplocation setRegion:mapRegion animated:YES];
NSLog(#"The USer Location are :%f %f",Maplocation.userLocation.coordinate.latitude,Maplocation.userLocation.coordinate.longitude);
}
The NSLog for malocation.userlocation.coordinate are always 0 when it starts.
The same code I have added it into the viewDidLoad part & also there was no difference
Kindly help
I think you need to initialize your MKCoordinateRegion before you start using it. Something like 'MKCoordinateRegion mapRegion = MKMakeRegionWithCoordinates(......'
Well I have saved the above method in a (void) method , then I have fired it after 5 Sec from viewWillAppear method.
-(void)viewWillAppear:(BOOL)animated{
[self performSelector:#selector(updatecurrentLocation) withObject:nil afterDelay:5];
}
-(void)updatecurrentLocation{
Maplocation.showsUserLocation=YES;
MKCoordinateRegion mapregion;
mapregion.center=Maplocation.userLocation.coordinate;
mapregion.span.latitudeDelta = 2;
mapregion.span.longitudeDelta = 2;
[Maplocation setRegion:mapregion animated: YES];
}
That is it folks :)

iOS4 MKMapKit - map not zooming in when more than one pin

Sure this is something simple as I'm just starting with the maps. I already have a map showing one location, but when I've added a second anotation the map stays zoomed all the way out rather than going to my locations. The pins are there when I zoom in, so I know that bit's working.
Code snippets:
- (void)viewDidLoad
{
...
...
...
// Set coordinates for our position
CLLocationCoordinate2D location;
location.latitude = [self.lat doubleValue];
location.longitude = [self.lon doubleValue];
// Add the annotation to our map view
MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc]
initWithTitle:self.placename
andSubtitle:self.subtitle
andCoordinate:location];
[self.mapView addAnnotation:newAnnotation];
[newAnnotation release];
// Set coordinates for our second position
CLLocationCoordinate2D amenitylocation;
amenitylocation.latitude = self.latitude;
amenitylocation.longitude = self.longitude;
// Add the annotation to our map view
MapViewAnnotation *amenityAnnotation = [[MapViewAnnotation alloc]
initWithTitle:self.callouttitle
andSubtitle:self.calloutsubtitle
andCoordinate:amenitylocation];
[self.mapView addAnnotation:amenityAnnotation];
[amenityAnnotation release];
[super viewDidLoad];
}
#pragma mark - MKMapView Delegates
// When a map annotation point is added, zoom to it (1500 range)
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
{
MKAnnotationView *annotationView = [views objectAtIndex:0];
id <MKAnnotation> mp = [annotationView annotation];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 1500, 1500);
[mv setRegion:region animated:YES];
[mv selectAnnotation:mp animated:YES];
}
- (MKAnnotationView *)mapView:(MKMapView *)mv viewForAnnotation:(id<MKAnnotation>)annotation
{
if(mapView.userLocation==annotation)
{
return nil;
}
NSString *identifier = #"IDENTIFIER";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if(annotationView==nil)
{
annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]autorelease];
annotationView.pinColor=MKPinAnnotationColorPurple;
annotationView.canShowCallout=YES;
}
return annotationView;
}
I'd appreciate any pointers.
Also, am I right in thinking I'll have to make custom callouts if I want more than one to appear on the map at the same time?
Sorry, found the answer - I didn't have the MKMapView delegate linked to File's Owner in IB, although I do have in my header file. Linked that up and it's working.

How to capture Tap gesture on MKMapView

I am trying to capture tap event on my MKMapView, this way I can drop a MKPinAnnotation on the point where user tapped. Basically I have a map overlayed with MKOverlayViews (an overlay showing a building) and I would like to give user more information about that Overlay when they tap on it by dropping a MKPinAnnotaion and showing more information in the callout.
Thank you.
You can use a UIGestureRecognizer to detect touches on the map view.
Instead of a single tap, however, I would suggest looking for a double tap (UITapGestureRecognizer) or a long press (UILongPressGestureRecognizer). A single tap might interfere with the user trying to single tap on the pin or callout itself.
In the place where you setup the map view (in viewDidLoad for example), attach the gesture recognizer to the map view:
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleGesture:)];
tgr.numberOfTapsRequired = 2;
tgr.numberOfTouchesRequired = 1;
[mapView addGestureRecognizer:tgr];
[tgr release];
or to use a long press:
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:#selector(handleGesture:)];
lpgr.minimumPressDuration = 2.0; //user must press for 2 seconds
[mapView addGestureRecognizer:lpgr];
[lpgr release];
In the handleGesture: method:
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state != UIGestureRecognizerStateEnded)
return;
CGPoint touchPoint = [gestureRecognizer locationInView:mapView];
CLLocationCoordinate2D touchMapCoordinate =
[mapView convertPoint:touchPoint toCoordinateFromView:mapView];
MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
pa.coordinate = touchMapCoordinate;
pa.title = #"Hello";
[mapView addAnnotation:pa];
[pa release];
}
I setup a long press (UILongPressGestureRecognizer) in viewDidLoad: but it just detect the only one touch from the first.
Where can i setup a long press to detect all touch? (it means the map ready everytime waiting user touch to screen to push a pin)
The viewDidLoad: method!
- (void)viewDidLoad {
[super viewDidLoad];mapView.mapType = MKMapTypeStandard;
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleLongPressGesture:)];
[self.mapView addGestureRecognizer:longPressGesture];
[longPressGesture release];
mapAnnotations = [[NSMutableArray alloc] init];
MyLocation *location = [[MyLocation alloc] init];
[mapAnnotations addObject:location];
[self gotoLocation];
[self.mapView addAnnotations:self.mapAnnotations];
}
and the handleLongPressGesture method:
-(void)handleLongPressGesture:(UIGestureRecognizer*)sender {
// This is important if you only want to receive one tap and hold event
if (sender.state == UIGestureRecognizerStateEnded)
{NSLog(#"Released!");
[self.mapView removeGestureRecognizer:sender];
}
else
{
// Here we get the CGPoint for the touch and convert it to latitude and longitude coordinates to display on the map
CGPoint point = [sender locationInView:self.mapView];
CLLocationCoordinate2D locCoord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
// Then all you have to do is create the annotation and add it to the map
MyLocation *dropPin = [[MyLocation alloc] init];
dropPin.latitude = [NSNumber numberWithDouble:locCoord.latitude];
dropPin.longitude = [NSNumber numberWithDouble:locCoord.longitude];
// [self.mapView addAnnotation:dropPin];
[mapAnnotations addObject:dropPin];
[dropPin release];
NSLog(#"Hold!!");
NSLog(#"Count: %d", [mapAnnotations count]);
}
}
If you want to use a single click/tap in the map view, here's a snippet of code I'm using. (Cocoa and Swift)
let gr = NSClickGestureRecognizer(target: self, action: "createPoint:")
gr.numberOfClicksRequired = 1
gr.delaysPrimaryMouseButtonEvents = false // allows +/- button press
gr.delegate = self
map.addGestureRecognizer(gr)
in the gesture delegate method, a simple test to prefer the double-tap gesture …
func gestureRecognizer(gestureRecognizer: NSGestureRecognizer, shouldRequireFailureOfGestureRecognizer otherGestureRecognizer: NSGestureRecognizer) -> Bool {
let other = otherGestureRecognizer as? NSClickGestureRecognizer
if (other?.numberOfClicksRequired > 1) {
return true; // allows double click
}
return false
}
you could also filter the gesture in other delegate methods if you wanted the Map to be in various "states", one of which allowed the single tap/click
For some reason, the UIGestureRecognizer just didn't work for me in Swift. When I use the UIGestureRecognizer way. When I used the touchesEnded method, it returns a MKNewAnnotationContainerView. It seems that this MKNewAnnotationContainerView blocked my MKMapView. Fortunately enough, it's a subview of MKMapView. So I looped through MKNewAnnotationContainerView's superviews till self.view to get the MKMapView. And I managed to pin the mapView by tapping.
Swift 4.1
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
let t = touches.first
print(t?.location(in: self.view) as Any)
print(t?.view?.superview?.superview.self as Any)
print(mapView.self as Any)
var tempView = t?.view
while tempView != self.view {
if tempView != mapView {
tempView = tempView?.superview!
}else if tempView == mapView{
break
}
}
let convertedCoor = mapView.convert((t?.location(in: mapView))!, toCoordinateFrom: mapView)
let pin = MKPointAnnotation()
pin.coordinate = convertedCoor
mapView.addAnnotation(pin)
}

Resources