入门
在您可以开始在 iOS 上使用 TPL Maps 之前,您需要下载 TPL Maps SDK for iOS。
第一步:获取最新版本的 Xcode
要使用 TPL Maps SDK for iOS 构建项目,您需要 Xcode 的 9.0 或更高版本。
第二步:生成配置文件
要使用 TPLMaps SDK for iOS,您必须获取 Config.plist 文件,然后将其添加到您的 iOS 应用程序中。Config.plist 用于跟踪与您的项目相关的 API 请求以进行使用和计费。
在此获取最新配置文件 点击这里。
此文件包含您刚刚启用的服务的配置细节,例如密钥和标识符。将 Config.plist 文件添加到您从 TPLMaps 站点下载的 xcode 项目中,开始使用 TPLmaps sdk。
步骤 3:安装 SDK
适用于 iOS 的 TPLMap SDK 作为 CocoaPods 包提供。CocoaPods 是一个开源的依赖管理器,用于 Swift 和 Objective-C Cocoa 项目。
如果您还没有 CocoaPods 工具,请在 macOS 上安装它,在终端中运行以下命令。有关详细信息,请参阅CocoaPods 入门指南。
sudo gem install cocoapods
为 iOS 的 TPLMaps SDK 创建一个 Podfile
文件,并使用它来安装 API 及其依赖项
-
如果您还没有 Xcode 项目,请现在创建一个,并将其保存到您的本地机器上。(如果您是 iOS 开发的新手,请创建一个 Single View Application。)
-
在您的项目目录中创建一个名为
Podfile
的文件。此文件定义了您项目的依赖项。 -
编辑
Podfile
并添加您的依赖项。以下是一个示例,包括您需要的用于 iOS TPLMaps SDK 的依赖项
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.3'
target 'YOUR_APPLICATION_TARGET_NAME_HERE' do
pod 'TPLMaps'
end
-
保存
Podfile
-
在终端中打开包含
Podfile
的目录
cd <path-to-project>
-
运行
pod install
命令。这将安装Podfile
中指定的 API,以及它们可能需要的所有依赖项。 -
关闭 Xcode,然后打开(双击)您的项目
.xcworkspace
文件以启动 Xcode。从现在起,您必须使用.xcworkspace
文件来打开项目。
步骤 4:添加地图
以下代码演示如何将简单地图添加到现有的 ViewController 中。如果您正在创建新的应用程序,请首先按照上述安装说明操作,并创建一个新的 Single View Application。
现在,在您的应用程序默认的 ViewController 中添加或更新一些方法,以创建和初始化 TPLMapView
的实例。
- (void)viewDidLoad {
[super viewDidLoad];
//Initializes and returns a new map view for specified frame
TPLMapView * mapView = [[TPLMapView alloc] initWithFrame:self.view.bounds];
//The map view delegate
mapView.delegate = self;
//Show user location on map. Read documentation for more info
mapView.showsUserLocation = true;
//Show user location button on map. Read documentation for more info
mapView.myLocationButtonEnabled = true;
//Show map zoom controls
mapView.zoomControlsEnabled = true;
//Show compass on map
mapView.showsCompass = true;
//Show POI's in map
mapView.showsPointsOfInterest = true;
//Show Buildings on map
mapView.showsBuildings = true;
//Enum provide MapView theme Day | Night.
mapView.mapTheme = NIGHT;
//Wether pan gesture is enabled or disabled on map.
mapView.scrollEnabled = true;
//Wether tilt gesture is enabled or disabled on map.
mapView.tiltEnabled = true;
//Wether rotate gesture is enabled or disabled on map.
mapView.rotateEnabled = true;
//Whether all gestures should be enabled (default) or disabled.
mapView.allGesturesEnabled = true;
//Map Max zoom
mapView.maximumZoom = 20;
//Map Min zoom
mapView.minimumZoom = 10;
//Render Maps to view
[self.view addSubview:mapView];
}
在 MapView 上添加点
CLLocation * newLoc = ;
//Initilize Marker with set of coordinate
Marker * annotationPin = [Marker markerWithPosition:newLoc.coordinate];
//The string containing the Marker's Title.
annotationPin.title = @"Point";
//A Boolean value indicating whether the annotation view display in a callout bubble.
annotationPin.canShowCallout = true;
//Marker icon to render. If left nil, uses a default SDK place marker.
annotationPin.icon = [UIImage imageNamed:@"car"];
//Sets the rotation angle for the Marker in degrees
annotationPin.rotation = 60.0;
//Controls whether this marker should be flat against the Earth's surface
annotationPin.flat = false;
//Method to render marker on map
[self.mapView addMarker:annotationPin];
在地图视图中添加圆
CLLocation * loc = ;
//Initilize Circle with set of Coordinate and radius
Circle * circle = [Circle circleWithCenterCoordinate:loc.coordinate radius:100];
//The outline width of the line in screen points.Default is 2
circle.outlineWidth = 0.1;
//A Boolean value that indicates whether the polyline is transparent.
circle.transparent = true;
//The UIColor used to render the polyline. if empty or nil default color will be Blue
circle.outlineColor = [UIColor colorWithRed:244.0/255.0 green:66.0/255.0 blue:226.0/255.0 alpha:1.0];
//Method to render Circle on map
[self.mapView drawCircle:circle];
在地图视图中添加折线
CLLocation * lineCoordinates1 = ;
CLLocation * lineCoordinates2 = ;
//CoordinateGeometry is colloction of CLLocationCooordinate2D
MutableCoordinateGeometry * geometry = [[MutableCoordinateGeometry alloc] init];
//Adds coordinate to the end of geometry
[geometry addCoordinate:lineCoordinates1.coordinate];
[geometry addCoordinate:lineCoordinates2.coordinate];
//Initilize Polyline with set of MutableCoordinateGeometry
PolyLine * polyline = [PolyLine polylineWithCoordinates:geometry];
//The outline width of the line in screen points.Default is 2
polyline.outlineWidth = 3;
//A Boolean value that indicates whether the polyline is transparent.
polyline.transparent = false;
//The Width of the line in screen points.Default is 2
polyline.lineWidth = 3;
//The UIColor used to render the polyline. if empty or nil default color will be Blue
polyline.outlineColor = [UIColor redColor];
//The UIColor used to fill the shape’s path
polyline.fillColor = [UIColor blueColor];
//Method to render polyline on map
[self.mapView drawPolyLine:polyline];
在地图视图中添加多边形
//CoordinateGeometry is colloction of CLLocationCooordinate2D
MutableCoordinateGeometry * geometry = [[MutableCoordinateGeometry alloc] init];
//Adds coordinate to the end of geometry
[geometry addCoordinate:loc1.coordinate];
[geometry addCoordinate:loc2.coordinate];
[geometry addCoordinate:loc3.coordinate];
[geometry addCoordinate:loc4.coordinate];
//Initilize Polygon with set of MutableCoordinateGeometry
Polygon * polygon = [Polygon polygonWithCoordinates:geometry];
//The outline width of the line in screen points.Default is 2
polygon.outlineWidth = 0.1;
//A Boolean value that indicates whether the polyline is transparent.
polygon.transparent = true;
//The UIColor used to render the polyline. if empty or nil default color will be Blue
polygon.outlineColor = [UIColor colorWithRed:244.0/255.0 green:66.0/255.0 blue:226.0/255.0 alpha:1.0];
//Method to render Polygon on map
[self.mapView drawPolygon:polygon];
反向地理编码
//Create location object
CLLocation * location = [[CLLocation alloc] initWithLatitude:coordinate.latitude
longitude:coordinate.longitude];
//Submits a reverse-geocoding request for the specified location and return place
[Geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<Place *> * placemarks, NSError * error) {
if (error) {
NSLog(@"%@", error.localizedDescription);
}else {
NSLog(@"placemark %@", placemarks.description);
}
} ];