How to display bubble on map annotation pin automatically without drop animation - iphone

I have a standart annotation pin, placing on my coordinates, automatically showing bubble with Title/Subtitle. When tracking my coordinates in move, the pin every time does drop animation. I tried to annView.animatesDrop=FALSE but in this case bubble doesn't appear automatically (for automatic appearance I used selectAnnotation:addAnnotation animated:YES). What needs to do to have a pin with automatic Title/Subtitle showing without drop animation?
P.S. Sorry for my bad English.

annView.animatesDrop=NO;
It works in my code. Displays the pin without drop animation.

You have to use the annView.animatesDrop=FALSE but at the right place. That would be in the
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation
delegate method, right after you've created that annotationView.
You cannot select the annotationView in the same run loop you add it, if I remember well. I use a
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay
to do it, setting the delay to 0 to have it occur on the new run loop.
In my case, I have
[self performSelector:#selector(selectPlace:) withObject:annotation afterDelay:0];
- (void)selectPlace:(id)<MKAnnotation>place{
//Lots of stuff for my precise case
CLLocationCoordinate2D center;
center.latitude = place.latitudeValue;
center.longitude = place.longitudeValue;
MKCoordinateSpan span;
span.latitudeDelta = 4;
span.longitudeDelta = 5;
MKCoordinateRegion germany;
germany.center = center;
germany.span = span;
[mapView setRegion:germany animated:YES];
// End of custom stuff
[mapView selectAnnotation:(id)place animated:YES]; //This is what you're interested in
}

Related

MKMapViewprevent user from zooming out past a certain range

I have a lot of pin annotations on the MKMapView in my app, the iPhone gets very slow and unresponsive when a lot of them are in view on the map. I would like the user to be able to zoom, but not out past a certain level, such as 2km squared or something.
Here's what I've got:
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
[[BicycleLDNService sharedService] requestLocationForClient:self];
CLLocationCoordinate2D zoomLocation;
CLLocation *deviceLocation = [[BicycleLDNService sharedService] deviceLocation];
zoomLocation.latitude = deviceLocation.coordinate.latitude;
zoomLocation.longitude = deviceLocation.coordinate.longitude;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*kMetresPerKilometre, 0.5*kMetresPerKilometre);
MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:viewRegion];
[self.mapView setRegion:adjustedRegion animated:YES];
self.mapView.zoomEnabled = YES;
}
Is there some sort of property or delegate method I can employ? Couldn't find anything useful on google or here!
Thanks in advance!
The span defines how much of the map at the given point should be visible and is also how you set the zoom level.
You can access this by using
region.span.latitute=0.5;
region.span.longitude=0.6;
Check the zoom level of the map and then set the zoomEnabled Property NO.
mapView.zoomEnabled=NO;

Showing Specific Region Using MapKit

I want to know is it possible to show only specific region on map not the full world map using Map Kit.
Like if i want to show Asia map in my application then map kit hides remaining part of the map.
To handle the "map kit hides remaining part of the map" requirement, one thing you can do is create a black polygon overlay that covers the whole world with a cutout over Asia (or wherever you like).
For example, where you initialize the map (eg. in viewDidLoad):
CLLocationCoordinate2D asiaCoords[4]
= { {55,60}, {55,150}, {0,150}, {0,60} };
//change or add coordinates (and update count below) as needed
self.asiaOverlay = [MKPolygon polygonWithCoordinates:asiaCoords count:4];
CLLocationCoordinate2D worldCoords[4]
= { {90,-180}, {90,180}, {-90,180}, {-90,-180} };
MKPolygon *worldOverlay
= [MKPolygon polygonWithCoordinates:worldCoords
count:4
interiorPolygons:[NSArray arrayWithObject:asiaOverlay]];
//the array can have more than one "cutout" if needed
[myMapView addOverlay:worldOverlay];
and implement the viewForOverlay delegate method:
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKPolygon class]])
{
MKPolygonView *pv = [[[MKPolygonView alloc] initWithPolygon:overlay] autorelease];
pv.fillColor = [UIColor blackColor];
pv.alpha = 1.0;
return pv;
}
return nil;
}
This looks like this:
If you also want to restrict the user from scrolling beyond Asia or zooming too far out, then you'll need to do that manually as well. One possible way is described in Restrict MKMapView scrolling. Replace theOverlay in that answer with asiaOverlay.
You can specify the region as an MKCoordinateRegion and then tell an MKMapView instance to only show that region using the setRegion and regionThatFits message.
Alternatively you could use the visibleMapRect property instead of the region. This might better fit your needs.
In short read the MKMapView Class Reference document from Apple.
Lifting from some code I've done in the past that assumes a mapView and a given location called locationToShow I used an MKCoordinateRegion.
- (void) goToLocation {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.01;
span.longitudeDelta=0.01;
region.span=span;
region.center=locationToShow;
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
}

GPS location on map in application

I have one application that shows maps. I need to show current position of user and manage to do that with
[mapView setShowsLocation:YES];
But when I zoom In or zoom Out map it needs a lot of time to show me that blue pin again.
Is it normal or I need to put something else to keep that blue pin on screen all time???
Thanks.
[self setCurrentLocation:self._mapView.userLocation.location.coordinate withZoom:1.0];
self._mapView.showsUserLocation = YES;
- (void)setCurrentLocation:(CLLocationCoordinate2D)coord withZoom:(float)zoomLevel {
MKCoordinateRegion region = self._mapView.region;
region.span.latitudeDelta = self.defaultSpanLevel.latitudeDelta*zoomLevel;
region.span.longitudeDelta = self.defaultSpanLevel.longitudeDelta*zoomLevel;
region.center = coord;
[self._mapView setRegion:region animated:YES];
}
You can use this in view will appear Method
Then You can use mapview delegate methods....
region did change animated:YES { and set map's region here.... take
current user location as region centre.. }
This will solve your problem
}

Center MKMapView on userLocation (initially) - only works on iPhone 4?

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.

Zoom on userLocation

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.