好久没写了,赶项目你懂得。
直接上图吧,╮(╯▽╰)╭。
#import <UIKit/UIKit.h>
@interface SearchViewController : UIViewController
//纬度
@property (nonatomic, copy) NSString *latitude; NSString * latitu
@property nonatomic copy NSSstring
//经度
@property (nonatomic, copy) NSString *longitude;
//地名
@property (nonatomic, copy) NSString *addressName;
@end
//
// SearchViewController.m
// CircleFace
// Created by chylee on 16/5/17.
// Copyright © 2016年 . All rights reserved.
#import "SearchViewController.h"
#import <MapKit/MapKit.h>
#import "MapAnnotation.h"
@interface SearchViewController ()<MKMapViewDelegate>{
MKMapView *mapView;
}
@end
@implementation SearchViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [UIBarButtonItem createBarButtonItemWithTitle:nil imageName:@"返回白色" highlightedImageName:@"返回白色" target:self selector:@selector(back) font:0];
mapView = [[MKMapView alloc]initWithFrame:self.view.bounds];
[self.view addSubview:mapView];
// 设置代理
mapView.delegate = self;
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([_latitude doubleValue],[_longitude doubleValue]);
MapAnnotation *annotation = [[MapAnnotation alloc] init];
annotation.coordinate = coordinate;
annotation.title = _addressName;
[mapView addAnnotation:annotation];
[mapView setRegion:MKCoordinateRegionMakeWithDistance(coordinate, 2000, 2000)];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
MKPinAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MapSample"];
annotationView.canShowCallout = YES;
return annotationView;
}
- (void)back {
[self.navigationController popViewControllerAnimated:YES];
}
@end
因为需要在地图上显示经纬度对应位置,使用了MapAnnotation,新建一个annotion类,继承NSObject,为这个类设置两个属性,CLLocationCoordinate2D、title.
//
// MapAnnotation.h
// CircleFace
//
// Created by chylee on 16/5/17.
// Copyright © 2016年 . All rights reserved.
//
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MapAnnotation : NSObject <MKAnnotation>
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@end