iOS 原生地图 开发


iOS开发有时候用到地图,不少人第一想到的是用第三方。当然有时候为了和安卓同步,可能会一起使用某一第三方。但有时候,我们是可以用原生地图开发的。上面两个示意图是原生地图的自定义开发。
运行demo,将展现图一界面,蓝色点是用户本人位置,弹出的大头针 是自己自定义 样式,另外 大头针是居中显示,我们同时可以设定地图放大的level效果。[maMapView setCenterCoordinate:location1 zoomLevel:10 animated:NO]; 我默认设置了 10, 你可以改变这个值,试试其他效果。
下面是核心代码以及git地址:
//
// MapViewController.m
// MapDemo
//
// Copyright © 2016年 qiye. All rights reserved.
//
#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height) #import "MapViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#import <MapKit/MKAnnotationView.h>
#import "QYAnnotation.h"
#import "MKMapView+ZoomLevel.h"
#import "DestinationMode.h" @interface MapViewController ()<MKMapViewDelegate> @end @implementation MapViewController{
UIView * uiView;
CLLocationManager * locationManager;
MKMapView * maMapView;
MKAnnotationView * annotationView;
DestinationMode * destinationMode;
} - (void)viewDidLoad {
[super viewDidLoad];
self.title = @"地图使用"; [self initMap];
} -(void) initMap
{ destinationMode = [DestinationMode initWithName:@"上海政府" desc:@"对,就是这个地方!" latitude:@"31.230344" longitude:@"121.47411346"]; maMapView=[[MKMapView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
[self.view addSubview:maMapView];
//设置代理
maMapView.delegate=self;
//请求定位服务
locationManager=[[CLLocationManager alloc]init];
if(![CLLocationManager locationServicesEnabled]||[CLLocationManager authorizationStatus]!=kCLAuthorizationStatusAuthorizedWhenInUse){
[locationManager requestWhenInUseAuthorization];
}
//用户位置追踪(用户位置追踪用于标记用户当前位置,此时会调用定位服务)
maMapView.userTrackingMode = MKUserTrackingModeFollow;
//设置地图类型
maMapView.mapType=MKMapTypeStandard;
//添加大头针
[self addAnnotation];
} -(void)addAnnotation{ CLLocationCoordinate2D location1=CLLocationCoordinate2DMake(destinationMode.destinationLatitude.floatValue, destinationMode.destinationLongitude.floatValue); QYAnnotation *annotation1=[[QYAnnotation alloc]init];
annotation1.title= destinationMode.destinationName;
annotation1.subtitle= destinationMode.destinationDesc;
annotation1.coordinate=location1; [maMapView setCenterCoordinate:location1 zoomLevel:10 animated:NO];
[maMapView addAnnotation:annotation1];
[maMapView selectAnnotation:annotation1 animated:YES]; } - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{ } - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
//由于当前位置的标注也是一个大头针,所以此时需要判断,此代理方法返回nil使用默认大头针视图
if ([annotation isKindOfClass:[QYAnnotation class]]) {
static NSString *key1=@"QYAnnotation";
annotationView=[mapView dequeueReusableAnnotationViewWithIdentifier:key1];
//如果缓存池中不存在则新建
if (!annotationView) {
annotationView=[[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:key1];
annotationView.canShowCallout=true;//允许交互点击
annotationView.calloutOffset=CGPointMake(0, 1);//定义详情视图偏移量
UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
[btn setBackgroundImage:[UIImage imageNamed:@"common_green_line"] forState:UIControlStateNormal];
[btn setTitle:@"到这去" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(turnAction:) forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView=btn;//定义详情左侧视图
annotationView.selected = YES;
}
//修改大头针视图
//重新设置此类大头针视图的大头针模型(因为有可能是从缓存池中取出来的,位置是放到缓存池时的位置)
annotationView.annotation=annotation;
annotationView.image=[UIImage imageNamed:@"common_map_site"];//设置大头针视图的图片 return annotationView;
}else {
return nil;
}
} -(void)turnAction:(id)sender{
[self doAppleNavigation];
} -(void)doAppleNavigation{
NSDictionary *options=@{MKLaunchOptionsMapTypeKey:@(MKMapTypeStandard),MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving};
CLLocationCoordinate2D fromCoordinate = maMapView.userLocation.location.coordinate;
CLLocationCoordinate2D toCoordinate = CLLocationCoordinate2DMake(destinationMode.destinationLatitude.floatValue, destinationMode.destinationLongitude.floatValue);
MKPlacemark *fromPlacemark = [[MKPlacemark alloc] initWithCoordinate:fromCoordinate
addressDictionary:nil]; MKPlacemark *toPlacemark = [[MKPlacemark alloc] initWithCoordinate:toCoordinate
addressDictionary:nil]; MKMapItem *fromItem = [[MKMapItem alloc] initWithPlacemark:fromPlacemark];
fromItem.name =@"当前位置";
MKMapItem *toItem=[[MKMapItem alloc]initWithPlacemark:toPlacemark];
toItem.name = destinationMode.destinationName;
[MKMapItem openMapsWithItems:@[fromItem,toItem] launchOptions:options];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
} @end
更多资料:
http://www.cnblogs.com/kenshincui/p/4125570.html#autoid-3-0-0
http://blog.csdn.net/nlforever/article/details/9075313
iOS 原生地图 开发的更多相关文章
- iOS原生地图开发指南续——大头针与自定义标注
iOS原生地图开发指南续——大头针与自定义标注 出自:http://www.sxt.cn/info-6042-u-7372.html 在上一篇博客中http://my.oschina.net/u/23 ...
- iOS原生地图开发详解
在上一篇博客中:http://my.oschina.net/u/2340880/blog/414760.对iOS中的定位服务进行了详细的介绍与参数说明,在开发中,地位服务往往与地图框架结合使用,这篇博 ...
- iOS原生地图开发进阶——使用导航和附近兴趣点检索
iOS原生地图开发进阶——使用导航和附近兴趣点检索 iOS中的mapKit框架对国际化的支持非常出色.在前些篇博客中,对这个地图框架的基础用法和标注与覆盖物的添加进行了详细的介绍,这篇博客将介绍两个更 ...
- IOS原生地图与高德地图
原生地图 1.什么是LBS LBS: 基于位置的服务 Location Based Service 实际应用:大众点评,陌陌,微信,美团等需要用到地图或定位的App 2.定位方式 1.GPS定位 ...
- iOS原生地图与高德地图的使用
原生地图 1.什么是LBS LBS: 基于位置的服务 Location Based Service 实际应用:大众点评,陌陌,微信,美团等需要用到地图或定位的App 2.定位方式 1.GPS定位 2. ...
- iOS 原生地图地理编码与反地理编码
当我们要在App实现功能:输入地名,编码为经纬度,实现导航功能. 那么,我需要用到原生地图中的地理编码功能,而在Core Location中主要包含了定位.地理编码(包括反编码)功能. 在文件中导入 ...
- iOS 原生地图(MapKit、MKMapView)轨迹渐变
WechatIMG2.png 项目已接入高德地图,并且大部分功能已经实现好,但BOSS觉得iOS自带的地图效果更好...本着面向老板编程的思想,换之.还好,高德地图是在MapKit上封装的,大部分ap ...
- 如何初始化一个iOS原生地图
/** 初始化一个mapView 需导入 #import <MapKit/MapKit.h> - returns: 返回一个mapView对象 */ mapView = [[MKMapV ...
- React Native Android原生模块开发实战|教程|心得|怎样创建React Native Android原生模块
尊重版权,未经授权不得转载 本文出自:贾鹏辉的技术博客(http://blog.csdn.net/fengyuzhengfan/article/details/54691503) 告诉大家一个好消息. ...
随机推荐
- FNN模糊神经网络——信息系统客户服务感知评价
案例描述 信息系统是否真正减轻业务人员的日常工作量提高工作效率?如何从提供“被动”服务转变为根据客户感知提供“主动”服务,真正实现电网企业对信息系统服务的有效管理?如何构建一套适合企业的信息系统客户服 ...
- Course Schedule 解答
Question There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may ...
- 小KING教你做android项目(一)
写在项目开始之前: 最近关于android的视频教程,入门的书籍也已经有很多了,例如我的入门就是看了mars的视频教程.但是这么一圈学习下来,觉得真正快速提高的,不是在看视频,而是在实际工作中动手做项 ...
- 所闻所获6:meditashayne项目总结
项目源码下载地址: https://github.com/ShayneYeorg/Meditashayne 1.首先一开始设计这个App的时候,我就希望它能比系统自带的备忘录更方便:比如备忘录需要手动 ...
- jQuery -> end方法的使用方法
我们在对结果集使用find.filter等方法时,会改变结果集. 这样的改变原先结果集的方法被称作destructive jQuery method jQuery cookbook有例如以下定义: A ...
- ModelAndView解析
查看spring的帮助文档得到下面信息: org.springframework.web.servlet Class ModelAndViewjava.lang.Object org.springfr ...
- HexColor
// // HexColor.swift // HexColor // // Created by Tuomas Artman on 1.9.2014. // Copyright (c) 2014 T ...
- 64bit ubuntu14.04编译PlatinumKit出现的arm-linux-androideabi-g++: not found错误解决方法
编译命令:scons target=arm-android-linux build_config=Release 出现错误: scons: Reading SConscript files ...** ...
- PES包头
PES是打包过的ES,已经插入PTS和DTS,一般是一个pes包为一帧图像 PES包格式: PES再打包成TS流或PS流,往往一个PES会分存到多个ts包中, start_code: 0x00 00 ...
- AjaxHelper的get和post请求的封装类
最近在学习ajax的时候发现不断的调用get和post请求时,代码重复很多,有的公司会用自带的封装的方法,这个可以直接调用ajax的方法,但是在运用的时候我们也应该学习它是怎么封装的和一些原理性的东西 ...