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.
Related
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.
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.
Is there a way to display some text on the blue dot alike to an annotation? Right now, it only shows "current location" when the user tap on it. I want it to act somewhat like an annotation to display current address. Where there's a title and subtitle property available to use.
I thought of replacing it with my own annotation but can't figure the way to do so.
Please advise.
Thank you
You should be able to do it by implementing mapView:didSelectAnnotationView: in MKMapView Delegate...
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
if(view.annotation = mapView.userLocation){
//reverse geocode mapview.userLocation.coordinate to get address
//then set title
}
}
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.
Ok, so I've got some issues with my MapKit annotations.
First of all, I want a callout for each annotation. I can't find out how to do this at all! :( I have two NSStrings (name of the place and a short description). Then I need to be able to log the click on the callout - so I can launch a disclosure view.
Secondly I want to change the view for the user location annotation.
At the moment all annotations are set to the "default" pin annotation view by way of this code:
- (MKAnnotationView *) mapView:(MKMapView *)myMapView viewForAnnotation:(id <MKAnnotation>) annotation{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"currentloc"];
annView.animatesDrop=TRUE;
return annView;
}
I would prefer to have the user location annotation set to the default blue pulsing circle thing, but at least I want to be able to change the pinColor property to a different color, so the user can distinguish between their own location and the results of their search.
I hope somebody can help me.
Thank you.
I think you should consider using showsUserLocation to depict your users location - it's the standard UI for the platform and helps people get a consistent experience across apps. Sure you can come up with some custom annotation for user location but why re-invent what people are used to seeing on the maps app and on many 3rd party apps?
You should take the time to review Apple's code samples for MapKit everything you need to do is there. This one in particular is a good starting point for you:
http://developer.apple.com/iphone/prerelease/library/samplecode/MapCallouts/Introduction/Intro.html
hmm... the problem was i didn't know which annotation was the one being used by current location. Its ok - I've found it now. Something like if(annotation == mapView.userlocation){ return nil; } or similar. :) Thanks for your help though