IOS Baidu map Demo and ios map demo

Source: Internet
Author: User

IOS Baidu map Demo and ios map demo

 

I. Environment Settings

1. Development Environment: Xocode 7.3.1

2. simulator environment: iOS 9.3

3. You cannot directly use HTTP for requests after iOS 9. You need to add a configuration for controlling ATS in Info. plist:

<key>NSAppTransportSecurity</key><dict>    <key>NSAllowsArbitraryLoads</key>    <true/></dict>

 

That is:


Note:
Bitcode is the configuration option added after xcode 7. The default value isYESWhen we submit a program to the App store, Xcode will compile the program into an intermediate representation (bitcode ). App store then compiles the bitcode into a 64-bit or 32-bit program that can be executed.

The bitcode function is not required in this Demo.NO.

4. Import Baidu map SDK

Baidu map provides two import methods: You can drag the SDK directly into the project, or you can use CocoaPods. Here I use the following method to import Baidu map SDK.

Edit Podfile as follows:

Pod 'baidumapkit' # Baidu map SDK

 

In the Podfile folder, enter the following command:

pod install

 

But whether it is executionpod installOrpod updateAre stuck in Analyzing dependencies.

If CocoaPods has been installed before, we recommend that you use the following statement when installing and updating third-party class libraries, so that you do not have to wait too long:

pod install --verbose --no-repo-updatepod update --verbose --no-repo-updatepod install --no-repo-update

 

Manually configure the. framework Development Kit

Step 1. Import the. framework package as needed

The Baidu map iOS SDK provides the. framework Package in the form of subcontracting. Make sure that the versions of the packages are consistent during use by developers. Here, BaiduMapAPI_Base.framework is the basic package. You need to import any SDK function. Other packages can be imported as needed.
Copy the required baidumapi_xx.framework to the folder where the project is located.
Click "+" in TARGETS-> Build Phases-> Link Binary With Libaries, click "Add Other" in the pop-up window, and select "baidumapi_xx.framework" to Add it to the project.
Note: Objective-C ++ is used in the static library. Therefore, you must ensure that at least one.mmSource file with a suffix (you can.mThe suffix of the file is renamed.mm), Or specify the compilation method in the Project properties, that is, find Compile Sources As in Xcode Project> Edit Active Target> Build Setting, and set it to "Objective-C ++"

Step 2 introduce the required system library

Baidu map SDK provides positioning and animation effects. In v2.0.0, OpenGL is used for rendering. Therefore, you need to introduce CoreLocation in your Xcode project. framework and QuartzCore. framework, OpenGLES. framework, SystemConfiguration. framework, CoreGraphics. framework, Security. framework, libsqlite3.0.tbd (libsqlite3.0.dylib before xcode7), CoreTelephony. framework, libstdc ++. 6.0.9.tbd (xcode7 was previously libstdc ++. 6.0.9.dylib ).
(Note: The system library marked in red is the new system library v2.9.0. To use the map SDK v2.9.0 or a later version, you must add and import these three system libraries .)
Add the system Libraries in Xcode Project> Active Target> Build Phases> Link Binary With Libraries.

Step 3: configure the environment

Add-ObjC to TARGETS-> Build Settings-> Other Linker Flags.

Step 4: Introduce the mapapi. bundle resource file

If you use the basic map function, you need to add the resource. Otherwise, the map cannot display mapapi normally. bundle stores resource images for locating, marking View and route key points by default, and also stores necessary resource files for vector map painting. If you do not need the built-in image display function, you can delete the image folder in the bundle file. You can also replace or delete the image files in the image folder of the bundle as needed.
Method: select the project name, right-click the project name, and choose Add Files to "project name "..., Select the mapapi. bundle file from the BaiduMapAPI_Map.framework | Resources file, select the Copy items if needed check box, and click the Add button to Add the resource file to the project.

After running the Baidu map imported using cocoaPods, an error may be reported:

Enter

Import all Baidu map frameworks to the project:


Then run the command again. If the error is solved, the following error occurs:


 
Enter info. plist to add Bundle display name, for example:

Configure the Baidu map resource file:

 

 

Run again. The console will output "setting successful", indicating that the setting is successful.


Ii. code structure:
  • Classess
    • Baidu map & Path Planning
    • Covering 
      • YMAnnotationViewController-Custom annotation drag Controller
      • YMAnnotationController-System annotation drag Controller
    • Master Controller 
      • YMTableViewController-The controller that just entered the program
        • Controller 
          • YMMapViewController-Baidu map Controller
          • YMPoiDetailViewController-Store details Controller
          • YMRouteAnnotationController-Route Planning Controller
        • Model 
          • YMPoi-Shop Model
          • YMPointAnnotation-Tagging Model
        • View 
          • YMAnnotationView-Annotation View
          • YMPaopaoView-Click "NOTE" to bring up a custom bubble.
    • Category 
      • UIImage + Rotate-Image extension on the Path Planning page

Note:
1. Change AppDelegate. m to the AppDelegate. mm file.
2. You need to write the path planning control as the. mm suffix, that is, YMRouteAnnotationController. mm.

New Feature: November July 5, 2016

Two controllers are added, which areYMAnnotationViewController,YMAnnotationControllerYou can drag custom and system labels.

Baidu map provides the following proxy methods:

/*** When you drag an annotation view, this function is called if the view status changes. Ios3.2 and later support * @ param mapView map View * @ param view annotation view * @ param newState new state * @ param oldState old state */-(void) mapView :( BMKMapView *) mapView annotationView :( BMKAnnotationView *) view didChangeDragState :( BMKAnnotationViewDragState) newState fromOldState :( BMKAnnotationViewDragState) oldState;

 

However, the above method does not work when I set it. The method may not work...

So I used the following method:

/*** When an annotation View is selected, call this interface * @ param mapView map View * @ param views Selected annotation views */-(void) mapView :( BMKMapView *) mapView didSelectAnnotationView :( BMKAnnotationView *) view;/*** when an annotation view is deselected, call this interface * @ param mapView map View * @ param views to cancel the Selected annotation views */-(void) mapView :( BMKMapView *) mapView didDeselectAnnotationView :( BMKAnnotationView *) view;

 

After this setting, When I click the pin, the pin is selected. This time, the pin is set to start to drag, and then drag the map to cancel the selection, set the pin to the end drag status.

The following code:

/*** When an annotation View is selected, call this interface * @ param mapView map View * @ param views Selected annotation views */-(void) mapView :( BMKMapView *) mapView didSelectAnnotationView :( BMKAnnotationView *) view {// after being marked, set the start to drag the view status. dragState = BMKAnnotationViewDragStateStarting;}/*** when an annotation views is deselected, call this interface * @ param mapView map View * @ param views to cancel the Selected annotation views */-(void) mapView :( BMKMapView *) mapView didDeselectAnnotationView :( BMKAnnotationView *) annotationView {// stop dragging annotationView after the Selected Annotation is canceled. dragState = BMKAnnotationViewDragStateEnding; // you can adjust the value of CGPoint dropPoint = CGPointMake (annotationView. center. x, CGRectGetMaxY (annotationView. frame); CLLocationCoordinate2D newCoordinate = [_ mapView convertPoint: dropPoint toCoordinateFromView: annotationView. superview]; [annotationView. annotation setCoordinate: newCoordinate]; // geo Retrieval Information Class, obtains the current city data BMKReverseGeoCodeOption * reverseGeoCodeOption = [[BMKReverseGeoCodeOption alloc] init]; reverseGeoCodeOption. reverseGeoPoint = newCoordinate; [_ geoSearch reverseGeoCode: reverseGeoCodeOption];}

 

Note:

1. In AppDelegate. mm- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptionsTo set the Baidu Map Manager, you need to set the AppKey In the Baidu map API console:

NSString * mapKey = @ "x5EHcRvWZm8uzkt3HUpGBQU3"; _ mapManager = [[BMKMapManager alloc] init]; // if you want to pay attention to network and authorization verification events, set the generalDelegate parameter BOOL ret = [_ mapManager start: mapKey generalDelegate: nil]; if (ret) {NSLog (@ "set successfully! ");}

 

2. You may encounter the following problems when using cocoaPods:

The solution is as follows:

If you run it again, no error will be reported.

3. Note: the tags on the map capture the data of Meituan.

Note: 
This is just a simple demo. The writing is rough. You can leave a message for me. What kind of functions do you need? If you have time, I will try my best to add the required functions, I will continue to update later. Thank you for your support! If you think the demo I wrote is helpful to you, please download it on github and give it a star. Thank you very much!

The following is a demo:Github

Source code: http://www.jinhusns.com/Products/Download

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.