Map (basic application, Location Manager, annotation)

Source: Internet
Author: User

# Import "viewcontroller. H"
# Import <mapkit/mapkit. h>

@ Interface viewcontroller () <cllocationmanagerdelegate, mkmapviewdelegate>

// Why do we set it as an attribute? In order to solve the problem of strong reference, if not set, the locationmanger will be released after the curly brackets, so that we cannot see the desired address change information. In the case of arc, the solution is to set it as a member attribute.
@ Property (retain, nonatomic) cllocationmanager * locationmanger;

@ End

@ Implementation viewcontroller

-(Void) viewdidload
{
[Super viewdidload];

// Create a map display view mkmapview
Mkmapview * map = [[mkmapview alloc] initwithframe: Self. View. bounds];


// A tag is used to identify the map to be displayed in the future.
Map. tag= 1000;


// Set Span
// Span refers to the display of map accuracy. The smaller the value, the more precise the map data is, the more detailed the map data is. If the value is greater, the greater the display area
Mkcoordinatespan span = {0.1, 0.1 };
// The second method for assigning struct values
// Mkcoordinatespan span2;
// Span2.latitudedelta = 100;
// Span2.longitudedelta = 100;

// Use the C function to create the specific data of the struct.
// Mkcoordinatespan span3;
// Span3 = mkcoordinatespanmake (100,100 );

/*
* There are three ways to assign values to struct variables in the OC environment:
1? During initialization, the curly braces are directly used. In the curly braces, values are assigned in sequence according to the order declared by the struct.
2? Declare the struct variable first. You can use the dot syntax to specify the value of the corresponding member variable.
3? Use the C function of the SDK to directly complete the settings
*/


// Set the latitude and longitude ---- the geographic coordinates of the current position displayed on the map
Cllocationcoordinate2d coordinate = {34.7568711, 113.663221 };


// Set the area displayed on the question map. Here, we need to set two parameters-cllocationcoordinate2d and mkcoordinatespan.
Mkcoordinateregion region = {coordinate, span };

// Set the region of the display interface to the mkcoordinateregion above.
Map. region = region;

// Display the current location
Map. showsuserlocation = yes;
// Zoom in and out
Map. zoomenabled = yes;
// Slip
Map. scrollenabled = yes;

// Set the type of the displayed map -------- three types
// Display the satellite map
// Map. maptype = mkmaptypesatellite;
// Display the mixture of a satellite map and a standard Map
// Map. maptype = mkmaptypehybrid;
// Display standard, that is, normal map. This is the default map type.
Map. maptype = mkmaptypestandard;

[Self. View addsubview: map];

// Add the map address manager ---- cllocationmanager
Self. locationmanger = [[cllocationmanager alloc] init];

// When the device moves more than the number of meters set by distancefilter, the location information is updated.
// It is understood that the map information is updated when the location of the map moves a certain distance.
Self. locationmanger. distancefilter = 100;

// The accuracy of the map location information, which is set to best here, which means that the accuracy is within 10 meters.
// Extern const cllocationaccuracy kcllocationaccuracybestfornavigation;
// Extern const cllocationaccuracy kcllocationaccuracybest;
// Extern const cllocationaccuracy kcllocationaccuracynearesttenmeters;
// Extern const cllocationaccuracy kcllocationaccuracyhundredmeters;
// Extern const cllocationaccuracy kcllocationaccuracykilometer;
// Extern const cllocationaccuracy kcllocationaccuracythreekilometers;

Self. locationmanger. desiredaccuracy = kcllocationaccuracybest;

// Set proxy

Self. locationmanger. Delegate = self;

// This sentence cannot be forgotten. It can be run only when startupdatinglocation is set.
[Self. locationmanger startupdatinglocation];
 
// Annotation class is very important. If you want to customize some text and icons on the map, you must use ------ annotation.
// Annotation class, which is used to provide annotation data and is used to display annotation views. You need to create an annotation in the delegate method.
Mkpointannotation * pointannotation = [[mkpointannotation alloc] init];


Pointannotation. Title = @ "Henan Province ";
Pointannotation. Subtitle = @ "Zhengzhou City ";

// Set the location of the annotation. ---- coordinate is required here.
Pointannotation. Coordinate = cllocationcoordinate2dmake (34.7568711, 113.663221 );

// Add the annotation to the map view
[MAP addannotation: pointannotation];

}

# Pragma mark-
# Pragma mark cllocationmanagerdelegate
// The first parameter indicates the address Manager
// The second parameter indicates the new location information when the location is updated.
// The third parameter indicates the old location information when the location is updated.


// The effect here is ------ change the position, and the map display changes the view interface as the position changes.
-(Void) locationmanager :( cllocationmanager *) Manager
Didupdatetolocation :( cllocation *) newlocation
Fromlocation :( cllocation *) oldlocation
{
Nslog (@ "newlocation's LAT is: % F, long is: % F", newlocation. Coordinate. Latitude, newlocation. Coordinate. longpolling );
Nslog (@ "oldlocation's LAT is: % F, long is: % F", oldlocation. Coordinate. Latitude, oldlocation. Coordinate. longpolling );

// Set Span
// Span refers to the display of map accuracy. The smaller the value, the more precise the map data is, the more detailed the map data is. If the value is greater, the greater the display area
// Here the C language method is called to assign values ------ c Assignment
Mkcoordinatespan span = mkcoordinatespanmake (1, 1 );

// Set the display area on the map
Mkcoordinateregion region = {newlocation. Coordinate, span };

// Here, tag is used to indicate that the actual interface after modification is the map we created previously
// Another method is to set it as a member attribute. You can also answer the result.
Mkmapview * map = (mkmapview *) [self. View viewwithtag: 1000];

// Set the display area of the map interface and add an animated effect,
// Similar to map. region = Region
[MAP setregion: Region animated: Yes];


// Calculate the distance between two locations, in meters ---- cllocationdistance

Cllocationdistance distance = [newlocation distancefromlocation: oldlocation];
Nslog (@ "the distance between two locations is: % F", distance/1000 );


// Call this method to terminate the location manager to update the location information. In actual applications, you must call this method to solve the power consumption problem of the device.
// [Self. locationmgr stopupdatinglocation];

}

# If 0
// This method and the above method are actually a method. After 6.0, this method is recommended by the system and exists at the same time, and the above method is invalid.
-(Void) locationmanager :( cllocationmanager *) Manager
Didupdatelocations :( nsarray *) Locations
{
// Print the face-to-face Method
Nslog (@ "% @", nsstringfromselector (_ cmd ));
Nslog (@ "% @", nsstringfromselector (@ selector (locationmanager: didupdatelocations :)));

// The last element of the array locations, which is the latest location information
// Use the address array of address manager to obtain the latest address
Cllocation * newlocation = locations [0];

// Set Span
Mkcoordinatespan span = mkcoordinatespanmake (1, 1 );
 
// Set Region
Mkcoordinateregion region = mkcoordinateregionmake (newlocation. Coordinate, span );

// Here, tag is used to indicate that the actual interface after modification is the map we created previously
// Another method is to set it as a member attribute. You can also answer the result.
Mkmapview * map = (mkmapview *) [self. View viewwithtag: 1000];

// Set the display area of the map interface and add an animated effect,
// Similar to map. region = Region
[MAP setregion: Region animated: Yes];

}
# Endif
-(Void) locationmanager :( cllocationmanager *) Manager
Didfailwitherror :( nserror *) Error
{
Nslog (@ "% s" ,__ func __);

Nslog (@ "% @", error );

}

# Pragma mark-
# Pragma mark mkmapviewdelegate
// Here is the specific proxy method for implementing Annotation
// There are two parameters: -------- mkmapview and ---------- annotation.
-(Mkannotationview *) mapview :( mkmapview *) mapview viewforannotation :( id <mkannotation>) Annotation
{
// If the current user address is the default user address, the default annotation is returned,
If ([annotation iskindofclass: [mkuserlocation class]) {
Return nil;
}

// The method for creating the annotation is similar to that for tabelview.
// Create an ID first
Static nsstring * pinviewindentifier [email protected] "pinviewindentifier ";

// Retrieve from reusable queues
Mkpinannotationview * pinview = (mkpinannotationview *) [mapview dequeuereusableannotationviewwithidentifier: pinviewindentifier];

// If it is null, create a pinview;
If (pinview = nil ){
Pinview = [[mkpinannotationview alloc] initwithannotation: annotation reuseidentifier: pinviewindentifier];
}

// Modify the color of the pin.
Pinview. pincolor = mkpinannotationcolorgreen;

// Show the effect of dropping from the sky when the pin appears
Pinview. animatesdrop = yes;

// If you want to display the preceding results, set canshowcallout to yes.
Pinview. canshowcallout = yes;

Return pinview;
}
@ End

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.