Baidu map API application developed for iOS

Source: Internet
Author: User

Currently, most of our IOS development projects use GoogleMap. IOS also comes with googleMap. However, if we want to display traffic information and other functions in real time on the map, googlemap does not, so sometimes we can use Baidu map for applications. Below I will briefly introduce the BMapKit application:

I. First, we need to create a map for BMKMapManager to manage the application. Without this class, the map cannot be displayed.

The red font below is the map api-key officially applied for by Baidu;

BMKMapManager * _ mapManager = [BMKMapManager
Alloc] init];

BOOL ret = [_ mapManager
Start: @ "C3252C69EDB6D21A10B3FC9657FD1DDC7E0000 **"
GeneralDelegate: self];

If (! Ret ){

NSLog (@ "manager start failed! ");

}

2. Add BMKMapView to view, set BMKMapViewDelegate, and add annotation (record point of interest, BMKAnnotation). At the same time, you can set the title (set the title of annotation) for each point of interest ), and subtitle ).

@ Interface MapBaiDu:
UIViewController <BMKMapViewDelegate> {}

@ Property (nonatomic,
Strong) BMKMapView * _ mapView;

@ End


-(Void) viewDidLoad {

_ MapView = [[BMKMapView
Alloc] initWithFrame: CGRectMake (0,
39,320,
377)]; // create a MKMapView

[Self. view
AddSubview: _ mapView];

[_ MapView
Release];

_ MapView. delegate =
Self;
// Set proxy

_ MapView. showsUserLocation =
YES; // set to display user location

CLLocationCoordinate2D coordinate; // you can specify the longitude and latitude.

Coordinate. latitude =
40.027283; // latitude

Coordinate. longpolling =
116.313217; // longitude

BMKCoordinateRegion viewRegion =
BMKCoordinateRegionMake (coordinate, BMKCoordinateSpanMake (1.0,
1.0 ));

BMKCoordinateRegion adjustedRegion = [_ mapView
RegionThatFits: viewRegion];

[_ MapView
SetRegion: adjustedRegion animated: YES];

}

Last line above: Set the longitude and latitude range of the current map. The Set range may be adjusted to a suitable range for display in the Map window. Region is an attribute of BMKMapView, type BMKCoordinateRegion. This line creates a coordinate center with 0.5 longitude (latitude) degrees between top and bottom. However, we need to note that the created area is a square and does not conform to the BMKMapView ratio we need. Then, adjust the display range using the regionThatFits method.


/// Indicates a latitude and longitude Area

Typedef struct {

CLLocationCoordinate2D center;
/// <Longitude and latitude coordinates of the central point

BMKCoordinateSpan span;
/// <Latitude and longitude range

} BMKCoordinateRegion;

/// Indicates a latitude and longitude range

Typedef struct {

CLLocationDegrees latitudeDelta;
/// <Latitude range

CLLocationDegrees longitudeDelta;
/// <Longitude range

} BMKCoordinateSpan;


Iii. delegate

1: Call the function when the map area changes:

-(Void) mapView :( BMKMapView *) mapView regionWillChangeAnimated :( BOOL) animated;

-(Void) mapView :( BMKMapView *) mapView regionDidChangeAnimated :( BOOL) animated;

2: annotation

 
* Generate the corresponding View based on anntation

-(BMKAnnotationView *) mapView :( BMKMapView *) mapView viewForAnnotation :( id <BMKAnnotation>) annotation;

 
* This interface is called when mapView adds annotation views.

-(Void) mapView :( BMKMapView *) mapView didAddAnnotationViews :( NSArray *) views;

 
* This interface is called when an annotation views is selected.

-(Void) mapView :( BMKMapView *) mapView didSelectAnnotationView :( BMKAnnotationView *) view;

* This interface is called when an annotation views is deselected.

-(Void) mapView :( BMKMapView *) mapView didDeselectAnnotationView :( BMKAnnotationView *) view;

Annotation is divided into two parts: the BMKAnotation class is the protocol of the annotation point and provides the basic information function of the annotation class. The title and subtitle are the title and subtitle respectively. You can also set the left side of the annotation, setCoordinate will be called during dragging;

BMKAnnotationView is the display view class for annotation points. This class inherits UIView. You can set the image displayed for this view and centerOffset (center position, positive offset, so that the view is moved beyond the lower right corner, the Unit is pixel. You can also set calloutOffset to change the position of the fade-out bubble (the positive offset allows the view to move beyond the lower right corner, and the negative to move toward the upper left, in pixels ). You can also set the touch event. The default value is YES, which can be selected or enabled.
= NO. Other attributes include: selected, canShowCallout, leftCalloutAccessoryView, and rightCalloutAccessoryView. And so on.

4. Call the function when the map view is located:

* This interface is called when an annotation views is deselected.

-(Void) mapView :( BMKMapView *) mapView didDeselectAnnotationView :( BMKAnnotationView *) view;

* This function is called when the map View is about to start positioning.

-(Void) mapViewWillStartLocatingUser :( BMKMapView *) mapView;

* This function is called when the map View is stopped.

-(Void) mapViewDidStopLocatingUser :( BMKMapView *) mapView;

* This function is called when locating fails.

-(Void) mapView :( BMKMapView *) mapView didFailToLocateUserWithError :( NSError *) error;

* This function is called after the user location is updated.

-(Void) mapView :( BMKMapView *) mapView didUpdateUserLocation :( BMKUserLocation *) userLocation;

5. This interface is called when overlay (shadow indicates a region) is generated or newly added.

* Generate the corresponding View based on overlay

-(BMKOverlayView *) mapView :( BMKMapView *) mapView viewForOverlay :( id <BMKOverlay>) overlay;

* This interface is called when mapView adds overlay views.

-(Void) mapView :( BMKMapView *) mapView didAddOverlayViews :( NSArray *) overlayViews;

6. This interface is called when you click the bubble popped up by annotation view.

* This interface is called when you click the bubble popped up by annotation view.

-(Void) mapview :( bmkmapview *) mapview annotationviewforbubble :( bmkannotationview *) view;

9: The annotation view has many different States. You can set different operations for different states. The status of the view changes when you drag the annotation view.

-(Void) mapview :( bmkmapview *) mapview annotationview :( bmkannotationview *) view didchangedragstate :( bmkannotationviewdragstate) newstate

Fromoldstate :( bmkannotationviewdragstate) oldstate;

Enum {

Bmkannotationviewdragstatenone = 0,
/// <Static status.

Bmkannotationviewdragstatestarting, // <
Start dragging

Bmkannotationviewdragstatedragging, // <
Dragging

Bmkannotationviewdragstatecanceling, // <
Undrag

BMKAnnotationViewDragStateEnding // <
Drag to end

};

Typedef NSUInteger BMKAnnotationViewDragState;

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.