Getting Started

Install the SDK

Copy this line to your Podfile then run pod update

pod 'GoongSDK'

Open Yourworkspace.xcworkspace then import the SDK to your ViewController

Swift

import GoongSDK

Objective C

@import GoongSDK;

Access Token

Add a GoongAccessToken key to the Info.plist file with the value of your Goong access token

Simple MapView

let mapView = MGLMapView(frame: view.bounds)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
mapView.delegate = self
// View more style at MGLStyle.h
mapView.styleURL = MGLStyle.dayStyleURL
        
// Hanoi
let center = CLLocationCoordinate2D(latitude: 19.820689, longitude: -155.468038)
        
// Optionally set a starting point.
mapView.setCenter(CLLocationCoordinate2DMake(21.028511, 105.804817), zoomLevel: 12, animated: false)
        
view.addSubview(mapView)

Objective C

MGLMapView *mapView = [[MGLMapView alloc] initWithFrame:self.view.bounds];
mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
mapView.delegate = self;

// View more style at MGLStyle.h
mapView.styleURL = [MGLStyle dayStyleURL];

    // Hanoi
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(21.028511, 105.804817);

// Optionally set a starting point.
[mapView setCenterCoordinate:center zoomLevel:12 direction:0 animated:NO];

[self.view addSubview:mapView];

Camera animation

Swift

 func mapViewDidFinishLoadingMap(_ mapView: MGLMapView) {
        // Wait for the map to load before initiating the first camera movement.

        // Create a camera that rotates around the same center point, rotating 180°.
        // `altitude:` is meters above mean sea level that an eye would have to be in order to see what the map view is showing.
        let camera = MGLMapCamera(lookingAtCenter: mapView.centerCoordinate, altitude: 4500, viewingAngle: 15, bearing: 180)

        // Animate the camera movement over 5 seconds.
        mapView.setCamera(camera, withDuration: 5, animationTimingFunction: CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut))
    
  }

Objective C


-(void)mapViewDidFinishLoadingMap:(MGLMapView *)mapView {
    // Wait for the map to load before initiating the first camera movement.

    // Create a camera that rotates around the same center point, rotating 180°.
    // `altitude:` is meters above mean sea level that an eye would have to be in order to see what the map view is showing.
    [MGLMapCamera cameraLookingAtCenterCoordinate:mapView.centerCoordinate altitude:4500 viewingAngle:15 bearing:180]

    // Animate the camera movement over 5 seconds.
    [mapView setCamera:camera withDuration:5 animationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
}

For more animation, view MGLMapView and MGLMapView+Animation

Add marker

Swift

// Declare the marker and set its coordinates, title, and subtitle.
let marker = MGLMarker()
marker.coordinate = CLLocationCoordinate2D(latitude: 21, longitude: 105)
marker.title = "Hello world!"
marker.subtitle = "Welcome to my marker"
marker.reuseIdentifier = "markerIdentifier"
//Customize marker
marker.icon = UIImage(named: "marker")
// or 
marker.iconView = YourCustomView()

marker.rotation = 90 // degrees
marker.opacity = 0.5 

// Add marker `hello` to the map.
marker.map = self.mapView

// animate marker position

UIView.animate(withDuration: 0.25) {
 	marker.coordinate = newCoordinate
}

Objective C

// Declare the marker  and set its coordinates, title, and subtitle.
MGLMarker *marker = [[MGLMarker alloc] init];
marker.coordinate = CLLocationCoordinate2DMake(21, 105);
marker.title = @"Hello world!";
marker.subtitle = @"Welcome to my marker";

marker.reuseIdentifier = @"markerIdentifier";
//Customize marker
marker.icon = [UIImage imageNamed:@"marker"];
// or 
marker.iconView = [YourCustomView new];

marker.rotation = 90; // degrees
marker.opacity = 0.5; 

// Add marker `hello` to the map.
marker.map = self.mapView

// animate marker position

[UIView animateWithDuration:0.25 animations:^{
 	marker.coordinate = newCoordinate;
}];

Point conversion

Swift

import GoongSDK

class ViewController: UIViewController, MGLMapViewDelegate {
    var mapView: MGLMapView!

    override func viewDidLoad() {
        super.viewDidLoad()

        mapView = MGLMapView(frame: view.bounds)
        mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        view.addSubview(mapView)

        // Add a single tap gesture recognizer. This gesture requires the built-in MGLMapView tap gestures (such as those for zoom and annotation selection) to fail.
        let singleTap = UITapGestureRecognizer(target: self, action: #selector(handleMapTap(sender:)))
        for recognizer in mapView.gestureRecognizers! where recognizer is UITapGestureRecognizer {
            singleTap.require(toFail: recognizer)
        }
        mapView.addGestureRecognizer(singleTap)

        // Convert `mapView.centerCoordinate` (CLLocationCoordinate2D) to screen location (CGPoint).
        let centerScreenPoint: CGPoint = mapView.convert(mapView.centerCoordinate, toPointTo: nil)
        print("Screen center: \(centerScreenPoint) = \(mapView.center)")
    }

    @objc @IBAction func handleMapTap(sender: UITapGestureRecognizer) {
        // Convert tap location (CGPoint) to geographic coordinate (CLLocationCoordinate2D).
        let tapPoint: CGPoint = sender.location(in: mapView)
        let tapCoordinate: CLLocationCoordinate2D = mapView.convert(tapPoint, toCoordinateFrom: nil)
        print("You tapped at: \(tapCoordinate.latitude), \(tapCoordinate.longitude)")

        // Create an array of coordinates for our polyline, starting at the center of the map and ending at the tap coordinate.
        var coordinates: [CLLocationCoordinate2D] = [mapView.centerCoordinate, tapCoordinate]

        // Remove any existing polyline(s) from the map.
        if mapView.annotations?.count != nil, let existingAnnotations = mapView.annotations {
            mapView.removeAnnotations(existingAnnotations)
        }

        // Add a polyline with the new coordinates.
        let polyline = GoongPolyline(coordinates: &coordinates, count: UInt(coordinates.count))
        mapView.addAnnotation(polyline)
    }
}

Objective C

#import "ViewController.h"
@import GoongSDK;
@interface ViewController ()
@property (nonatomic) MGLMapView *mapView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.mapView = [[MGLMapView alloc] initWithFrame:self.view.bounds];
    self.mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:self.mapView];

    // Add a single tap gesture recognizer. This gesture requires the built-in MGLMapView tap gestures (such as those for zoom and annotation selection) to fail.
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleMapTap:)];
    for (UIGestureRecognizer *recognizer in self.mapView.gestureRecognizers) {
        if ([recognizer isKindOfClass:[UITapGestureRecognizer class]]) {
            [singleTap requireGestureRecognizerToFail:recognizer];
        }
    }
    [self.mapView addGestureRecognizer:singleTap];

    // Convert `mapView.centerCoordinate` (CLLocationCoordinate2D) to screen location (CGPoint).
    CGPoint centerScreenPoint = [self.mapView convertCoordinate:self.mapView.centerCoordinate toPointToView:nil];
    NSLog(@"Screen center: %@ = %@", NSStringFromCGPoint(centerScreenPoint), NSStringFromCGPoint(self.mapView.center));
}

- (IBAction)handleMapTap:(UITapGestureRecognizer *)tap {
    // Convert tap location (CGPoint) to geographic coordinate (CLLocationCoordinate2D).
    CGPoint tapPoint = [tap locationInView:self.mapView];
    CLLocationCoordinate2D tapCoordinate = [self.mapView convertPoint:tapPoint toCoordinateFromView:nil];
    NSLog(@"You tapped at: %.5f, %.5f", tapCoordinate.latitude, tapCoordinate.longitude);

    // Create an array of coordinates for our polyline, starting at the center of the map and ending at the tap coordinate.
    CLLocationCoordinate2D coordinates[] = { self.mapView.centerCoordinate, tapCoordinate };
    NSUInteger numberOfCoordinates = sizeof(coordinates) / sizeof(CLLocationCoordinate2D);

    // Remove any existing polyline(s) from the map.
    if (self.mapView.annotations.count) {
        [self.mapView removeAnnotations:self.mapView.annotations];
    }

    // Add a polyline with the new coordinates.
    GoongPolyline *polyline = [GoongPolyline polylineWithCoordinates:coordinates count:numberOfCoordinates];
    [self.mapView addAnnotation:polyline];
}

@end

Display 3D buildings

Swift

mapView.isBuildingsEnabled = true

Objective C

[mapView setBuildingsEnabled:YES];

Add a polyline

Swift

let polyline = GoongPolyline(path: "ylj_Ce|ydSfoCuqG")
polyline.map = self.mapView
polyline.strokeColor = .green
polyline.strokeWidth = 5
polyline.opacity = 1

Objective C

GoongPolyline *polyline = [GoongPolyline polylineWithPath:@"mot_CgshdS~HsyDb_AasChWid@rjBt|EcuB`iG"];
polyline.map = self.mapView;
polyline.strokeColor = [UIColor greenColor];
polyline.strokeWidth = 5;
polyline.opacity = 1;

Decode geometry string

Swift

let geometryString = "abcxyz#$%^&*"
let locations = MGLUtility.decodeGeometry(geometryString) // default precision is 5
let locationsWithPrecision = MGLUtility.decodeGeometry(geometryString, precision: 5)

Objective C

NSString *geometryString = @"abcxyz$%^&*";
NSArray *locations = [MGLUtility decodeGeometry:geometryString]; // Default precision is 5
NSArray *locationsWithPrecision = [MGLUtility decodeGeometry:geometryString precision:5];