Mapkit Study Notes
1. Overview
Insert Mapview, set delegate (typically controller), annotations record interest location points (Annotationview is used to show interest location points), annotation is optional, The selected annotation displays callout, which is used to display the information.
2. Set the map display type:
Mapview.maptype = Mkmaptypestandard;
Mapview.maptype = Mkmaptypesatellite;
Mapview.maptype = Mkmaptypehybrid;
3. Display User Location
Set to display the user's location:
Mapview.showsuserlocation = YES;
Determines whether the user's current location is visible (read-only property):
Userlocationvisible
Get user location coordinates: When userlocationvisible is yes
cllocationcoordinate2d coords = mapView.userLocation.location.coordinate;
4. Coordinate range
The mkcoordinateregion is used to set the coordinate display range.
Includes two parts: center (cllocationcoordinate2d struct, including latitude and longitude), coordinate center
and span (Mkcoordinatespan struct, including Latitudedelta and Longitudedelta), zoom level
Mkcoordinateregion viewregion = Mkcoordinateregionmakewithdistance (center,2000, 2000);
The above code to create a center-centric, up and down each 1000 meters, the left and right of the 1000-meter area, but it is a rectangle, does not conform to the mapview of the vertical ratio
Mkcoordinateregion adjustedregion = [Mapview regionthatfits:viewregion];
The above code creates an area that conforms to the Mapview vertical scale.
[Mapview setregion:adjustedregion Animated:yes];
The above code is: final display of the area
5, Delegate
Use of Mapview must comply with Mkmapviewdelegate protocol
5.1. Map Loading delegate
When you need to get a new map from Google server
Mapviewwillstartloadingmap:
When you have successfully obtained the map,
Mapviewdidfinishloadingmap:
When the map has failed (it is recommended to implement this method at least)
Mapviewdidfailloadingmap:witherror:
5.2. Range Change Delegate
When the gesture starts (drag, zoom in, zoom out, double-click)
Mapview:regionwillchangeanimated:
When the gesture is over (drag, zoom in, zoom out, double-click)
Mapview:regiondidchangeanimated:
Determine if the coordinates are within the Mapview display range:
Cllocationdegrees leftdegrees = mapview.region.center.longitude– (mapview.region.span.longitudedelta/2.0);
Cllocationdegrees rightdegrees = MapView.region.center.longitude + (mapview.region.span.longitudedelta/2.0);
Cllocationdegrees bottomdegrees = mapview.region.center.latitude– (mapview.region.span.latitudedelta/2.0);
Cllocationdegrees topdegrees = Self.region.center.latitude + (mapview.region.span.latitudedelta/2.0);
if (Leftdegrees > Rightdegrees) {//Int ' L Date line in View
Leftdegrees = -180.0–leftdegrees;
if (Coords.longitude > 0)//coords to West of the Date line
Coords.longitude = -180.0–coords.longitude;
}
If (leftdegrees <= coords.longitude && coords.longitude <= rightdegrees && bottomdegrees <= coor Ds.latitude && coords.latitude <= topdegrees) {
Coordinates are within range
}6, Annotation
Annotation contains two parts: Annotation object and Annotation View
Annotation object must conform to protocol mkannotation, including two methods: Title and subtitle, respectively, to display the caption and sub-headings of the note. There is also the coordinate property, which returns CLLOCATIONCOORDINATE2D, indicating the location of the annotation
Then, you need to use Mapview:viewforannotation: method to return a subclass of Mkannotationview or Mkannotationview to display the annotation (note: This does not show the pop-up box after the annotation is selected)
You can subclass the Mkannotationview and then DrawRect: Do your own drawing action in the method (this method is stupid)
You can simply instantiate a mkannotationview and then change its Image property, which is simple.
7. Add Remove annotation
Add a annotation
[Mapview addannotation:annotation];
Add a annotation array
[Mapview Addannotations:[nsarray Arraywithobjects:annotation1, Annotation2, Nil]];
Removing a annotation
Removeannotation:
Remove a annotation array
Removeannotations:
Remove all annotation
[Mapview removeAnnotations:mapView.annotations];
8. Select annotation
Only one annotation can be selected at a time, callout (floating box) will appear when selected
Simple callout display title and subtitle, but you can also customize a uiview as callout (as with custom Tableviewcell)
Annotation can be checked by code:
Selectannotation:animated:
or cancel the selection:
Deselectannotation:animated:
9. Display annotation
This method is called by the Mapview:viewforannotation: method to display annotation, each of which joins a annotation in Mapview
Example (as Tableview:cellforrowatindexpath: very similar)
-(Mkannotationview *) Mapview: (Mkmapview *) Themapview viewforannotation: (id <MKAnnotation>) annotation {
static NSString *placemarkidentifier = @ "My annotation identifier";
if ([Annotation iskindofclass:[myannotation class]]) {
Mkannotationview *annotationview = [Themapview dequeuereusableannotationviewwithidentifier:placemarkidentifier];
if (Annotationview = = nil) {
Annotationview = [[Mkannotationview alloc] initwithannotation:annotation reuseidentifier:placemarkidentifier];
Annotationview.image = [UIImage imagenamed:@ "Blood_orange.png"];
}
Else
annotationview.annotation = annotation;
return annotationview;
}
return nil;
}
10. Get the real address
Example:
Initialize Mkreversegeocoder
Mkreversegeocoder *geocoder = [[Mkreversegeocoder alloc] initwithcoordinate:coordinates];
Geocoder.delegate = self;
[Geocoder start];
If the coordinates cannot be processed, call the Reversegeocoder:didfailwitherror: method
-(void) Reversegeocoder: (Mkreversegeocoder *) Geocoder didfailwitherror: (nserror *) Error {
NSLog (@ "error resolving coordinates:%@", [Error localizeddescription]);
Geocoder.delegate = nil;
[Geocoder Autorelease];
}
If successful, call Reversegeocoder:didfindplacemark: and store the information in Mkplacemark
Didfindplacemark: (Mkplacemark *) Placemark {
NSString *streetaddress = Placemark.thoroughfare;
NSString *city = placemark.locality;
NSString *state = Placemark.administrativearea;
NSString *zip = Placemark.postalcode;
Do something with information
Geocoder.delegate = nil;
[Geocoder Autorelease];
}
*******************************************************
User Location:Mapviews uses the core location to save the user's path and indicate it with a blue dot on the map. You can open: Mapview.showuserlocation = YES; If the map is tracking the user's unknown, you can use the read-only property userlcoationvisible to detect whether the user's current location is visible. If yes, it is visible. You can first set Showsuserlocation to Yes to the user's current specified coordinates. Then access the Userlocation property. This property returns an instance variable of mkuserlocation. Mkuserlocation is an object that has a property called location (cllocation type). A cllocation contains a coordinate attribute that points to a collection of coordinates, all of which means that you can get the actual coordinates from the Mkuserlocation object: subordinate that: cllocationcoordinae2d coords = MapView.userLocation.location.coordinate;
coordinate regionsMap view is less smart if you don't tell me what to show or find a specific location in the world. The key to doing this through map view is mkcoordinateregion, a structure that contains two parts of the data, and also defines where the map view species is to be displayed. The first member is a center. This is another structure type: Cllocationcoordinate2d, a cllocationcoordinate2d contains two floating-point values, longitude and latitude. This point represents the middle of the map view. The second one is called span. Is the structure of the Mkcoordinatespan type. It has two programs called Latitudedelta and Longitudedelta. The two programs are used to set the zoom level of the map-how much area should be displayed around the center. These values represent the distance between longitude and latitude. If the Latitudedelta and Longitudedelta are small, the map will be scaled very dense, and if large, the map will be enlarged and a larger area displayed.
Convert degree to distanceEach latitude represents 69 miles, or 111km, wherever you are. This makes it easier to calculate the latitudedelta that are passed as Mkcoordinatespan parameters. The distance represented by longitude is not so easy to calculate. In order to do the same calculation, you must use latitude, because it represents the distance from the equator relative to where you are. In order to calculate the distance represented by longitude, you must perform some mathematical calculations. In fact, Apple has provided some way to do such calculations: Mkcoordinateregionmakewithdistance () creates a region. You provide coordinates as center, distance (m) for longitude and latitude of span. For example, create a region to display the specified area location of 1km. By calling cllocationcoordinate2d mkcoordinateregion viewregion = mkcoordinateregionmakewithdistance (center, 2000, 2000); To show the 1km around each edge, we must specify 2000m for each span:1000m to the left, 1000m to the right, 1000m up, 1000m down. After the call, Viewregion will contain a formatted mkcoordinateregion, of course you can use it. The rest is the ratio conversion problem.
Total Cross ratio:Mkmapview An instance method will adapt to a coordinate area to match the map view's horizontal scale. Regionthatfits: When using, you simply pass in the coordinate area you created, and it will return a new coordinate area to fit the map view scale. Mkcoordinateregion adjustedregion = [Mapview regionthatfits:viewregion[;
Setting area display:Once you have created the coordinate area, you can tell map view to display the area created by the setregion:animated: method. If you pass yes to the second argument, the Mapview will be scaled to move, and so on. Mkcoordinateregion viewregion = mkcoordinateregionmakewithdistance (center,2000,2000); Mkcoordinateregion adjustedregion = [Mapview regionthatfits:viewregion]; [Mapview setregion:adjustedregion Animated:yes];p osted @ 2012-04-23 16:23 翛 Yao Read (13) Comments (0) Edit
March 18, 2012 #
NSDate use
1 nsstring* timestr = @ "2011-01-26 17:40:50";
2 NSDateFormatter *formatter = [[[[NSDateFormatter alloc] init] autorelease]; 3 [Formatter Setdatestyle: Nsdateformattermediumstyle]; 4 [Formatter settimestyle:nsdateformattershortstyle]; 5 [Formatter setdateformat:@ "Yyyy-mm-dd HH:mm:ss"];//----------Set the format you want, the difference between HH and HH: 12-hour system, 24-hour 6 7// Set the time zone, which is sometimes very important for the processing of the Times 8//For example, you publish information in the country, the user in another time zone abroad, you want to let the user see the correct release time to pay attention to the TIMEZONE setting, time conversion. 9//For example, if you posted the time of 2010-01-26 17:40:50, what should the user see in the UK and Ireland? 10//They have a 7-hour time difference with us, so they're not there yet. That's what's going to happen in the future. nstimezone* timeZone = [Nstimezone timezonewithname:@ "Asia/shanghai"];13 [Formatter settimezone:timezone];14 nsdate* date = [Formatter datefromstring:timestr];//-convert string to formatter by Nsdate16 - NSDate *datenow = [NSDate date];//now time, you can output to see what format the nsstring *nowtimestr = [Formatter stringfromdate: datenow];//convert NSDate to NSString by formatter format
Time stamp
NSString *timesp = [NSString stringwithformat:@ "%d", (long) [Datenow timeIntervalSince1970]]; NSLog (@ "timesp:%@", TIMESP); Timestamp value timestamp Time-stamping method nsdate *confromtimesp = [NSDate datewithtimeintervalsince1970:1296035591]; NSLog (@ "1296035591 =%@", confromtimesp); NSString *confromtimespstr = [formatter stringfromdate:confromtimesp]; NSLog (@ "confromtimespstr = %@", confromtimespstr);
Mapkit Study Notes