Original address: iOS 6 Apple map application (mapkit)-built-in Development
This article is a comment on the Apple case regiondefiner.
# Import "viewcontroller. H "# import" myannotation. H "@ implementation viewcontroller-(void) viewdidload {[Super viewdidload]; self. mainmapview = [[mkmapview alloc] init]; self. mainmapview. delegate = self; [self. mainmapview setuserinteractionenabled: Yes]; [self. mainmapview setscrollenabled: Yes]; uitoolbar * toolbar = [[uitoolbar alloc] init]; toolbar. autoresizingmask = uiviewautoresizingflexibletopmarg In | uiviewautoresizingflexiblewidth; [toolbar sizetofit]; cgsize containersize = self. view. bounds. size; cgsize toolbarsize = toolbar. bounds. size; uibarbuttonitem * resetbutton = [[uibarbuttonitem alloc] initwithtitle: @ "reset" style: uibarbuttonitemstylebordered target: Self action: @ selector (removepins)]; uibarbuttonitem * flexiblespace = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem: uibarbu Ttonsystemitemflexiblespace target: Nil action: Nil]; uibarbuttonitem * share = [[uibarbuttonitem alloc] initwithtitle: @ "log" style: Custom target: Self action: @ selector (tappedshare)]; toolbar. frame = cgrectmake (0, containersize. height-toolbarsize. height, containersize. width, toolbarsize. height); self. mainmapview. frame = cgrectmake (0, 0, containersize. width, containersize. he Ight-toolbarsize. height); [self. view addsubview: Self. mainmapview]; [self. view addsubview: toolbar]; // Add "reset button", "blank", "log button" [toolbar setitems: @ [resetbutton, flexiblespace, share] to the toolbar. [self setupgesture]; self. itemsarray = [[nsmutablearray alloc] init];}-(void) viewdidunload {[self setmainmapview: Nil]; [Super viewdidunload];}-(bool) implements :( uiinterfaceorientation) Interfaceorientation {If ([[uidevice currentdevice] userinterfaceidiom] = uiuserinterfaceidiomphone) {return (interfaceorientation! = Uiinterfaceorientationportraitupsidedown);} else {return YES ;}# Pragma mark-# pragma mark dropping pins // Add a long-pressed gesture-(void) setupgesture {self. longpress = [[uilongpressgesturerecognizer alloc] initwithtarget: Self action: @ selector (handlelongpress :)]; self. longpress. delegate = self; [self. view addgesturerecognizer: Self. longpress];} // process the long-pressed gesture-(void) handlelongpress :( uilongpressgesturerecognizer *) recognizer {If (recognizer. state = uigesturerecognizerstatebegan) {cgpoint longpresspoint = [recognizer locationinview: Self. view]; // draw the pin [self droppinatpoint: longpresspoint];}-(void) updatepolygon {cllocationcoordinate2d * points = malloc (sizeof (cllocationcoordinate2d) * self. itemsarray. count); nsuinteger I = 0; For (myannotation * pin in self. itemsarray) {// Add the longitude and latitude points [I] = pin for all coordinates. coordinate; I ++;} // clear the previously drawn image on the map [self. mainmapview removeoverlay: Self. polygon]; // draw a polygon self. polygon = [mkpolygon polygonwithcoordinates: points count: Self. itemsarray. count]; [self. mainmapview addoverlay: Self. polygon];}-(void) droppinatpoint: (cgpoint) pointtoconvert {// converts the point in the view to the longitude and latitude cllocationcoordinate2d convertedpoint = [self. mainmapview convertpoint: pointtoconvert tocoordinatefromview: Self. view]; // the title of the pin nsstring * pintitle = [nsstring stringwithformat: @ "pin number % I", self. itemsarray. count]; // the sub-title of the pin, displaying the longitude and latitude nsstring * subcoordinates = [nsstring stringwithformat: @ "% F, % F", convertedpoint. latitude, convertedpoint. longpolling]; myannotation * droppedpin = [[myannotation alloc] initwithcoordinate: convertedpoint title: pintitle Subtitle: subcoordinates]; // Add this mark to the map [self. mainmapview addannotation: droppedpin]; // Add the array [self. itemsarray addobject: droppedpin]; // update the polygon [self updatepolygon];}-(void) removepins {// remove the mark [self. mainmapview removeannotations: Self. mainmapview. annotations]; // reset [self. itemsarray removeallobjects]; // clear the drawn polygon [self. mainmapview removeoverlay: Self. polygon]; // update a polygon [self updatepolygon];} # pragma mark-output // operation log-(void) tappedshare {nslog (@ "% @", [self coordinates]);}-(nsstring *) coordinates {// at least three points if (self. itemsarray. count <3) {return @ "minimum of 3 vertices requried to make polygon. ";} nsstring * masterstring = @" \ n {\ "Type \": \ "multipolygon \", \ n \ "coordinates \": [\ n [["; for (myannotation * pin in self. itemsarray) {masterstring = [masterstring stringbyappendingformat: @ "[% F, % F], \ n", pin. coordinate. longpolling, pin. coordinate. latitude];} // geojson requires that the first and last vertices be identical myannotation * firstpin = [self. itemsarray objectatindex: 0]; masterstring = [masterstring stringbyappendingformat: @ "[% F, % F], \ n", firstpin. coordinate. longpolling, firstpin. coordinate. latitude]; masterstring = [masterstring stringbyappendingstring: @ "] \ n} \ n"]; masterstring = [masterstring stringbyreplacingoccurrencesofstring: @ "], \ n] "withstring: @"] "]; return masterstring ;}# Pragma mark-mkmapviewdelegate-(mkoverlayview *) mapview :( mkmapview *) mapview viewforoverlay :( id <mkoverlay>) Overlay {If (self. polygonview & self. polygonview. polygon = self. polygon) return self. polygonview; self. polygonview = [[mkpolygonview alloc] initwithpolygon: Self. polygon]; // fill color self. polygonview. fillcolor = [uicolor colorwithred: 0 Green: 1 Blue: 0 ALPHA: 0.3f]; // The border color self. polygonview. strokecolor = [uicolor colorwithred: 0 Green: 1 Blue: 0 ALPHA: 0.9f]; // edge width self. polygonview. linewidth = 1.0f; return self. polygonview;} @ end