IPhone obtains the user coordinate details mkplacemark userlocation mkmapview

Source: Internet
Author: User

-(Ibaction) reversegeocodecurrentlocation

{

Self. reversegeocoder =

[[Mkreversegeocoderalloc] initwithcoordinate: mapview. userlocation. Location. Coordinate] autorelease];

Reversegeocoder. Delegate = self;

[Reversegeocoder
Start];

}

-(Void) reversegeocoder :( mkreversegeocoder *) geocoder didfailwitherror :( nserror *) Error

{

Nsstring * errormessage = [Error
Localizeddescription];

Uialertview * alertview = [[uialertviewalloc] initwithtitle: @ "cannot obtain address ."

Message: errormessage

Delegate: Nil

Cancelbuttontitle: @ "OK"

Otherbuttontitles: Nil];

[Alertview show];

[Alertview release];

}

-(Void) reversegeocoder :( mkreversegeocoder *) geocoder didfindplacemark :( mkplacemark *) placemark

{

Nslog (@ "reverse finished ...");
Nslog (@ "Country: % @", placemark. Country );
Nslog (@ "countrycode: % @", placemark. countrycode );
Nslog (@ "locality: % @", placemark. locality );
Nslog (@ "sublocality: % @", placemark. sublocality );
Nslog (@ "postalcode: % @", placemark. postalcode );
Nslog (@ "subthoroughfare: % @", placemark. subthoroughfare );
Nslog (@ "Thoroughfare: % @", placemark. thoroughfare );
Nslog (@ "administrativearea: % @", placemark. administrativearea );
Self. locationinfo. locationname = [nsstring stringwithformat: @ "% @, % @, % @",
Placemark. Country,
Placemark. administrativearea,
Placemark. locality,
Placemark. sublocality,
Placemark. thoroughfare];
Nslog (@ "administrativearea: % @", self. locationinfo. locationname );
// Incluself.tar get inclumselector: Self. Callback withobject: @ "0"];
Geocoder = nil;

}

-(Void) mapview :( mkmapview *) mapview didupdateuserlocation :( mkuserlocation *) userlocation

{

// We have received our current location, so enable the "get current address" button

[Getaddressbutton
Setenabled: Yes];

}

Below are some common Information reprinted

IOS map location Development

The iPhone SDK provides three categories to manage location information: cllocation cllocationmanager and cllheading (not commonly used ). In addition to using GPS to obtain the current location information, the iPhone can also obtain location information based on Wi-Fi base stations and Wireless Transmitting towers. GPS has the highest precision and can be accurate to the meter level, but it also consumes the most power.



------------ Cllocation

The cllocation class represents a location, which also includes the direction and speed. For example, I walked west at 188 Chang 'an street at a speed of 5 km/h. Cllocation has the following attributes and methods:

@ Property cllocationcoordinate2d coordinate; // location information expressed by longitude and latitude

@ Property cllocationdistance altitude; // altitude

@ Property cllocationaccuracy horizontalaccuracy; // horizontal precision (for example, accurate to meter)

@ Property cllocationaccuracy verticalaccuracy; // vertical precision

@ Property cllocationdirection course; // direction

@ Property cllocationspeed speed; // speed

-(Nsdate *) timestamp;

// Distance between two locations

-(Cllocationdistance) distancefromlocation :( cllocation *) location;



------------- Cllocationmanager

Cllocationmanager manages and provides location services. Its Attributes and methods include:

@ Property cllocation * location; // location

@ Property id <cllocationmanagerdelegate> delegate;

@ Property cllocationdistance distancefilter; // distance filter, for example, less than 500

@ Property cllocationaccuracy verticalaccuracy; // vertical precision

-(Void) startupdatinglocation; // start to update the location (for example, you are going somewhere)

-(Void) stopupdatinglocation; // stop the update location

-(Void) startupdatingheading; // start to update the direction (for example, you can change to East)

-(Void) stopupdatingheading; // stop the update direction.

Cllocationmanagerdelegate is a delegate class. Your application needs to use this delegate class. When the user changes the location, the cllocationmanager callback method is as follows:

-(Void) locationmanager :( cllocationmanager *) manager didupdatetolocation :( cllocation *) newlocation fromlocation :( cllocation *) oldlocation;

When the user changes the direction, the method called is:

-(Void) locationmanager :( cllocationmanager *) manager didupdateheading :( cllheading *) newheading;

When the iPhone cannot obtain information about the current position, the callback method is as follows:

-(Void) locationmanager: (cllocationmanager *) manager didfailloadwitherror :( nserror *) error;



========================================================== ======================================

Next we will look at the basic steps of a location class:

1. Start the positioning service

Cllocationmanager * locmanager = [[cllocationmanager alloc] init];

Locmanager. Delegate = self;

[Locmanager startupdatinglocation];

Ii. Obtain Location Information

-(Void) locationmanager :( cllocationmanager *) Manager

Didupdatetolocation :( cllocation *) newlocation fromlocation: (cllocation *) oldlocation

{

Nstimeinterval howrecent = [newlocation. timestamp timeintervalsincenow];

If (howrecent <-10) return; // The time from the last update is less than 10 seconds.

If (newlocation. horizontalaccuracy> 100) return; // precision> 100 meters

// Longitude and latitude

Double lat = newlocation. Coordinate. Latitude;

Double Lon = newlocation. Coordinate. longpolling;

}

3. Obtain direction information (for example, going south)

-(Void) locationmanager :( cllocationmanager *) manager didupdateheading :( clheading *) newheading

{

// Obtain the direction

Cllocationdirection heading = newheading. trueheading;

}

4. Stop locating

[Locmanager stopupdatinglocation];

You can set the desired precision and distance Filtering:

Locmanager. desiredaccuracy = kllocationaccuracybest;

Location manager. distancefilter = 1000;



Mapkit framework:

It mainly provides four functions: display the conversion between maps, cllocation and addresses, and support marking on maps (such as marking Tiananmen Square in Beijing), resolve a location into an address (for example, I want to know the exact address information in the Water Cube ).

Mkmapview class: mainly to complete the following functions:

------- Display a map, for example, displaying a map of Beijing;

------- Provides multiple display modes, such as standard map format and satellite map;

------- Supports zoom-in and zoom-out of maps;

------- Supports marking on maps, such as Tiananmen Square;

------- Display the current location of the mobile phone on the map.

The mkmapview class has the following attributes:

@ Property mkcoordinateregin region; // area displayed on the map

@ Property cllocationcoordinate2d centercoordinate; // The center location determined by the longitude and latitude

@ Property mkmapview maptype; // map display type, for example, satellite map

@ Property nsarray * annotations; // map flag

@ Property mkuserlocation userlocation; // user location

@ Property id <mkmapviewdelegate> delegate; // delegate class



The following callback methods are used to load a map:

-(Void) mapviewwillstartlocationmap :( mkmapview *) mapview; // starts to load the map.

-(Void) mapviewdidfinishlocationmap :( mkmapview *) mapview; // end map Loading

-(Void) mapvewdidfailloadingmap :( mkmapview *) mapview witherror :( nserror *) error; // loading failed



The callback method when the position changes:

-(Void) mapview :( mkmapview *) mapview regionwillchangeanimated :( bool) animated; // change

-(Void) mapview: (mkmapview *) mapview regiondidchangeanimated :( bool) animated; // changed



Mkplacemark, mkuserlocation, and mkreversegeocoder

The mkplacemark class is used to mark a map. This class uses (compliant) mkannotation protocol. Mkannotation contains multiple attributes, such as location (longitude and latitude, cllocationcoordinate2d type) and text Tag Information (nsstring type.

Mkplacemark stores the ing between the location (longitude and latitude) and the address (Dictionary class. The initialization method is as follows:

-(Void) initwithcoordinate :( cllocationcoordinate2d *) Coordinate addressdictionary :( nsdictionary *) dictionary;

Mkuserlocation refers to the current location of the mobile phone. It is a special case of mkannotation (because mkannotation can be any marker on the map, while mkuserlocation only marks the current location of the mobile phone on the map ). This class contains multiple attributes: mobile phone location (cllocation type), location text information (nsstring type), and so on.

Mkplacemark saves the ing between the location (latitude and longitude) and the address. Is there a tool to convert the two? This is mkrecersegeocoder. Given a location information, this class can return the corresponding address information. The initialization method of mkreversegeocoder is:

-(Void) initwithcoodinate :( cllocationcoordinate2d) coordinate;

The following are some common attributes and methods of mkreversegeocoder:

@ Property id <mkreversegeocoderdelegate> delegate; // delegate

-(Void) Start; // start Conversion

-(Void) Cancel; // cancel Conversion



The callback methods include:

-(Void) reversegeocoder :( mkreversegeocoder *) geocoded didfindplacemark :( mkplacemark *) placemark; // The conversion is successful.

-(Void) reversegeocoder: (mkreversegeocoder *) geocoded didfailwitherror :( nserror *) error; // Conversion failed

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.