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) 告诉大家一个好消息. ...
随机推荐
- 产生n bit所有可能的序列
void binary(int n) { ) printf("%s\n",A); // Assume A is a global variable else { A[n-] = ' ...
- uva 1589 by sixleaves
坑爹的模拟题目.自己对于这种比较复杂点得模拟题的能力概述还不够,还多加练习.贴别是做得时候一直再想如何检查车中间有没有棋子,炮中间有没有棋子.到网上参考别人的代码才发先这么简单的办法,自己尽然想不到. ...
- 异常:ERROR [org.hibernate.proxy.BasicLazyInitializer] - CGLIB Enhancement failed...
ERROR [org.hibernate.proxy.BasicLazyInitializer] - CGLIB Enhancement failed: com.movie.类 放到lib 包下 \W ...
- centos网速特别慢的最佳解决的方法 - 关闭ipv6
我使用了centOS,可是发现网速实在是卡得差点儿不能上网,连百度都打不开,可是win却飞快. 后来想到偶然记得有一次看过一段话,说到关闭ipv6,測试来一下,果然有效,关闭来ipv6打开网速飞快. ...
- HTML中IE条件注释判断语句(<!--[if XX IE X]><![endif]-->)
<!--[if XX IE X]><![endif]-->是IE专门提供的一种语法,其他浏览器会将其作为注释而忽略这些语句. 作用: 根据不同的IE版本加载对应的CSS或者 ...
- 【转载自友盟消息推送iOS文档】在appDelegate中注册推送
1.2 基本功能集成指南 提示 请先在友盟的消息推送管理后台中创建App,获得AppKey和AppSecret 导入SDK 下载 UMessage_Sdk_All_x.x.x.zip并解压缩 导入 ...
- 转:C#: static关键字的作用
tatic意思是静态,可以修饰类.字段.属性.方法 标记为static的就不用创建实例对象调用了,可以通过类名直接点出来 static三种用法: 1.用于变量前,表示每次重新使用该变量所在方法.类或自 ...
- C#同步数据库的数据到Neo4J
数据组件采用https://github.com/Readify/Neo4jClient 在nuget里面有 需要注意的是 以下是示例代码: using System;using System.Col ...
- <link>: rel, href
Reference: http://www.w3schools.com/tags/tag_link.asp <link> Attributes: Attribute Value Descr ...
- plsql基本语法(
1. 定义常量的语法格式 常量名 constant 类型标识符 [not null]:=值; 常量,包括后面的变量名都必须以字母开头,不能有空格,不能超过30个字符长度,同时不能和保留字同 ...