IOS input location Displays the current location map and labels with a pin

Source: Internet
Author: User

Not much nonsense said directly on the project projects, or first look at it!

The project has two files, Chonviewcontroller and maplocation files, maplocation file for the landmark display and additional information, yesterday when the additional information displayed normal, today want to write a blog but not show, very depressed!



After the new project remember to join the class library, Mapkit.framework specific Add method is not explained here

Below is a look at the ChonViewController.h file code as follows:

#import <UIKit/UIKit.h> #import <MapKit/MapKit.h> #import "MapLocation.h" @interface Chonviewcontroller: Uiviewcontroller<mkmapviewdelegate> @property (Weak, nonatomic) Iboutlet Uitextfield *txtquerykey; @property ( Weak, nonatomic) Iboutlet Mkmapview *mapview;-(ibaction) Geocodequery: (ID) sender; @end

The chonviewcontroller.m file code is as follows:

chonviewcontroller.m//mylocation2////Created by Choni on 14-5-13.//Copyright (c) 2014 Choni. All rights reserved.//#import "ChonViewController.h" @interface Chonviewcontroller () @end @implementation    chonviewcontroller-(void) viewdidload{[Super Viewdidload];    Callout Map Type _mapview.maptype = Mkmaptypestandard; The delegate property used to assign the current view controller to the map view _mapview.delegate = self;}   -(void) didreceivememorywarning{[Super didreceivememorywarning]; } #pragma mark-query button trigger action-(ibaction) Geocodequery: (ID) Sender {if (_txtquerykey.text = = Nil | |    [_txtquerykey.text length] = = 0) {return;        } clgeocoder *geocode = [[Clgeocoder alloc] init]; [GeoCode geocodeaddressstring:_txtquerykey.text completionhandler:^ (Nsarray *placemarks, NSError *error)                {NSLog (@ "Query Record Count:%i", [Placemarks Count]);                     if ([placemarks count] > 0) {//Remove all labels on the current map [_mapview removeannotations:_mapview.annotations];         }       for (int i = 0; i< [placemarks Count]; i++) {Clplacemark * placemark = placemarks[i];            Close the keyboard [_txtquerykey Resignfirstresponder]; Adjust the map position and scale, the first parameter is the center point of the target area, the second parameter: the span of the north and south of the target area, and the third parameter: the object span of the target area, the units are meters mkcoordinateregion viewregion = mkcoordinate                        Regionmakewithdistance (Placemark.location.coordinate, 10000, 10000);            Reset the display area of the map view [_mapview setregion:viewregion Animated:yes];            Instantiate MapLocation Object maplocation * annotation = [[MapLocation alloc] init];            annotation.streetaddress = Placemark.thoroughfare;            annotation.city = placemark.locality;            Annotation.state = Placemark.administrativearea;            Annotation.zip = Placemark.postalcode;                        Annotation.coordinate = placemark.location.coordinate; Add the callout point MapLocation object to the map view, and once the method is called, the Map View delegate method Mapview:viewforannotation: It will be callback [_mapview AddannOtation:annotation];    }                    }]; } #pragma mark Mapview Delegate map When adding callout callback-(Mkannotationview *) Mapview: (Mkmapview *) Themapview viewforannotation: (ID &lt ; mkannotation>) Annotation {//Get Map Callout Object Mkpinannotationview * Annotationview = (Mkpinannotationview *) [_mapview D    equeuereusableannotationviewwithidentifier:@ "Pin_annotation"]; if (Annotationview = = nil) {Annotationview = [[Mkpinannotationview alloc] Initwithannotation:annotation reuseIdent    ifier:@ "Pin_annotation"];    }//Set the pin-label gaze to purple annotationview.pincolor = Mkpinannotationcolorpurple;    Whether the map is displayed as an animated effect on the map annotationview.animatesdrop = YES;        For some additional information on the callout point annotationview.canshowcallout = YES;    return annotationview; }-(void) Mapview: (Mkmapview *) Mapview didupdateuserlocation: (mkuserlocation *) userlocation{_ Mapview.centercoordinate = userLocation.location.coordinate;} -(void) Mapviewdidfailloadingmap: (Mkmapview *) Themapview witherror: (nserror *) error{NSLog (@ "error:%@", [error description]);} @end

The MapLocation.h code is as follows:

  maplocation.h//  mylocation2////  Created by Choni on 14-5-13.//  Copyright (c) 2014 Choni. All rights reserved.//#import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface maplocation: nsobject<mkannotation>//map Punctuation class must implement Mkannotation Protocol//geo-coordinates @property (nonatomic, ReadWrite) cllocationcoordinate2d coordinate;//street attribute Information @property (nonatomic, copy) NSString * streetaddress;//City Information Properties @property (non Atomic, copy) NSString * City,//state, provincial and municipal information @property (nonatomic, copy) NSString *;//Zip @property (nonatomic, copy) NS String * Zip  ; @end
The maplocation.m file is as follows:

  maplocation.m//  mylocation2////  Created by Choni on 14-5-13.//  Copyright (c) 2014 Choni. All rights reserved.//#import "MapLocation.h" @implementation the main title on the maplocation#pragma Mark punctuation-(NSString *) title{    return @ "Your location!";} #pragma  the subtitle on Mark Punctuation-(NSString *) subtitle{    nsmutablestring *ret = [nsmutablestring new];    if (_state) {        [ret appendstring:_state];    }    if (_city) {        [ret appendstring:_city];    }    if (_city && _state) {        [ret appendstring:@, '];    }    if (_streetaddress && (_city | | _state | | _zip)) {        [ret appendstring:@ ' · "];    }    if (_streetaddress) {        [ret appendstring:_streetaddress];    }    if (_zip) {        [ret appendformat:@,  %@ ", _zip];    }    return ret;} @end

Ok, the comments in the code is still more detailed!


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.