iphone - How to add a button on the Google maps in iOS? -
i new ios programming , have downloaded google maps sdk ios , followed instruction on website ( shown in link https://developers.google.com/maps/documentation/ios/start ) , able map in application.
now trying add button on screen @ bottom on google maps giving option user go previous screen.
i know uibutton subclass of uiview , can make button appear on view making sub view of class. ios used use google maps default mkmapview , have seen examples in books on internet showing screen shots of apps button or text box appear on map. dragging button in interface builder doesn't sdk of google maps.
here code:
viewcontroller.h
#import <uikit/uikit.h> #import <mapkit/mapkit.h> #import <googlemaps/googlemaps.h> @interface viewcontroller : uiviewcontroller @property (weak, nonatomic) iboutlet uibutton *btn; @end
viewcontroller.m
#import "viewcontroller.h" #import <mapkit/mapkit.h> #import <googlemaps/googlemaps.h> #import <corelocation/corelocation.h> @interface viewcontroller () @end @implementation viewcontroller { gmsmapview *mapview_; } - (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } - (void)loadview { cllocationmanager *locationmanager = [[cllocationmanager alloc] init]; locationmanager.distancefilter = kcldistancefilternone; locationmanager.desiredaccuracy = kcllocationaccuracykilometer; [locationmanager startupdatinglocation]; //latitude , longitude of current location of device. double lati = locationmanager.location.coordinate.latitude; double longi = locationmanager.location.coordinate.longitude; nslog(@"latitude = %f", lati); nslog(@"longitude = %f", longi); cllocation *mylocation = [[cllocation alloc] initwithlatitude:lati longitude:longi]; // create gmscameraposition tells map display coordinate gmscameraposition *camera = [gmscameraposition camerawithlatitude:lati longitude:longi zoom:11.5]; mapview_ = [gmsmapview mapwithframe:[[uiscreen mainscreen] bounds] camera:camera]; mapview_.mylocationenabled = yes; self.view = mapview_; // creates marker in center of map. gmsmarker *marker = [[gmsmarker alloc] init]; marker.position = cllocationcoordinate2dmake(lati, longi); marker.title = @"it's me"; marker.snippet = @"my location"; marker.map = mapview_; [mapview_ addsubview:_btn]; [mapview_ bringsubviewtofront:_btn]; } @end
you can see in last 2 lines have made button subview of mapview , tried bring front. didn't help. please let me know missing or if there way using other function.
please check screenshot of storyboard have created can understand better trying here.
thanks.
gmsmapview
subclass of uiview
can add subviews other view
try code
uibutton *button = [uibutton buttonwithtype:uibuttontyperoundedrect]; button.frame = cgrectmake(mapview_.bounds.size.width - 110, mapview_.bounds.size.height - 30, 100, 20); button.autoresizingmask = uiviewautoresizingflexibleleftmargin | uiviewautoresizingflexibletopmargin; [button settitle:@"button" forstate:uicontrolstatenormal]; [mapview_ addsubview:button];
it adds 100x20
button subview of gmsmapview
, positioned bottom right corner. have tested , button can touched in other view
edit: move code -loadview
-viewdidload
. -loadview
method never called when using ib create ui. docs -loadview
says:
if use interface builder create views , initialize view controller, must not override method.
edit 2: believe when create view hierarchy using interface builder, can not reset self.view
property doing.
do in -viewdidload
[self.view addsubview: mapview_];
instead of
self.view = mapview_;
if passing gmsmapview
self.view
property, map view in controller point. thats, believe, reason why u can't see ib-created button.
Comments
Post a Comment