In my code, I'm calling
- (IBAction)goToUserLocation:(id)sender {
[mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
}
which then has this callback
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
// I then try to set the region in here, which doesnt always work. Sometimes it zooms into the user location, then zooms back out to the region I specify. Other times, it will just stay zoomed in.
}
The MKUserTrackingModeFollow zooms in far more than I would like. I'd like to be able to set it so that it zooms into a region I specify. Is there a proper way to do this or is the region of MKUserTrackingModeFollow set?
Thank you!
viewForAnnotation is completely the wrong place. That is called when ever the map decides it needs to draw an annotation on the map. The map may not need to redraw an annotation if it was already on screen when someone pressed the button to goToUserLocation.
Instead try setting the zoomed out region before the tracking mode is set or after the animation is done.
Related
How can I get all the visible MKAnnotationViews in
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)clickedview
Basically I want to make some calculation to get all the views touching (in cascade) the clickedview
thanks
You can use annotationsInMapRect: to get all the annotations inside a map rect. visibleMapRect will get you the current map rect to use.
My map shows current location with blue dot..
Of course, when I move, blue dot moves.. I just want to keep blue dot center of the map all the time, making map's moving instead like Google map navigator..
I searched a lot but couldn't find the way...
In your map view delegate, do this:
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
[mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
}
Try using mapView.userTrackingMode = MKUserTrackingModeFollow;, you'll get smoother panning than using the location manager manually.
In addition note that there is a ready-made button for managing the various tracking states:
MKUserTrackingBarButtonItem.
The easiest way would be to create a new UIView on top of your map's view with a blue dot and only move the map's view.
I have an maps application in which I show the points of interest in the map using pins. I want to represent the current location with the blue circle which is shown in Google maps. Is there any way we can add it. Can any one please help me with this. I really appreciate your time.
Thanks
You can get this by specifying nil in the appropriate delegate method:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation: (id<MKAnnotation>)annotation {
if (annotation == mapView.userLocation) {
return nil;
}
}
This should cause MKMapView to use the default, which should be the blue circle.
You should set the setShowsUserLocation: property of your map view to YES. This can be done programmatically or in Interface Builder. Be careful, as I believe the simulator still shows the user location in Cupertino, CA.
Also, if you continue tracking the user and their movements, the blue indicator will continue to move as they do.
Update #5 I guess it's bounty time. 100+ views and no one's taken a stab, even with the code sample I've posted. How about some reputation points!
Update #4This is a pretty complicated question, so I created a new tab based project which just includes the parts of my app I'm having trouble with here. You can download it from: http://www.servinitup.net/CustomCalloutAnnotation.zip
Feel free to open it up (need to add your own bundle identifier to run it on your phone) and play around with it, and see if you can get that darned callout annotation to move with the pin!
Update #3 Tried making setAnnotation a public method of the tutorial's CalloutMapAnnotationView and calling that directly. Didn't have any luck. Despite little oddities that occurred, the only thing that moved was the little triangle portion of the callout. I could not get the entire callout to move.
Update #2 Still not much luck, but have now been looking for ways to programmatically create a "pinch to zoom" and then undo it immediately, so the user never sees a change. Hoping that doing this programmatically will have the same effect as doing it manually, and the callout annotation will pop back to it's parent. Any ideas?
Update #1 After playing around here's where I've gotten:
- replaced self.calloutAnnotation.coordinate = coords; with self.calloutAnnotation.latitude = coords.latitude;self.calloutAnnotation.longitude = coords.longitude;
- With that changed, if I slightly pinch the map to zoom in or out after the pin has been updated, the callout annotation animates to the correct position, right over the pin.
So now I need to figure out how to get this to happen without the user having to actually pinch to zoom.
Original Post
I, along with other SO users, are using this awesome solution to create a custom callout annotation:
http://blog.asolutions.com/2010/09/building-custom-map-annotation-callouts-part-1/
When you use a standard callout (annotationview.canShowCallout = true) and the pin moves around the screen as the location is updated, the standard callout tracks right along with the pin, as if they're locked together.
When using the custom callout solution above, when my pin moves around after location updates, the callout annotation stays in it's original location. Of course, I'd like to mimic the iOS standard and have the custom callout annotation track along with the pin.
Here's the code I have so far, that's successfully moving the annotation view, but not the custom callout annotation view:
/* core location broadcasts a notification, and my view controller listens to that notification and calls locationManagerDidFindLocation */
- (void)locationManagerDidFindLocation:(NSNotification *)notif {
CLLocation *location = [notif.userInfo objectForKey:#"location"];
CLLocationCoordinate2D coords = [location coordinate];
MKCoordinateSpan span = MKCoordinateSpanMake((5/69), (5/69));
MKCoordinateRegion region = {coords, span};
// if we don't have a current location yet, create one, place it on the map, and adjust the map's region
// otherwise, update the annotation placement and map position in a smooth animation
if (self.currentLocationAnnotation == nil) {
self.currentLocationAnnotation = [[CurrentLocationAnnotation alloc] initWithCoordinate:coords andTitle:#"My Title" andSubtitle:#"My subtitle"];
[self.mapView addAnnotation:self.currentLocationAnnotation];
[self.mapView setRegion:region animated:true];
[self.mapView regionThatFits:region];
} else {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.45];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
// this moves my annotation view perfectly
self.currentLocationAnnotation.coordinate = coords;
// ******* this is where my problem is
// ******* this custom callout annotation view stays
// ******* in it's original place, even though it's
// ******* parent annotation view is moving around the screen
self.calloutAnnotation.coordinate = coords;
[self.mapView setRegion:region animated:true];
[self.mapView regionThatFits:region];
[UIView commitAnimations];
}
}
I created a project based on your CalloutMapAnnotationView demonstrating a IB based solution. Arrow keys animate motion of the location annotation and it's callout annotation. The callout now also automatically resizes based on the supplied contentView, and the view is loaded from a separate nib. Good luck!
https://github.com/jacobjennings/JJMapCallout
I know you aren't going to like this answer, but it works. The sample you linked to just draws a custom image in the overlay through the most difficult way possible (drawRect:). Have you considered rendering your overlay once into a UIImage, and just setting the image property on a very simple MKAnnotationView? Even if you need to change the content periodically, like updating the number of friends at a bar, you can redraw the image when the change occurs and update the appropriate MKAnnotationView.
I review your code and my recommendation is to create a new custom MKAnnotationView and encapsulate on it both views (the pin and the callout).
But for your current code consider read the approved response this question: MKMapView moving Annotations Automatically - animate them?
Cheers.
I am developing an iPhone application in which I need to show an image at a specific Latitude & Longitude on a Map. The requirement is - Even if the user zooms in or out on this map screen, the map control should automatically center itself after the zoom, to the same Lat & Long. I am using MKMapView.
I have tried to go through all available documentation. However, I could not come across any callback or other method that will allow me to achieve the objective.
One alternative is to handle touch events. However, I find that as a very complicated solution. And I hope that a simpler alternative exists. Any ideas on the above?
Can you please help? Any example will be appreciated.
Implement MKMapViewDelegate and then try this:
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
[mapView setCenterCoordinate:locationManager.coordinate animated:NO];
}
Replace locationManager with whatever the name of your CLLocationManager is.