在iPhone中的Google地图中添加子视图(如UISlider或UIButtom) [英] Adding a subview like UISlider or UIButtom in Google Maps in iPhone
问题描述
我是iOS编程的新手,对COCOA Touch了解甚少。我想在谷歌地图底部的屏幕上添加一个按钮,让用户回到上一个屏幕。我只是知道 UIButton
是 UIView
的子类,我们可以使按钮出现在视图上,使其成为子视图的类。以前,iOS在 MKMapView
中默认使用Google地图,我在互联网上的书籍中看到了一些示例,其中显示了应用程序的屏幕截图,其中会出现按钮或文本框在地图上。但现在只要在界面生成器中拖动按钮就没有帮助。
I am new to iOS Programming and have very little knowledge about COCOA Touch. I am trying to add a button on the screen at the bottom over Google maps for giving an option to the user to go back to the previous screen. I just know that UIButton
is a subclass of UIView
and we can make button appear on a view by making it the sub view of that class. Previously iOS used to use Google Maps by default by in MKMapView
and I have seen examples in books an on the Internet showing screen shots of apps where a button or a text box would appear on the map. But now just dragging the button in the interface builder doesn't help.
这里是我的代码:
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];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)loadView
{
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
[locationManager startUpdatingLocation];
//Latitude and longitude of the current location of the 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 a GMSCameraPosition that tells the map to display the 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 a marker in the center of the 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
推荐答案
在当前视图上执行正常的UI构建,然后将 GMSMapView
添加为子视图
(在索引
self.view
(不要做 self.view = mapView;
)
Do normal UI building on your current view, then add the GMSMapView
as subview
(at index
0) of the self.view
(DON'T do self.view = mapView;
)
以下是重要代码:
mapView = [GMSMapView mapWithFrame:self.view.bounds camera:camera];
[self.view insertSubview:mapView atIndex:0];
在 index
0插入地图检视将其余对象设置在前面。
Inserting the map view at index
0 will set the rest of the objects in front.
这篇关于在iPhone中的Google地图中添加子视图(如UISlider或UIButtom)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!