如何实现 iOS Google 地图 SDK

5

我有一个问题,似乎找不到答案。

我是iOS开发的新手,正在尝试在我的应用程序中使用Google Maps。

我已经按照他们给出的示例进行了操作。

    #import "DemoViewController.h"
#import <GoogleMaps/GoogleMaps.h>

@implementation DemoViewController {
  GMSMapView *mapView_;
}

- (void)loadView {
  GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:1.285
                                                          longitude:103.848
                                                               zoom:12];
  mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  self.view = mapView_;
}

@end

但是,你可以看到他们设置了self.view = mapView_;,而UIView类没有一个view函数。
我希望地图在我拥有的一个UIView中,并且它在另一个ViewController内。
你是否跟上了?这里是一张图片。 enter image description here 因此,我希望地图加载在该视图(或空白处)中。
感谢大家的帮助。

我刚刚尝试了他们提供的示例,其中你可以创建一个ViewController并加载地图。(以上已更新) - Claud
你可以将地图视图作为子视图添加吗?'self.view addSubview:mapView' - Rad'Val
4个回答

6

所以你有一个GMSMapView。你可以使用addSubview:将一个视图作为另一个视图的子视图添加到界面中。如果我是你,我不会在loadView中这样做。而是在viewDidLoad中进行。

我认为你真正的问题是你过于急躁了。你试图在不了解视图如何工作、视图控制器如何工作等基础知识的情况下完成这个任务。我建议你先深呼吸,学习iOS编程,然后再全力以赴。否则,你甚至不知道你的代码(或Google的代码)是什么意思。


你说得对,我需要学习更多,这也是我提出问题的原因。我只是想知道最好的学习方法是什么。 - Claud
嗯,我想我已经回答了。你需要了解视图和子视图,通过插座获取对视图的引用,加载视图控制器的视图并使用实例变量来引用插座等等。这些都是基本的iOS编程知识,与Google地图无关。当你掌握了所有这些知识后,如何将GMSMapView放入你的界面将变得非常明显。 - matt
@matt:另外一件事,你知道基于谷歌地图的应用在苹果上的审核情况如何吗?我不太确定。 - Kishor Kundan
1
我没有理由认为Google在这里做的任何事情违反了Apple的规定。苹果随时可以武断地执行他们的规定,但是你必须做的一切才能使用Google API似乎都不违反规定;这都是简单明了的光明正大的事情。 - matt

4
我找到了一个适合我的解决方案,如果有人需要进行类似的操作,可以参考以下步骤。首先,我使用了容器视图,然后创建了一个单独的UIViewController类(其中包含Google Maps SDK代码添加地图部分),并通过[self.view addSubview:googleMapsView]; 将其与主视图控制器连接起来,其中googleMapsView是我连接到主视图控制器的容器视图。

4

我在这里做了完全相同的事情:这里

只需通过界面构建器添加一个类型为GMSMapView的视图,然后在控制器中设置属性即可。


1
Please try below code.
@property (strong, nonatomic) IBOutlet GMSMapView *mapView;
-(void)addAllPinsOnMapView
{
arrMapPin=[[NSMutableArray alloc] init];
for(int i = 0; i < arrOfferList.count; i++)
{
    GMSCameraPosition *cameraPosition=[GMSCameraPosition cameraWithLatitude:[[[arrOfferList objectAtIndex:i] objectForKey:@"latitude"] floatValue] longitude:[[[arrOfferList objectAtIndex:i] objectForKey:@"longitude"] floatValue] zoom:12];
    //    mapView = [GMSMapView mapWithFrame:CGRectZero camera:cameraPosition];
    _mapView.camera = cameraPosition;
    _mapView.myLocationEnabled=YES;

    GMSMarker *marker=[[GMSMarker alloc]init];
    marker.position=CLLocationCoordinate2DMake([[[arrOfferList objectAtIndex:i] objectForKey:@"latitude"] floatValue], [[[arrOfferList objectAtIndex:i] objectForKey:@"longitude"] floatValue]);
    marker.title = [[arrOfferList objectAtIndex:i] objectForKey:@"business"];
    marker.map=_mapView;
    _mapView.delegate = self;
}
}
- (void)mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:
(GMSMarker *)marker {
NSPredicate *result;
result=[NSPredicate predicateWithFormat:@"business CONTAINS[c] %@",marker.title];
NSArray * array = [arrOfferList filteredArrayUsingPredicate:result];
OfferDetailsViewController *objOfferDetailsViewController = [[OfferDetailsViewController alloc]init];
objOfferDetailsViewController.dictOfferDetails=[array objectAtIndex:0];
[self.navigationController pushViewController:objOfferDetailsViewController animated:YES];
}
- (IBAction)btnLocateMe:(UIButton *)sender
{
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:[[[AppDelegate initAppDelegate].DictLocation objectForKey:@"Latitude"] floatValue] longitude:[[[AppDelegate initAppDelegate].DictLocation objectForKey:@"Longitude"] floatValue] zoom:14];
[_mapView animateToCameraPosition:camera];
}

虽然这段代码可能回答了问题,但提供有关它如何以及/或为什么解决问题的附加上下文将改善答案的长期价值。 - Timothy Truckle

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接