How does "iOS development" develop in the Swift language for lbs applications?

Source: Internet
Author: User

This article is divided into three parts, the first part of the introduction of the Swift language to develop LBS application, and give a complete sample and source code; The second part describes how to request the LBS key, the third part is a comprehensive sample view, scan the QR code to see the sample demo.

The first section uses the Swift language to develop LBS applications

1. Download the latest version of the iOS map SDK, address: http://lbs.amap.com/api/ios-sdk/down/

2. Apply for LBS key (see Part Two).

3.Xcode New Project

Create a single View application project. Note: Language Select Swift

4. project configuration

A. Introducing the Map Gallery & Search Library

Select the project name in the left-hand directory, click the "+" button in the Targets->build phases-> Link Binary with Libaries, and click the "Add Other" button in the pop-up window to select the extracted The Mamapkit.framework file is added to the project.

The search library is added in the same way.

B. Introduction of Amap.bundle resource files

The Amap.bundle resource file stores images such as location, default PIN label gaze, and can be exploited using these resource images.

Select the project name in the left-hand directory, select Add files to "project name" in the right-click menu, select the Amap.bundle file from the Mamapkit.framework->resources folder, and tick "Copy items if needed check box, click the Add button to add the resource file to the project.

C. Introduction of System Libraries

Select the project name in the left-hand directory, click the "+" button in the Targets->build settings-> Link Binary with Libaries, find and select the desired library in the pop-up window (see the table below), click the "Add" button, Add the library file to the project.

Description

In the notes, the system files required to use 2D raster maps are represented by the system files used in the 3D vector map, and search indicates the system files needed to use the search library.

Systemconfiguration.framework, Coretelephonysecurity.framework, security.framework are used to count app information.

D.swift compiling the configuration

First: Create a new bridge header file (placed under the project path), named Amapdemoswift-bridging-header.h, and import the required library file in the header file with the following code:

#import <MAMapKit/MAMapKit.h>#import <AMapSearchKit/AMapSearchAPI.h>

Then, with the project name selected in the left-hand directory, enter the path to the bridging file in Targets->build phases-> Swift compiler-code Generation, objective-c briding Header

5, the map display

Take the vector Map SDK as an example to introduce.

In Viewcontroller.swift, inherit the Mamapviewdelegate protocol, configure the user key in the Viewdidload method, initialize the Mamapview object, and add it to the Subview. The code is as follows:

Let APIKey ="8a1383b14466a8dbf362f44357c496c0" classViewcontroller:uiviewcontroller, mamapviewdelegate{var mapview:mamapview?Overridefunc viewdidload () {super.viewdidload ()//additional setup after loading the view, typically from a nib. //Configure user KeyMamapservices.sharedservices (). ApiKey =APIKey//Initialize MamapviewInitmapview ()} func Initmapview () {Mapview=Mamapview (frame:self.view.bounds) Mapview!.Delegate=Self self.view.addSubview (Mapview!)    }}

Run the program, the map shows, it is so simple ~

6. A practical example

Take inverse geocoding as an example and write a complete example. The implementation steps are as follows:

(1) Initialize the main search object Amapsearchapi and inherit the search protocol amapsearchdelegate.

(2) construct the Request object and configure the search parameters.

(3) Initiate a search with the Request object as a parameter through the main search object.

(4) Implement the corresponding callback function in the search protocol, and obtain the search result by parsing the Response object.

By positioning to get the latitude and longitude of the current position, when you click on the positioning dimension (small blue dot), the inverse geocoding, in the pop-up bubbles show the address of the anchor point. There are several steps to implementing this scenario:

1. Turn on the positioning and display the location callout (small blue dot).

2. Gets the latitude and longitude of the anchor point in the positioned callback function.

3. Click on the location callout to perform the inverse geocoding query.

4. Set the title and subtitle of the location callout in the inverse geocoding callback.

All source code:

Import UIKit Let APIKey="8a1383b14466a8dbf362f44357c496c0" classViewcontroller:uiviewcontroller, Mamapviewdelegate, amapsearchdelegate{var mapview:mamapview?var search:amapsearchapi?var currentlocation:cllocation?Overridefunc viewdidload () {super.viewdidload ()//additional setup after loading the view, typically from a nib.mamapservices.sharedservices (). ApiKey=APIKey Initmapview () Initsearch ()} func Initmapview () {MA PView=Mamapview (frame:self.view.bounds) Mapview!.Delegate=Self self.view.addSubview (Mapview!) Let COMPASSX= Mapview?. compassorigin.x Let ScaleX= Mapview?. scaleorigin.x//setting the position of the compass and the scale barMapview?.compassorigin = Cgpointmake (compassx!, +) Mapview?. Scaleorigin = Cgpointmake (scalex!, +)                 //Turn on positioningmapview!. Showsuserlocation =true                  //Set the follow positioning mode to set the anchor point to the map center pointmapview!. Usertrackingmode =Mausertrackingmodefollow}//Initialize AMAPSEARCHAPIfunc Initsearch () {Search= Amapsearchapi (Searchkey:apikey,Delegate: Self); }         //Inverse geo-codingfunc reversegeocoding () {Let coordinate= Currentlocation?. Coordinate//constructing amapregeocodesearchrequest objects, configuring query parameters (center point coordinates)Let regeo:amapregeocodesearchrequest =amapregeocodesearchrequest () regeo.location= Amapgeopoint.locationwithlatitude (CGFloat (coordinate!. Latitude), Longitude:cgfloat (coordinate!. Longitude)) println ("Regeo: \ (Regeo)")         //perform inverse geocoding queriesself.search!. Amapregoecodesearch (Regeo)}//positioning callbacksFunc Mapview (mapview:mamapview!, Didupdateuserlocation userlocation:mauserlocation!, Updatinglocation:bool) {        ifupdatinglocation {currentlocation=Userlocation.location}}//Click Annoation callbackFunc Mapview (mapview:mamapview!, Didselectannotationview view:maannotationview!) {        //if you clicked on a positional callout, perform inverse geocoding        ifView.annotation.isKindOfClass (mauserlocation) {reversegeocoding ()}}//Inverse geocoding CallbackFunc Onregeocodesearchdone (request:amapregeocodesearchrequest!, response:amapregeocodesearchresponse!) {println ("request: \ (Request)") println ("response: \ (response)")                 if(Response.regeocode! =Nil) {var title=response.regeocode.addressComponent.city var length:int{returncountelements (title)}if(Length = =0) {title=Response.regeocode.addressComponent.province}//assigns the title and subtitle of the location callout, displaying the address information of the anchor point in the bubbleMapview?. Userlocation.title =title Mapview?. Userlocation.subtitle =response.regeocode.formattedAddress} }}

full source Download: Https://github.com/hadesh/MyRoute

Part two how to apply for lbs key

1. Access Application key address: http://lbs.amap.com/console/key/

2. Enter the real app name and select the IOS SDK Platform service.

3. Get bundle Indentifier

Get method One, code get

NSString *bundleidentifier = [[NSBundle mainbundle] bundleidentifier];

Get the way two, Xcode switch to the General tab, view bundle Identifier

4. Click the Get Key button.

Part III viewing examples

In case of any lbs related issues, you can send email to [email protected]and reply to you within 48 hours.

How does "iOS development" develop in the Swift language for lbs applications?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.