How to add callout into individual annotation in map view - ios

I'm trying to set each annotation with different callout detail info.
Currently when I click on any annotation location, it shows all the callout info.
#import "AnnotationViewController.h"
#import "Annotation.h"
#implementation AnnotationViewController
#synthesize mapView;
-(void)viewDidLoad {
[super viewDidLoad];
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
[mapView setDelegate:self];
MKCoordinateRegion TT = { {0.0, 0.0} , {0.0, 0.0} };
TT.center.latitude = 43.65343;
TT.center.longitude = -79.396311;
TT.span.longitudeDelta = 0.02f;
TT.span.latitudeDelta = 0.02f;
[mapView setRegion:TT animated:YES];
Annotation *ann1 = [[Annotation alloc] init];
ann1.title = #"Annotation 01";
ann1.subtitle = #"Message 01";
ann1.coordinate = TT.center;
[mapView addAnnotation:ann1];
MKCoordinateRegion TTY = { {0.0, 0.0} , {0.0, 0.0} };
TTY.center.latitude = 43.76919;
TTY.center.longitude = -79.41245;
TTY.span.longitudeDelta = 0.02f;
TTY.span.latitudeDelta = 0.02f;
[mapView setRegion:TTY animated:YES];
Annotation *ann2 = [[Annotation alloc] init];
ann2.title = #"Annotation 02";
ann2.subtitle = #"Message 02";
ann2.coordinate = TTY.center;
[mapView addAnnotation:ann2];
}
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:id<MKAnnotation>)annotation
{
MKPinAnnotationView *view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"pin"];
view.pinColor = MKPinAnnotationColorPurple;
view.enabled = YES;
view.animatesDrop = YES;
view.canShowCallout = YES;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"GPSicon.png"]];
view.leftCalloutAccessoryView = imageView;
view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return view;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSString *msg = [#"Location 01" stringByAppendingFormat:#"Opening 01"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Plaza 01" message:msg delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
NSString *msg02 = [#"Location 02" stringByAppendingFormat:#"Opening 02"];
UIAlertView *alert02 = [[UIAlertView alloc] initWithTitle:#"Plaza 02" message:msg02 delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert02 show];
}
-(void)button:(id)sender {
NSLog(#"Button action");
}
- (void)dealloc
{
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
end
So this is what's happening: When I click on "Annotation 01" the bubble will appear. But when I click on the callout "detail icon", it will popup both Location 01 & Location 2 titles.
Thanks Anna for you help. But I still get 2 callouts when i check one annotation. This is the code now..
#import "AnnotationViewController.h"
#import "Annotation.h"
#implementation AnnotationViewController
#synthesize mapView;
-(void)viewDidLoad {
[super viewDidLoad];
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
[mapView setDelegate:self];
MKCoordinateRegion TT = { {0.0, 0.0} , {0.0, 0.0} };
TT.center.latitude = 43.65343;
TT.center.longitude = -79.396311;
TT.span.longitudeDelta = 0.02f;
TT.span.latitudeDelta = 0.02f;
[mapView setRegion:TT animated:YES];
Annotation *ann1 = [[Annotation alloc] init];
ann1.title = #"Annotation 01";
ann1.subtitle = #"Message 01";
ann1.coordinate = TT.center;
[mapView addAnnotation:ann1];
MKCoordinateRegion TTY = { {0.0, 0.0} , {0.0, 0.0} };
TTY.center.latitude = 43.76919;
TTY.center.longitude = -79.41245;
TTY.span.longitudeDelta = 0.02f;
TTY.span.latitudeDelta = 0.02f;
[mapView setRegion:TTY animated:YES];
Annotation *ann2 = [[Annotation alloc] init];
ann2.title = #"Annotation 02";
ann2.subtitle = #"Message 02";
ann2.coordinate = TTY.center;
[mapView addAnnotation:ann2];
}
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:id<MKAnnotation>)annotation
{
MKPinAnnotationView *view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"pin"];
view.pinColor = MKPinAnnotationColorPurple;
view.enabled = YES;
view.animatesDrop = YES;
view.canShowCallout = YES;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"GPSicon.png"]];
view.leftCalloutAccessoryView = imageView;
view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return view;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
//first make sure the annotation is our custom class...
if ([view.annotation isKindOfClass:[Annotation class]])
{
//cast the object to our custom class...
Annotation *ann1 = (Annotation *)view.annotation;
//show one alert view with title set to annotation's title
//and message set to annotation's subtitle...
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:ann1.title
message:ann1.subtitle
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
//first make sure the annotation is our custom class...
if ([view.annotation isKindOfClass:[Annotation class]])
{
//cast the object to our custom class...
Annotation *ann2 = (Annotation *)view.annotation;
//show one alert view with title set to annotation's title
//and message set to annotation's subtitle...
UIAlertView *alert2 = [[UIAlertView alloc]
initWithTitle:ann2.title
message:ann2.subtitle
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
-(void)button:(id)sender {
NSLog(#"Button action");
}
- (void)dealloc
{
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
end
My Annotation.m file code
#import "Annotation.h"
#implementation Annotation
#synthesize coordinate, title, subtitle;
-(void)dealloc {
}
#end
My Annotation.h file code
#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>
#interface Annotation : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
#property(nonatomic, assign) CLLocationCoordinate2D coordinate;
#property(nonatomic, copy) NSString *title;
#property(nonatomic, copy) NSString *subtitle;
#end
Do i have to make alot of h/m files for each Annotation to make the callout different? im just stomp at this point. any guesses would be great, thanks!

In calloutAccessoryControlTapped, the code is showing two alert views with hard-coded text for the title and message one after the other. So that's what you get obviously.
In the calloutAccessoryControlTapped method, the annotation object whose callout button was tapped is available via view.annotation.
The view parameter is the MKAnnotationView which has an annotation property that points to its related annotation.
So change the code to show only one alert view and with the title and message based on the properties of the annotation object.
It's also a good idea to check the class of the annotation to make sure it's the type of annotation you're expecting.
For example:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
//first make sure the annotation is our custom class...
if ([view.annotation isKindOfClass:[Annotation class]])
{
//cast the object to our custom class...
Annotation *ann = (Annotation *)view.annotation;
//show one alert view with title set to annotation's title
//and message set to annotation's subtitle...
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:ann.title
message:ann.subtitle
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}

Related

How to code for MKMapKitView Which shows another container view when user click on pin? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want this type of output for ios Application. I hadn't any idea how to do it. Please let me know if anyone know how to do it.
EDIT
I had updated my code as per answer, but still it was not working. I can't unserstand that how UIView display and what will be its size ?
#import <UIKit/UIKit.h>
#interface MyView : UIView
#end
#import "MyView.h"
#implementation MyView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
// Initialization code
// initilize all your UIView components
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(20,30, 200, 44)];
label1.text = #"i am label 1";
[self addSubview:label1]; //add label1 to your custom view
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(20,80, 200, 44)];
label2.text = #"i am label 2";
[self addSubview:label2]; //add label2 to your custom view
}
return self;
}
=================================================
#import <MapKit/MapKit.h>
#import "MyView.h"
#interface MyCustomView : MKAnnotationView
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event;
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event;
- (void)setShowCustomCallout:(BOOL)showCustomCallout animated:(BOOL)animated;
- (void)setShowCustomCallout:(BOOL)showCustomCallout
;
#property(strong, nonatomic) MyView *calloutView ;
#end
#import "MyCustomView.h"
#implementation MyCustomView
- (void)setShowCustomCallout:(BOOL)showCustomCallout
{
[self setShowCustomCallout:showCustomCallout animated:NO];
}
- (void)setShowCustomCallout:(BOOL)showCustomCallout animated:(BOOL)animated
{
//if (showCustomCallout == showCustomCallout) return;
showCustomCallout = showCustomCallout;
void (^animationBlock)(void) = nil;
void (^completionBlock)(BOOL finished) = nil;
if (showCustomCallout)
{
self.calloutView.alpha = 0.0f;
animationBlock = ^{
self.calloutView.alpha = 1.0f;
[self addSubview:self.calloutView];
};
} else {
animationBlock = ^{ self.calloutView.alpha = 0.0f; };
completionBlock = ^(BOOL finished) { [self.calloutView removeFromSuperview]; };
}
if (animated) {
[UIView animateWithDuration:0.2f animations:animationBlock completion:completionBlock];
} else {
animationBlock();
completionBlock(YES);
}
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView *view = [super hitTest:point withEvent:event];
if ([view isKindOfClass:_calloutView.class]) {
return nil; // todo: add a new delegate method to the map protocol to handle callout taps
} else {
return view;
}
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event
{
CGRect rect = self.bounds;
BOOL isInside = CGRectContainsPoint(rect, point);
if(!isInside)
{
for (UIView *view in self.subviews)
{
isInside = CGRectContainsPoint(view.frame, point);
if(isInside)
break;
}
}
return isInside;
}
=================================================
#import "ViewController.h"
#interface ViewController ()
{
CLLocationManager *locationManager;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; //whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
[locationManager requestWhenInUseAuthorization]; // Add This Line
[locationManager startUpdatingLocation];
_mapView.showsUserLocation = YES;
_mapView.delegate = self;
CLLocationCoordinate2D annotationCoord;
annotationCoord.latitude = 23.041261;
annotationCoord.longitude = 72.513892;
_mapView.region = MKCoordinateRegionMakeWithDistance(annotationCoord, 800, 800);
// MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:MKCoordinateRegionMakeWithDistance(annotationCoord, 800, 800)];
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = annotationCoord;
annotationPoint.title = #"I am here";
annotationPoint.subtitle = #"Microsoft's headquarters";
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView1 viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:#"pin"];
annView.pinColor = MKPinAnnotationColorGreen;
return annView;
}
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
[((MyCustomView *)view) setShowCustomCallout:NO animated:YES];
}
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
MyCustomView *annotationView = [[MyCustomView alloc]init];
[annotationView setShowCustomCallout:YES animated:YES];
}
You have to make a custom view to make a custom callout on mapview.you have to make a class that contain a custom view which will be on pin tapped . Here is the sample code on github.
https://gist.github.com/ShadoFlameX/7495098

About the Tab bar button with NSMutableArray annotation in MapKit

I have created 4 type of annotation group. In the map UI, I also added a tab bar on the bottom as my button.
My tab bar is used to filter out the annotation in MapKit.
For example... I have 4 group of annotation and 4 tab bar item.
When I clicked tab bar item 1, it show only 1 group of annotation in MapKit, other group of annotation will hide/remove in MapKit but I failed to achieve this kind of work.
My code:
in MapViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#interface MapViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate , UITabBarDelegate>{
IBOutlet UITabBar *tabBar;
}
#property (weak, nonatomic) IBOutlet UIBarButtonItem *sidebarButton;
#property (weak, nonatomic) IBOutlet MKMapView *mapView;
#property (strong, nonatomic) CLLocationManager *locationManager;
#end
my mapViewController.m:
#import "MapViewController.h"
#import "SWRevealViewController.h"
#import "Annotation.h"
#interface MapViewController ()<CLLocationManagerDelegate>
#end
//set desitination of map
#define PENANG_LATI 5.419501;
#define PENANG_LONG 100.323264;
//shop
#define SHOP_LATI 5.419501;
#define SHOP_LONG 100.323264;
//cafe
#define CAFE_LATI 5.419917;
#define CAFE_LONG 100.322969;
//food
#define FOOD_LATI 5.419746;
#define FOOD_LONG 100.322610;
//mural
#define MURAL_LATI 5.419786;
#define MURAL_LONG 100.322510;
#define THE_SPAN 0.005f;
#implementation MapViewController
#synthesize mapView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//tabBar
tabBar.delegate = self;
MKCoordinateRegion myRegion;
//Center
CLLocationCoordinate2D center;
center.latitude = PENANG_LATI;
center.longitude = PENANG_LONG;
//SPAN
MKCoordinateSpan span;
span.latitudeDelta = THE_SPAN;
span.longitudeDelta = THE_SPAN;
myRegion.center = center;
myRegion.span = span;
//set map
[mapView setRegion:myRegion animated:YES];
SWRevealViewController *revealViewController = self.revealViewController;
if ( revealViewController )
{
[self.sidebarButton setTarget: self.revealViewController];
[self.sidebarButton setAction: #selector( revealToggle: )];
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}
/*
//create coordinate
CLLocationCoordinate2D penangLocation;
penangLocation.latitude = PENANG_LATI;
penangLocation.longitude = PENANG_LONG;
Annotation * myAnnotation = [Annotation alloc];
myAnnotation.coordinate = penangLocation;
myAnnotation.title = #"THE ONE ACADEMY PENANG";
myAnnotation.subtitle = #"HELLO!! I STUDY HERE";
[self.mapView addAnnotation:myAnnotation];
*/
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
//my group of annotation location
NSMutableArray * myShop = [[NSMutableArray alloc] init];
NSMutableArray * myCafe = [[NSMutableArray alloc] init];
NSMutableArray * myFood = [[NSMutableArray alloc] init];
NSMutableArray * myMural = [[NSMutableArray alloc] init];
CLLocationCoordinate2D penangLocation;
Annotation * myShopAnnotation;
Annotation * myCafeAnnotation;
Annotation * myFoodAnnotation;
Annotation * myMuralAnnotation;
//shop location
myShopAnnotation = [[Annotation alloc] init];
penangLocation.latitude = SHOP_LATI;
penangLocation.longitude = SHOP_LONG;
myShopAnnotation.coordinate = penangLocation;
myShopAnnotation.title = #"Shop";
myShopAnnotation.subtitle = #"I study here";
[myShop addObject:myShopAnnotation];
//cafe location
myCafeAnnotation = [[Annotation alloc] init];
penangLocation.latitude = CAFE_LATI;
penangLocation.longitude = CAFE_LONG;
myCafeAnnotation.coordinate = penangLocation;
myCafeAnnotation.title = #"Cafe";
myCafeAnnotation.subtitle = #"I paid here";
[myCafe addObject:myCafeAnnotation];
//food location
myFoodAnnotation = [[Annotation alloc] init];
penangLocation.latitude = FOOD_LATI;
penangLocation.longitude = FOOD_LONG;
myFoodAnnotation.coordinate = penangLocation;
myFoodAnnotation.title = #"Food";
myFoodAnnotation.subtitle = #"I walk here";
[myFood addObject:myFoodAnnotation];
//Mural location
myMuralAnnotation = [[Annotation alloc] init];
penangLocation.latitude = MURAL_LATI;
penangLocation.longitude = MURAL_LONG;
myMuralAnnotation.coordinate = penangLocation;
myMuralAnnotation.title = #"Mural";
myMuralAnnotation.subtitle = #"I walk here";
[myMural addObject:myMuralAnnotation];
if(item.tag == 1)
{
//show and hide annotation
NSLog(#"shop");
[mapView addAnnotations:myShop];
[mapView removeAnnotations:myCafe];
[mapView removeAnnotations:myFood];
[mapView removeAnnotations:myMural];
}
if(item.tag == 2)
{
//show and hide annotation
NSLog(#"cafe");
[mapView removeAnnotations:myShop];
[mapView addAnnotations:myCafe];
[mapView removeAnnotations:myFood];
[mapView removeAnnotations:myMural];
}
if(item.tag == 3)
{
//show and hide annotation
NSLog(#"food");
[mapView removeAnnotations:myShop];
[mapView removeAnnotations:myCafe];
[mapView removeAnnotations:myFood];
[mapView addAnnotations:myMural];
}
if(item.tag == 4)
{
//show and hide annotation
NSLog(#"mural");
[mapView removeAnnotations:myShop];
[mapView removeAnnotations:myCafe];
[mapView removeAnnotations:myFood];
[mapView addAnnotations:myMural];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
When removeAnnotations: is called like this:
[mapView removeAnnotations:myCafe];
the map view looks for the exact objects (using pointer comparison) in myCafe that are already on the map.
The map view here does not match up annotations using their coordinate, title, etc.
Since the code creates new instances of each annotation every time a selection is made, the new instances are not found on the map and so removeAnnotations: does nothing.
Instead, in your case, you could just tell the map view to remove all existing annotations and then add the new annotations based on the current selection.
Instead of this:
[mapView addAnnotations:myShop];
[mapView removeAnnotations:myCafe];
[mapView removeAnnotations:myFood];
[mapView removeAnnotations:myMural];
do this:
[mapView removeAnnotations:mapView.annotations]; //remove all
[mapView addAnnotations:myShop]; //then add new
The code could be simplified as well by using a switch statement and in each case, only create the annotations needed for the selection (you don't need to create all annotations every time).

iOS Google Maps textField not Responding

#import "MyLocationViewController.h"
#import <GoogleMaps/GoogleMaps.h>
#interface MyLocationViewController ()
#end
#implementation MyLocationViewController {
GMSMapView *mapView_;
CLLocationManager *locationManager;
}
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
NSLog(#"Current identifier: %#", [[NSBundle mainBundle] bundleIdentifier]);
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:151.20
zoom:6];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = YES;
self.view = mapView_;
//Add text field
UITextField *textField = [[UITextField alloc] init];
textField.frame = CGRectMake(mapView_.bounds.size.width - 290, mapView_.bounds.size.height - 48, 180, 40);
textField.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = #"enToi UHMUH Bar Heya";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.delegate = self;
[mapView_ addSubview:textField];
[mapView_ resignFirstResponder];
[mapView_ bringSubviewToFront:textField];
[textField becomeFirstResponder];
//Add Send Chat Button to UI
UIButton *sendButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
sendButton.frame = CGRectMake(mapView_.bounds.size.width - 100, mapView_.bounds.size.height - 48, 90, 40);
sendButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
[sendButton setTitle:#"Send" forState:UIControlStateNormal];
[mapView_ addSubview:sendButton];
// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
marker.title = #"Sydney";
marker.map = mapView_;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"didFailWithError: %#", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:#"Error"
message:#"Failed to get your location!"
delegate:nil
cancelButtonTitle:#"OKAY"
otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *currentLocation = [locations lastObject];
NSLog(#"didUpdateLocations: %#", currentLocation);
if (currentLocation != nil) {
GMSCameraPosition *newSpot = [GMSCameraPosition cameraWithLatitude:currentLocation.coordinate.latitude
longitude:currentLocation.coordinate.longitude
zoom:6];
[mapView_ setCamera:newSpot];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Here's my header file:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface MyLocationViewController : UIViewController <CLLocationManagerDelegate, UITextFieldDelegate>
#property (nonatomic, retain) UITextField *textField;
#end
I am not using Interface Builder. When the app loads I can see my textfield and button. The button does its default toggle behavior when touched. The text field is not editable - can't bring up the keyboard by clicking it, cant type into, etc.
I did run someone's method to determine if a frame is out of its parent view's bounds and it came back as saying it was out of bounds - but I'm not quite sure how to resolve this.
The googleMapView has a BlockingGestureRecognizer that blocks all user input. don't add the textfield to the map OR remove the blocker:
// Remove the GMSBlockingGestureRecognizer of the GMSMapView.
+ (void)removeGMSBlockingGestureRecognizerFromMapView:(GMSMapView *)mapView
{
if([mapView.settings respondsToSelector:#selector(consumesGesturesInView)]) {
mapView.settings.consumesGesturesInView = NO;
}
else {
for (id gestureRecognizer in mapView.gestureRecognizers)
{
if (![gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]])
{
[mapView removeGestureRecognizer:gestureRecognizer];
}
}
}
}
Add this if you need touch event enable in uitextfield
for (id gestureRecognizer in mapView_.gestureRecognizers)
{
NSLog(#"mapview recognizer %#",gestureRecognizer);
if (![gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]])
{
[mapView_ removeGestureRecognizer:gestureRecognizer];
}
}
Add Your UITextfield or UISearchbar in to subview of Mapview_
UISearchBar *search;
search = [[UISearchBar alloc] init];
[search setTintColor:[UIColor colorWithRed:233.0/255.0
green:233.0/255.0
blue:233.0/255.0
alpha:1.0]];
search.frame = CGRectMake(50,20,self.view.frame.size.width-70,32);
search.delegate = self;
search.placeholder = #"MapView";
[mapView_ addSubview:search];
Add Following method to your .m file
// Remove the GMSBlockingGestureRecognizer of the GMSMapView.
+ (void)removeGMSBlockingGestureRecognizerFromMapView:(GMSMapView *)mapView
{
for (id gestureRecognizer in mapView.gestureRecognizers)
{
NSLog(#"mapview recognizer %#",gestureRecognizer);
if (![gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]])
{
[mapView removeGestureRecognizer:gestureRecognizer];
}
}
}
Call This Method from your view will appear
- (void)viewWillAppear:(BOOL)animated {
[ViewController removeGMSBlockingGestureRecognizerFromMapView:mapView_];
}
Now it will work , i got tested with this code only.
Try
[self.view addSubview: textField];
Instead of
[mapView_ addSubview:textField];
This may help you.

Show User's current location on map

I am trying to display user's current location on the map but for some reason, does not takes my default coordinates.
I have a MKMapView class that called inside a UIViewController.
MapView.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#interface MapView : MKMapView <MKMapViewDelegate, CLLocationManagerDelegate>{
CLLocationManager *locationManager;
}
#end
MapView.m
#import "MapView.h"
#interface MapView ()
#end
#implementation MapView
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
self.delegate = self;
[self addAnnotation:[self userLocation]];
[locationManager startUpdatingLocation];
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"didFailWithError: %#", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:#"Error" message:#"Failed to Get Your Location" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
NSLog(#"didUpdateUserLocation");
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 250, 250);
[self setRegion:[self regionThatFits:region] animated:YES];
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = userLocation.coordinate;
point.title = #"Where am I?";
point.subtitle = #"I'm here!!!";
[self addAnnotation:point];
}
- (MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
NSLog(#"viewForAnnotation");
static NSString *identifier = #"MyAnnotation";
if ([annotation isKindOfClass:[MKUserLocation class]]) {
NSLog(#"Is the user %f, %f", [annotation coordinate].latitude, [annotation coordinate].longitude);
return nil;
}
return nil;
}
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views{
NSLog(#"didAddAnnotationViews");
for (MKAnnotationView *view in views){
if ([[view annotation] isKindOfClass:[MKUserLocation class]]){
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([[view annotation] coordinate] , 250, 250);
[mapView setRegion:region animated:YES];
}
}
}
#end
I am getting the output below once access the map.
viewForAnnotation
Is the user 0.000000, 0.000000
didAddAnnotationViews
I can't understand why my didUpdateUserLocation: never get called even if re-submit custom coordinates.
What I am missing here?
You're using a CLLocationManager and setting it's delegate, but your delegate methods are all methods of MKMapViewDelegate.
You can either set the mapview's delegate, or use the locationManager:didUpdateLocations: CLLocationManagerDelegate method.
If it is important to you that the user have the option to turn his location on and off on the map then Nevan King is correct. However, if you are OK with continually showing the user location then no scripting is necessary. Simply click on your mapView in the Storyboard and on the right (in attributes, I think) check the "shows user location" box.

Open Detail View from Annotation

I have a map view with several annotations. Each annotation has a button that takes the user to a detail view for that annotation. The detail view currently has two labels, one for the title from the annotation and the other for the subtitle of the annotation, and a "Get Directions" button. I want the title label to be populated with the title from the annotation and the subtitle label to be populated with the subtitle from the annotation. I want the Get Directions button to take the user into the Maps app and give them directions to the address from their current location. I'd also like to add website and phone number to the detail view but I'm not sure how to populate those fields either. The detail view is called AnnotationDetailView but I haven't done anything with it besides add the button and labels in IB. I'm just not sure how to populate the labels and get the button doing what I want. Any help would be great.
Here is my code:
//
// RSFM.m
#import "RSFM.h"
#import "AnnotationDetailView.h"
#interface RSFM ()
#end
#implementation RSFM
{
}
#synthesize centerCoordinate, coordinate, title, subtitle;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
self.title = NSLocalizedString(#"Farm Markets", #"Farm Markets");
// Create location manager object
locationManager = [[CLLocationManager alloc]init];
[locationManager setDelegate:self];
// And we want it to be as accurate as possible regardless of how much time/power it takes
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
// Tell our manager to start looking for its location immediately
// [locationManager startUpdatingLocation];
}
return self;
}
/*
- (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate
{
centerCoordinate = CLLocationCoordinate2DMake(37.7885, 85.3279);
}
*/
- (void)findLocation
{
[locationManager startUpdatingLocation];
[activityIndicator startAnimating];
// [locationTitleField setHidden:YES];
[locationManager stopUpdatingLocation];
}
- (void)foundLocation:(CLLocation *)loc
{
CLLocationCoordinate2D coord = [loc coordinate];
// Create an instance of BNRMapPoint with the current data
// BNRMapPoint *mp = [[BNRMapPoint alloc]initWithCoordinate:coord title:[locationTitleField text]];
// Add it to the map view
// [worldView addAnnotation:mp];
// Zoom the region to this location
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 700000, 700000);
[worldView setRegion:region animated:YES];
// Reset the UI
// [locationTitleField setText:#""];
[activityIndicator stopAnimating];
// [locationTitleField setHidden:NO];
[locationManager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"%#", newLocation);
// How many seconds ago was this new location created?
NSTimeInterval t = [[newLocation timestamp]timeIntervalSinceNow];
// CLLocationManagers will return the last found location of the device first, you don't want that data in this case.
// If this location was made more than 3 minutes ago, ignore it.
if (t < -180)
{
// this is cached data, you don't want it, keep looking
return;
}
[self foundLocation:newLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"Could not find location: %#", error);
}
- (void)dealloc
{
// Tell the location manager to stop sending us messages
[locationManager setDelegate:nil];
}
- (IBAction)showDetails:(id)sender
{
AnnotationDetailView *detail = [[AnnotationDetailView alloc] initWithNibName:nil bundle:nil];
detail.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController pushViewController:detail animated:YES];
}
- (MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
// If it's the user location, return nil
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// Try to dequeue an existing pin view first
static NSString *annotationIdentifier = #"AnnotationIdentifier";
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[rightButton addTarget:self action:#selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;
return pinView;
}
- (void)viewDidLoad
{
[locationManager startUpdatingLocation];
[worldView setShowsUserLocation:YES];
[locationManager stopUpdatingLocation];
NSMutableArray *marketLocations = [[NSMutableArray alloc]init];
NSMutableArray *lat = [[NSMutableArray alloc]initWithObjects:#"37.7867266", #"37.0703517", #"37.1610806", #"37.318367", #"37.3559204", #"37.4154066", #"37.4757622", #"37.7450252", #"37.6318978", #"37.0716803", nil];
NSMutableArray *lon = [[NSMutableArray alloc]initWithObjects:#"-87.608209", #"-88.1237899", #"-87.9148629", #"-87.5074402", #"-87.5448032", #"-87.8003148", #"-87.9515986", #"-87.9061638", #"-87.1148574", #"-87.3008418", nil];
NSMutableArray *title1 = [[NSMutableArray alloc]initWithObjects:#"Cates Farm", #"Broadbent B & B Foods", #"Cayce's Pumpkin Patch", #"Metcalfe Landscaping", #"Brumfield Farm Market", #"Dogwood Valley Farm", #"Country Fresh Meats & Farmers Market", #"Jim David Meats", #"Trunnell's Farm Market", #"Lovell's Orchard & Farm Market", nil];
NSMutableArray *subtitle1 = [[NSMutableArray alloc]initWithObjects:#"Hwy 425 Henderson, KY 42420", #"257 Mary Blue Road Kuttawa, KY 42055", #"153 Farmersville Road Princeton, KY 42445", #"410 Princeton Road Madisonville, KY 42431", #"3320 Nebo Road Madisonville, KY 42431", #"4551 State Route 109N Clay, KY 42404", #"9355 US Hwy 60 W Sturgis, KY 42459",#"350 T. Frank Wathen Rd. Uniontown, KY 42461", #"9255 Hwy 431 Utica, KY 42376", #"22850 Coal Creek Road Hopkinsville, KY 42240", nil];
CLLocationCoordinate2D location;
MKPointAnnotation *marketAnnotation;
for (int x = 0; x < [lat count]; x++)
{
marketAnnotation = [[MKPointAnnotation alloc]init];
location.latitude = [[lat objectAtIndex:x]floatValue];
location.longitude = [[lon objectAtIndex:x]floatValue];
marketAnnotation.coordinate = location;
marketAnnotation.title = [title1 objectAtIndex:x];
marketAnnotation.subtitle = [subtitle1 objectAtIndex:x];
[marketLocations addObject:marketAnnotation];
}
[worldView addAnnotations:marketLocations];
/*
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = CLLocationCoordinate2DMake(37.7867266, -87.608209);
point.title = #"Cates Farm";
point.subtitle = #"Hwy 425 Henderson, KY 42420";
[worldView addAnnotation:point];
*/
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
CLLocationCoordinate2D loc = [userLocation coordinate];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 700000, 700000);
[worldView setRegion:region animated:YES];
[locationManager stopUpdatingLocation];
locationManager.delegate = nil;
}
- (IBAction)selectSegmentControl
{
int segmentTouched = [mapVarieties selectedSegmentIndex];
NSString *segmentName = [mapVarieties titleForSegmentAtIndex:segmentTouched];
if ([segmentName isEqualToString:#"Street"])
{
[worldView setMapType:MKMapTypeStandard];
}
if ([segmentName isEqualToString:#"Satellite"])
{
[worldView setMapType:MKMapTypeSatellite];
}
if ([segmentName isEqualToString:#"Hybrid"])
{
[worldView setMapType:MKMapTypeHybrid];
}
}
#end
Here is where I show the detail view
- (IBAction)showDetails:(id)sender
{
AnnotationDetailView *detail = [[AnnotationDetailView alloc] initWithNibName:nil bundle:nil];
detail.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
NSLog(#"%#", marketAnnotation.title);
detail.ti.text = marketAnnotation.title;
[self.navigationController pushViewController:detail animated:YES];
}
and the AnnotationDetailView
//
// AnnotationDetailView.m
#import "AnnotationDetailView.h"
#interface AnnotationDetailView ()
#end
#implementation AnnotationDetailView
#synthesize ti;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// [title setText:marketAnnotation.title];
// [subtitle setText:marketAnnotation.subtitle];
ti = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, 281, 21)];
[self.view addSubview:ti];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I'm having a couple of issues with this. One, the label is not showing up. Two, I have an NSLog statement in my showDetails method to show me what marketAnnotations is. It is showing the title of the last annotation added.
You can add extra parameters to your MKAnnotation class. For example this Annotation class uses 3 extra strings to hold information:
// MyAnnotation.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface MyAnnotation : NSObject <MKAnnotation>
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
#property (nonatomic, copy, readonly) NSString *title;
#property (nonatomic, copy, readonly) NSString *subtitle;
#property (nonatomic, copy, readonly) NSString *myString1;
#property (nonatomic, copy, readonly) NSString *myString2;
#property (nonatomic, copy, readonly) NSString *myString3;
-(id)initWithCoordinates:(CLLocationCoordinate2D) paramCoordinates
title:(NSString *) paramTitle
subTitle:(NSString *) paramSubTitle
myString1:(NSString *) paramMyString1
myString2:(NSString *) paramMyString2
myString3:(NSString *) paramMyString3;
#end
// MyAnnotation.m
#import "MyAnnotation.h"
#implementation MyAnnotation
-(id)initWithCoordinates:(CLLocationCoordinate2D)paramCoordinates
title:(NSString *)paramTitle
subTitle:(NSString *)paramSubTitle
myString1:(NSString *)paramMyString1
myString2:(NSString *)paramMyString2
myString3:(NSString *)paramMyString1
{
self = [super init];
if(self != nil)
{
_coordinate = paramCoordinates;
_title = paramTitle;
_subtitle = paramSubTitle;
_myString1 = paramMyString1;
_myString2 = paramMyString2;
_myString3 = paramMyString3;
}
return (self);
}
#end
When the user taps a pin, you can do this to pass information from the Annotation to the next View Controller:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
if ([(UIButton*)control buttonType] == UIButtonTypeDetailDisclosure)
{
YourViewController *vc = [[YourViewController alloc] init];
[vc setTitleString:[(MyAnnotation *)view.annotation title]];
[vc setSubTitleString:[(MyAnnotation *)view.annotation subTitle]];
[vc setPassedString1:[(MyAnnotation *)view.annotation myString1]];
[vc setPassedString2:[(MyAnnotation *)view.annotation myString2]];
[vc setPassedString3:[(MyAnnotation *)view.annotation myString3]];
[[self navigationController] pushViewController:vc animated:YES];
}
}
In your next View Controller (vc), you can modify the button text like this:
[myButton setTitle:myPassedString1 forState:UIControlStateNormal];

Resources