我是 iOS 编程新手,我已经下载了适用于 iOS 的谷歌地图 sdk,并按照他们网站上的说明进行操作(如此链接所示)https://developers.google.com/maps/documentation/ios/start https://developers.google.com/maps/documentation/ios/start)
并能够在我的应用程序中获取地图。
现在,我尝试在 Google 地图底部的屏幕上添加一个按钮,以便用户可以选择返回到上一个屏幕。
我只知道 UIButton 是 UIView 的子类,我们可以通过使其成为该类的子视图来使按钮出现在视图上。以前,iOS 通过 MKMapView 默认使用 Google 地图,我在书籍和互联网上看到过一些示例,其中显示了应用程序的屏幕截图,其中按钮或文本框将出现在地图上。但现在仅仅拖动界面生成器中的按钮对谷歌地图的SDK没有帮助。
这是我的代码:
视图控制器.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <GoogleMaps/GoogleMaps.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *btn;
@end
视图控制器.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
您可以看到,在最后两行中,我已将按钮设置为地图视图的子视图,并尝试将其置于前面。但这没有帮助。请让我知道我缺少什么,或者是否有其他方法可以通过使用其他功能来做到这一点。
另请检查我创建的故事板的屏幕截图,以便您可以更好地理解我在这里尝试做的事情。
Thanks.