IOS Development Offline Map core code _ios

Source: Internet
Author: User
Tags zip uikit

One, the effect chart.


Second, engineering drawings.


Three, code.

ViewController.h

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import "MapLocation.h"
@ Interface Viewcontroller:uiviewcontroller
<MKMapViewDelegate>
{
  mkmapview *_mapview;
  NSString *addressstring;
}

Viewcontroller.m

 #import "ViewController.h" @interface Viewcontroller () @end @implementation Viewcontroller-(void) Viewdidload {[sup
  ER viewdidload];
  Do no additional setup after loading the view.
  Call the system's own gold map//show the current location of the offline map _mapview = [[Mkmapview alloc] init];
  _mapview.frame = CGRectMake (0, 40, 320,400);
  _mapview.delegate = self;
  _mapview.maptype = Mkmaptypestandard;
  [Self.view Addsubview:_mapview];
  addressstring=@ "Light Kai City";
  NSLog (@ "---addressstring---%@", addressstring);
[Self geocodequery]; }-(void) geocodequery{if (addressstring = = Nil | |
  [addressstring length] = = 0) {return;
  } clgeocoder *geocoder = [[Clgeocoder alloc] init]; [Geocoder geocodeaddressstring:addressstring completionhandler:^ (Nsarray *placemarks, NSError *error)
    {NSLog (@ "Query records:%ld", [Placemarks Count]);
    if ([placemarks count] > 0) {[_mapview removeannotations:_mapview.annotations]; for (int i = 0; i < [Placemarks count]; i++) {clplacemark* placemark = plaCemarks[i]; Adjust map position and zoom ratio mkcoordinateregion viewregion = mkcoordinateregionmakewithdistance (placemark.location.coordinate, 100
      00, 10000);
      [_mapview setregion:viewregion Animated:yes];
      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;
    [_mapview addannotation:annotation];
}
  }]; #pragma mark Map View Delegate Methods-(Mkannotationview *) Mapview: (Mkmapview *) Themapview viewforannotation: (ID < mkannotation>) Annotation {Mkpinannotationview *annotationview = (Mkpinannotationview *) [_mapview DequeueReusableA
  nnotationviewwithidentifier:@ "Pin_annotation"];
                  if (Annotationview = = nil) {Annotationview = [[Mkpinannotationview alloc] Initwithannotation:annotation           reuseidentifier:@ "Pin_annotation"];
  } Annotationview.pincolor = Mkpinannotationcolorpurple;
  Annotationview.animatesdrop = YES;
  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 DESCR
Iption]);  } @end

MapLocation.h

#import <MapKit/MapKit.h>
@interface maplocation:nsobject<mkannotation>
//Street Info Properties
@ Property (nonatomic, copy) NSString *streetaddress;
City Information attribute
@property (nonatomic, copy) NSString *city;
State, provincial and municipal information
@property (nonatomic, copy) NSString *state;
Zip code
@property (nonatomic, copy) NSString *zip;
Geographical coordinates
@property (nonatomic, ReadWrite) cllocationcoordinate2d coordinate;

Maplocation.m

Map Call function
#import "MapLocation.h"
@implementation maplocation
-(NSString *) title {return
  @ "your location!";
}
-(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

Related Article

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.