#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.
Related
I had created a UIMapView and set delegate, but it's not called delegate of mapview.
#import <MapKit/MapKit.h>
#interface MapViewController : UIView <MKMapViewDelegate>
- (id)initWith_Latitude:(NSString *)Latitude Longitude:(NSString *)Longitude;
- (id)initWith_Latitude:(NSString *)Latitude Longitude:(NSString *)Longitude
{
CGRect frame = CGRectMake(0, 0, 200, 200);
self = [super initWithFrame:frame];
if (self) {
// Initialization code
mapView = [[MKMapView alloc] initWithFrame:frame];
MKCoordinateRegion mapRegion;
[mapView setUserInteractionEnabled:YES];
[mapView setDelegate:self];
mapRegion.center.latitude = [Latitude doubleValue];
mapRegion.center.longitude = [Longitude doubleValue];
mapRegion.span.latitudeDelta = 0.005;
mapRegion.span.longitudeDelta = 0.005;
mapView.region = mapRegion;
[self addSubview:mapView];
}
return self;
}
delegate is not called:
- (void)mapViewDidFinishRenderingMap:(MKMapView *)mapView2 fullyRendered:(BOOL)fullyRendered{
NSLog(#"abc...");
}
How to I can resolve this problem, please help me.
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
I am using google maps api in my app. I've got two buttons in my app. the first button adds a marker (pin) in my map.
Now I want the second button to move the added pin to the center of the page horizontally and make it move to 25% off the top of the page. I want the camera (The area that the user is viewing) to move it too.
this is my code:
#implementation ViewController
{
double latitudes;
double longitudes;
CLLocationManager *locationManager;
GMSMapView *mapView_;
}
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
[self GetMyLocation];
UIButton *pinButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
pinButton.frame = CGRectMake(self.view.frame.size.width-80,self.view.frame.size.height-80, 60, 60);
[pinButton setTitle:#"Self" forState:UIControlStateNormal];
[pinButton setBackgroundColor:[UIColor whiteColor]];
[pinButton addTarget:self action:#selector(ShowMyLocation:) forControlEvents:UIControlEventTouchUpInside];
UIButton *add = [UIButton buttonWithType:UIButtonTypeRoundedRect];
add.frame = CGRectMake(20,self.view.frame.size.height-80, 60, 60);
[add setTitle:#"add" forState:UIControlStateNormal];
[add setBackgroundColor:[UIColor whiteColor]];
[add addTarget:self action:#selector(Move:) forControlEvents:UIControlEventTouchUpInside];
// Create a GMSCameraPosition that tells the map to display the
// nokte mohem ine ke baadan ye logitude & latitude default ezafe kon chon shaiad tuie ye sharaiete tokhmi ke hamid esrar dare va ye kasi mariz bood dastresie GPS ro ghat kard
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:latitudes longitude:longitudes zoom:14];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = YES;
[mapView_ setMapType:kGMSTypeNormal];
self.view = mapView_;
[mapView_ addSubview:pinButton];
[mapView_ addSubview:add];
}
- (IBAction)Move:(id)sender{
//move marker place
}
- (IBAction)ShowMyLocation:(id)sender{
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(coor.latitude,coor.longitude);
marker.title = #"I'm Here";
marker.snippet = #"Rahnova Co.";
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.map = mapView_;
}
- (void) GetMyLocation{
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
#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:#"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
longitudes = currentLocation.coordinate.longitude;
latitudes = currentLocation.coordinate.latitude;
}
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:latitudes longitude:longitudes zoom:14];
[mapView_ animateToCameraPosition:camera];
}
Try this code:-
On the header of your .M implementation file, you have to implement the GMSMapViewDelegate.
#interface YourViewController ()<GMSMapViewDelegate>
Inside viewDidLoad, you have to set the mapView delegate to self.
mapView_.delegate=self;
For the delegate method:-
-(BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker{
[mapView animateToLocation:marker.position];
return YES;
}
It shall help you to go to the marker position when you tap on it.
You can move markers with animation. Here is code
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
CATransaction.begin()
CATransaction.setValue(2.0, forKey: kCATransactionAnimationDuration)
CATransaction.setCompletionBlock {
self.driverMarker.groundAnchor = CGPoint(x: 0.5, y: 0.5)
}
self.mapView.animate(to: GMSCameraPosition.camera(withLatitude: locations[0].coordinate.latitude, longitude: locations[0].coordinate.longitude, zoom: 17))
self.driverMarker.position = locations[0].coordinate
CATransaction.commit()
self.driverMarker.map = self.mapView
}
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];
}
}
I am making an iPhone application that uses a GMSMapView and I want to be able to add a toolbar on top of the map. This is my code:
#import "MapViewController.h"
#import <GoogleMaps/GoogleMaps.h>
#interface MapViewController ()
#end
#implementation MapViewController
{
GMSMapView *mapView_;
id<GMSMarker> myMarker;
}
- (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.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)back:(id)sender
{
[self dismissViewControllerAnimated:YES completion:NULL];
}
// You don't need to modify the default initWithNibName:bundle: method.
- (void)loadView
{
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:30.616083 longitude:-96.338908 zoom:13];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = YES;
mapView_.delegate = self;
self.view = mapView_;
GMSMarkerOptions *options = [[GMSMarkerOptions alloc] init];
options.position = CLLocationCoordinate2DMake(30.616083, -96.338908);
options.title = #"College Station";
options.snippet = #"Texas";
[mapView_ addMarkerWithOptions:options];
}
#pragma mark - GMSMapViewDelegate
- (void)mapView:(GMSMapView *)mapView
didTapAtCoordinate:(CLLocationCoordinate2D)coordinate
{
[myMarker remove];
NSLog(#"You tapped at %f,%f", coordinate.latitude, coordinate.longitude);
GMSMarkerOptions *marker = [[GMSMarkerOptions alloc] init];
marker.position = CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude);
marker.title = #"Tap Event";
// (icon for marker) marker.icon = [UIImage imageNamed:#"house"];
myMarker = [mapView_ addMarkerWithOptions:marker];
}
#end
In my storyboard i have a toolbar but I do not know how to get it to be displayed over the GMSMapView. Im new to iOS and have searched for solutions online but haven't had any luck with GMSMapView.
Because you set the view to the mapView_, the view made in StoryBoard is overriden. I've added a toolbar with an Segmented Control and an button using the following code:
CGRect screenBounds = [[UIScreen mainScreen] bounds];
UINavigationBar *navBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, screenBounds.size.width, 44)];
UINavigationItem *navItems = [[UINavigationItem alloc]init];
UIBarButtonItem *barButtonRight = [[UIBarButtonItem alloc]initWithTitle:#"" style:UIBarButtonItemStylePlain target:self action:#selector(SettingsButtonPressed)];
UISegmentedControll *segControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:#"Opt1",#"Opt2",#"Opt3", nil]];
UIImage *imageSettings= [UIImage imageNamed:#"36-toolbox.png"];
[segControl setSegmentedControlStyle:UISegmentedControlStyleBar];
[segControl setSelectedSegmentIndex:0];
[segControl setFrame:CGRectMake(10, 5, 150, 32)];
[segControl addTarget:self action:#selector(KiesVC:) forControlEvents:UIControlEventValueChanged];
[barButtonRight setImage:imageSettings];
[navItems setRightBarButtonItem:barButtonRight];
[navItems setTitleView:segControl];
[navBar setItems:[NSArray arrayWithObject:navItems] animated:NO];
You then add the toolbar by calling :
[self.view addSubview:navBar];
Hope it helps