i have a map with Annotations and when anyone click on any Annotation its alert with information that i have been programmed it.but the only annotation that is not loaded with my infos is the one that the user current location is near by.how could i solve this problem and make the annotation of the current location appear with infos.
here is the little code of user current location
[self.mapView setDelegate:self];
[self.mapView setShowsUserLocation:YES];
self.mapView.userTrackingMode = MKUserTrackingModeFollow;
when i click on this alert in pic nothing happen coz its the current location.but others are worked perfectly
For this you can override user location by
On mapview uncheck the 'shows user location'
fetch coordinates of User from delegate methods
Use those coordinates in custom annotations.
This way your user location can be overridden.
Hope this helps.
Related
I'm using the following code to zoom into my user's current location on a MapView. It works great, however I still want to allow my users to be able to scroll outside of the set region as well (e.g. so that they can see other users on the map). Right now, if my user scrolls outside of the set region, the MapView reverts the user back inside of the set region. How can I execute the below code (zooming into the user's location) while still allowing them to scroll outside of the set region?
MapViewController.m
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 1300, 1300);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
}
I wrote a short an easy app that will show the user location and also keeps track of the user location. The app will focus on the user location and will keep the user location centered as long as you don't scroll the map view.
As soon as you start scrolling the map view moves away from the user location and stays that way, since the map view is not automatically moving back. To enable you to get back to the user location, I added a button called "focus" that centers the map view back to the user location and makes the map again focus on the user location until you start scrolling away again.
The app is actually quite simple, it uses the two properties in MKMapView:
var showUserLocation: Bool {get set}
var userTrackingMode: MKUserTrackingMode {get set}
You can, of course, zoom further in to the user location by setting the region, but that is easily done with the setRegion(_:animated:).
You can find the code on Github here and download it to see how it works. You can easily test this with the simulator.
Let me know if this helps.
I'm building an app where annotations mark points of interest that the user drives by. The map tracks the user's location and scrolls along, but annotations that should come into view on the map don't appear until the user drags the map with her finger.
Adding and removing annotations works fine so I'm not sure what code to show.
Has anyone seen similar behavior?
Edit:
This affects only some annotations - some appear, some don't. No apparent pattern.
As suggested, I've changed the code so that I add and remove annotations only on the main thread:
dispatch_async(dispatch_get_main_queue(), ^{
[self.mapView removeAnnotations:annotationsToRemove];
[self.mapView addAnnotations:annotationsToAdd];
});
But the problem persists. Darn...
For me I just had to call addAnnotations on the main thread:
dispatch_async(dispatch_get_main_queue(), ^{
[self.mapView addAnnotations:annotations];
});
And it works fine
when you add an annotation call all the following methods on that annotation for it to show.
[_mapView addAnnotation:theAnnotation];
[_mapView deselectAnnotation:theAnnotation animated:NO];
[_mapView selectAnnotation:theAnnotation animated:NO];
For some of the apps I have worked on, unless you deselect and select the annotation, it wouldn't show it on the map (even if you just add it). Hope this helps!
It's hard to say without seeing any of your code. That being said, if you haven't already, try checking to see if any of the annotations are within the current visible map area in mapView:didUpdateUserLocation and, if they are, refresh your map view (see the answer to How do you check if an MKAnnotation is available within a MKCoordinateRegion for more on checking annotations against the visible map rect).
I have an app that calls addAnnotation exactly 3 times (after verifying that the coordinates are good using CLLocationCoordinate2DIsValid, passing a model that responds to id, title latitude, longitude and coordinate. In the model, I watch the callbacks by doing an NSLog in my coordinate method.
Note that coordinate and name are implemented as methods, although this should make no difference, right?
What I expect:
MapKit will access the coordinate method 3 times
What I get:
MapKit access the coordinate 3 times per coordinate, then tries a 4th time, even though there is no 4th coordinate and the app crashes on a memory exception as the result is nil.
I am pretty naive about MapKit, but there has to be some explanation for this that I'm missing.
Any help appreciated!
If the user location is shown on your map (the blue dot) you'll have four annotations, as the blue dot also is an annotation.
Your app probably crashes as the annotation used to show the user location has no method called coordinate
To prevent your code to call the method coordinate you can either check if the retrieved annotation is an instance of your custom annotation class:
if ([annotion isKindOfClass:[YourCustomeAnnotation class]])
// it's one of your annotations
else
// it's the current location annotation
or check if the current annotation is the current location anotation
if ([annotion isKindOfClass:[MKUserLocation class]])
// it's the current location annotation
else
// it's one of your annotations
I have a map of a zoo with pins that locate the animals. Zoom on the map. Tap on a pin then tap callout and it takes the user to a view with pic of animal and description/narrative. The "map" button on that view returns the user to the zoo map at the original zoom level. Does anyone know of a way to return to the map at the last zoom level/location?
You can set what the region of what the user can see when your map gets displayed by using the
[self.mapView setRegion:adjustedRegion animated:YES];
function. So what you want to do is, save the "region" property just when the map view is about to disappear so that you can set it again when the map view appears.
I am working on an application which use user location updates, I have got the current latitude and longitude and display the current location of the user and annotate the pin, However I need help regarding moving a pin when user changes his location or when user moves and get new latitude and longitude the pin should also move to that position.
To show the current location, you could just set showsUserLocation to YES and let the map show the blue dot for you.
If you want to show your own annotation instead and have it move automatically, implement setCoordinate: in the class that implements the MKAnnotation protocol.
Then when the coordinates change, update the annotation's coordinate and the map view will automatically (via KVO) move the annotation's view/pin.
You could also remove the annotation and create a new one at the new location but that can result in flicker.
If you are using Core Location to get location updates, you would update the annotation's coordinates in the locationManager:didUpdateToLocation:fromLocation: delegate method.