Mapkit adding new annotation and remove the old - ios

i want the map to get the location of the user and he can change the location by taping on the map. The problem is when tap on location the method (void)mapView:(MKMapView *)mapView_ didUpdateUserLocation , called and the map show both the current location and the location he tapped in!! i want only one location >>>
here is the code
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.mapView.delegate = self;
locationManager = [[CLLocationManager alloc] init];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[mapView addGestureRecognizer:singleTap];
}
- (void)mapView:(MKMapView *)mapView_ didUpdateUserLocation:(MKUserLocation *)userLocation
{
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
longitudeLabel.text = [NSString stringWithFormat:#"%.8f", userLocation.coordinate.longitude];
latitudeLabel.text = [NSString stringWithFormat:#"%.8f", userLocation.coordinate.latitude];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
[mapView setRegion:[mapView regionThatFits:region] animated:YES];
// Add an annotation
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = userLocation.coordinate;
point.title = #"Where am I?";
point.subtitle = #"I'm here!!!";
[mapView addAnnotation:point];
}
- (void)handleSingleTap:(UIGestureRecognizer *)sender
{
CLLocationCoordinate2D coord = [mapView convertPoint:[sender locationInView:mapView] toCoordinateFromView:mapView];
NSLog(#"Map touched %f, %f.", coord.latitude, coord.longitude);
longitudeLabel.text = [NSString stringWithFormat:#"%.8f", coord.longitude];
latitudeLabel.text = [NSString stringWithFormat:#"%.8f", coord.latitude];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 800, 800);
[mapView setRegion:[mapView regionThatFits:region] animated:YES];
// Add an annotation
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = coord;
point.title = #"Where am I?";
point.subtitle = #"I'm here!!!";
[mapView removeAnnotations:[mapView annotations]];
[mapView addAnnotation:point];
}

Firstly, on ask one question on each page. If I answer question 2 right and someone else answers question 1 right, which one gets marked as the right answer?
Secondly, the method didUpdateUserLocation is called by iOS when it gets new information about where the device is. You don't need to add a new annotation there or you'll end up with lots of annotations for every time the device moves. If you want to show the user's current location call mapView.showsUserLocation = YES;. Once the user has tapped and you've created a new annotation, you can turn the userLocation off by setting it to NO.

Related

Pin not showing on Mapkit - obj-c

I'm a newbie to ios dev. I tried to add pin annotations on map using mapkit. However, after tried the most basic methods, it still didn't show up, either that's a simple pin, or a custom annotationview. There is no error message, it's just the pin won't show up in the map.
Here's my code:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.mapView.delegate = self;
NSLog(#"Custom Annotations start.");
// Custom Annotations.
CLLocationCoordinate2D bryanParkCoordinates = CLLocationCoordinate2DMake(37.785834, -122.406417);
MyCustomAnnotation *bryanParkAnnotation = [[MyCustomAnnotation alloc]initWithTitle:#"Bryant Park" Location:bryanParkCoordinates];
[self.mapView addAnnotation:bryanParkAnnotation];
// Simple pin.
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate:bryanParkCoordinates];
[annotation setTitle:#"Title"];
[self.mapView addAnnotation:annotation];
// Another try.
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(37.330713, -121.894348);
MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
MKCoordinateRegion region = {coord, span};
MKPointAnnotation *newannotation = [[MKPointAnnotation alloc] init];
[newannotation setCoordinate:coord];
[self.mapView setRegion:region];
[self.mapView addAnnotation:annotation];
// On your location.
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = bryanParkCoordinates;
point.title = #"Where am I?";
point.subtitle = #"I'm here!!!";
[self.mapView addAnnotation:point];
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if([annotation isKindOfClass:[MyCustomAnnotation class]])
{
MyCustomAnnotation *myLocation = (MyCustomAnnotation *)annotation;
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:#"MyCustomAnnotation"];
if(annotationView == nil)
annotationView = myLocation.annotationView;
else
annotationView.annotation = annotation;
return annotationView;
}
else{
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"DETAILPIN_ID"];
[pinView setAnimatesDrop:YES];
[pinView setCanShowCallout:NO];
return pinView;
}
}
If I just want a simple pin on map, just adding those lines
// Simple pin.
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate:bryanParkCoordinates];
[annotation setTitle:#"Title"];
[self.mapView addAnnotation:annotation];
should be enough? Is there other things that I could have missed? Like adding other delegate or something else other than adding those simple codes in 'viewDidLoad'? Thanks so much!
Managing Annotation Views
You should implement this delegate method and create a view for the annotation and return it.
basically when you add an annotation this delegates method will get called and you have to create the view for that annotation within this delegate method
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.mapView.delegate = self;
NSLog(#"Custom Annotations start.");
// Custom Annotations.
CLLocationCoordinate2D bryanParkCoordinates = CLLocationCoordinate2DMake(37.785834, -122.406417);
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(37.330713, -121.894348);
MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
MKCoordinateRegion region = {coord, span};
[self.mapView setRegion:region];
[self.mapView addAnnotation:annotation];
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = bryanParkCoordinates;
point.title = #"Where am I?";
point.subtitle = #"I'm here!!!";
[self.mapView addAnnotation:point];
}
Try the following code:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if (annotation == mapView.userLocation) return nil;
static NSString* Identifier = #"PinAnnotationIdentifier";
MKPinAnnotationView* pinView;
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:Identifier];
if (pinView == nil) {
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:Identifier];
pinView.canShowCallout = YES;
return pinView;
}
pinView.annotation = annotation;
return pinView;
}
Don't forget to add <MKMapViewDelegate> to your #interface section.
#interface YourViewController () <MKMapViewDelegate>

How to set longitude and latitude using textfield

How can I set the longitude and latitude using a textfield? I have tried making my textfield a double but it just seems to crash.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
mapview.showsUserLocation = YES;
MKCoordinateRegion region = { {0.0, 0.0}, {0.0,0.0}};
region.center.latitude = *(myTextField);
region.center.longitude = *(myTextField1);
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapview setRegion:region animated:YES];
MapPin *ann = [[MapPin alloc] init];
ann.title = #"Test";
ann.subtitle = #"test";
ann.coordinate = region.center;
[mapview addAnnotation:ann];
}
How I can make this work?
MKPointAnnotation *annonation = [[MKPointAnnotation alloc]init];
CLLocationCoordinate2D mycordinate;
mycordinate.latitude = [self.latitude floatValue];
mycordinate.longitude =[self.longitude floatValue];
annonation.coordinate = mycordinate;
[self.mapview addAnnotation:annonation];
In self.latitude store your latitude value and
in self.longitude store your longitude value.
Try below Code:
Just get text from textFields and set latitude,longitude float value from text.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
mapview.showsUserLocation = YES;
MKCoordinateRegion region = { {0.0, 0.0}, {0.0,0.0}};
region.center.latitude = [self.myTextField.text floatValue];
region.center.longitude = [self.myTextField1.text floatValue];
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapview setRegion:region animated:YES];
MapPin *ann = [[MapPin alloc] init];
ann.title = #"Test";
ann.subtitle = #"test";
ann.coordinate = region.center;
[mapview addAnnotation:ann];
}

MapView does not show proper point

I have just started to use MapKit framework, and my first practice did not go well :)
Everything seems to be works fine except MapView showing somewhere in ocean and it did not find any place (ex: Land, island). I think latitude and longitude getting out of range somehow.
here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIBarButtonItem *zoomButton =
[[UIBarButtonItem alloc]
initWithTitle: #"Zoom-In"
style:UIBarButtonItemStyleBordered
target: self
action:#selector(zoomIn:)];
UIBarButtonItem *typeButton =
[[UIBarButtonItem alloc]
initWithTitle: #"Zoom-Out"
style:UIBarButtonItemStyleBordered
target: self
action:#selector(zoomOut:)];
NSArray *buttons = [[NSArray alloc]
initWithObjects:zoomButton, typeButton, nil];
barButtons.items = buttons;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
zoomLocation.latitude = 48.431638;
zoomLocation.longitude= 27.169436;
annotationPoint.coordinate = zoomLocation;
annotationPoint.title = #"My Company";
annotationPoint.subtitle = #"my Company Title";
//[self __mapView];
MKCoordinateRegion adjustedRegion = [__mapView regionThatFits:viewRegion];
[__mapView setRegion:adjustedRegion animated:YES];
[__mapView addAnnotation:annotationPoint];
}
You are setting zoomLocation coordinates after creating region. Try first to set zoomLocation
...
zoomLocation.latitude = 48.431638;
zoomLocation.longitude= 27.169436;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
...

Issue with zooming MapView using current location

I am showing a map view on front page of my application, I set the region and coords from current location using CLLocation.
When the application is run, a blue screen is being displayed on the mapview, when I open its child views and come back to main screen, the map show perfectly with location and zoom level.
Is there any thing wrong in the following code which causes the blue screen?
- (void)viewWillAppear:(BOOL)animated {
CLLocationManager *lm = [[CLLocationManager alloc] init];
lm.delegate = self;
lm.desiredAccuracy = kCLLocationAccuracyBest;
lm.distanceFilter = kCLDistanceFilterNone;
[lm startUpdatingLocation];
CLLocation *location = [lm location];
CLLocationCoordinate2D coord;
coord = [location coordinate];
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = coord.latitude; //39.281516;
zoomLocation.longitude= coord.longitude; //-76.580806;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.3*METERS_PER_MILE, 0.3*METERS_PER_MILE);
MKCoordinateRegion adjustedRegion = [self.mainMapView regionThatFits:viewRegion];
[self.mainMapView setRegion:adjustedRegion animated:YES];
}
My guess would be that you are seeing blue because the location is (0, 0) which is in the middle of the sea. Probably because the first time round, the location on the CLLocationManager is not set yet.
You should probably check if there's a location yet and if not then wait for a callback in the CLLocationManagerDelegate method locationManager:didUpdateToLocation:fromLocation: and then centre the map on the location you want.
Something like this:
...
#property (nonatomic, assign) BOOL needsCentering
...
#synthesize needsCentering = _needsCentering;
- (void)viewWillAppear:(BOOL)animated {
CLLocationManager *lm = [[CLLocationManager alloc] init];
lm.delegate = self;
lm.desiredAccuracy = kCLLocationAccuracyBest;
lm.distanceFilter = kCLDistanceFilterNone;
[lm startUpdatingLocation];
CLLocation *location = [lm location];
if (!location) {
_needsCentering = YES;
} else {
_needsCentering = NO;
CLLocationCoordinate2D coord;
coord = [location coordinate];
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = coord.latitude; //39.281516;
zoomLocation.longitude= coord.longitude; //-76.580806;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.3*METERS_PER_MILE, 0.3*METERS_PER_MILE);
MKCoordinateRegion adjustedRegion = [self.mainMapView regionThatFits:viewRegion];
[self.mainMapView setRegion:adjustedRegion animated:YES];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if (_needsCentering) {
_needsCentering = NO;
CLLocationCoordinate2D coord;
coord = [newLocation coordinate];
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = coord.latitude; //39.281516;
zoomLocation.longitude= coord.longitude; //-76.580806;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.3*METERS_PER_MILE, 0.3*METERS_PER_MILE);
MKCoordinateRegion adjustedRegion = [self.mainMapView regionThatFits:viewRegion];
[self.mainMapView setRegion:adjustedRegion animated:YES];
}
}
But also, you might want to take a look at showsUserLocation of MKMapView.

Two Annotation Pins at a time in MKMapView

I successfully implemented MKMapVIew and a single annotation on my Map. I am not able to represent two postitions simultaneously. I am using MKMapViewDelegate method :
mapView:viewForAnnotation:
Can someone look into this thing.
Thanks!
EDIT
- (void)viewDidLoad
{
[super viewDidLoad];
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 22.569722 ;
region.center.longitude = 88.369722;
region.span.longitudeDelta = 0.1f;
region.span.latitudeDelta = 0.1f;
MKCoordinateRegion anotherRegion = { {0.0, 0.0 }, { 0.0, 0.0 } };
anotherRegion.center.latitude = 28.38 ;
anotherRegion.center.longitude = 77.12;
anotherRegion.span.longitudeDelta = 90.0f;
anotherRegion.span.latitudeDelta = 90.0f;
[mapView setRegion:region animated:YES];
[mapView setDelegate:self];
DisplayMap *ann = [[DisplayMap alloc] init];
ann.title = #" Kolkata";
ann.subtitle = #"Mahatma Gandhi Road";
ann.coordinate = region.center;
[mapView addAnnotation:ann];
DisplayAnotherMap *annMap = [[DisplayAnotherMap alloc] init];
annMap.title = #" New Delhi";
annMap.subtitle = #"Shahdara";
annMap.coordinate = anotherRegion.center;
[mapView addAnnotations:[NSArray arrayWithObjects:annMap,ann,nil]];
}
This will fulfill the requirement for you! ...:)
The method mapView:viewForAnnotation: is just for the view of a the annotations, eg. the colour of the pin and the title label etc. If you want to show multiple annotations you should alloc and init the all with the required positions. For example, you could create a .plist with all the coordinates and the in for cycle just add them.
EDIT
This is a sample code, taken from somewhere. You must alloc and init all anotations.
-(void)loadDummyPlaces{
srand((unsigned)time(0));
NSMutableArray *tempPlaces=[[NSMutableArray alloc] initWithCapacity:0];
for (int i=0; i<1000; i++) {
MyPlace *place=[[MyPlace alloc] initWithCoordinate:CLLocationCoordinate2DMake([self RandomFloatStart:42.0 end:47.0],[self RandomFloatStart:14.0 end:19.0])];
[place setCurrentTitle:[NSString stringWithFormat:#"Place %d title",i]];
[place setCurrentSubTitle:[NSString stringWithFormat:#"Place %d subtitle",i]];
[place addPlace:place];
[tempPlaces addObject:place];
[place release];
}
places=[[NSArray alloc] initWithArray:tempPlaces];
[tempPlaces release];
}

Resources