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) 告诉大家一个好消息. ...
随机推荐
- windows平台vhd磁盘文件挂载
在windows平台下挂载vhd磁盘文件类似于挂载iso等文件; 使用VHDMount工具挂载VHD文件 启动Hyper-V里的外部VHD文件有点困难.如果在备份驱动上有个VHD文件,并需要从其虚拟机 ...
- HOWTO Use Python in the web — Python v3.0.1 documentation
HOWTO Use Python in the web - Python v3.0.1 documentation mod_python¶ People coming from PHP often f ...
- 关于 require的缓存
有两个文件 a.js内容如下: var add = require("./t.js").add; var add2 = require("./t.js").ad ...
- 如何煉成NET架構師
微软的DotNet 开发绝对是属于那种入门容易提高难的技术.而要能够成为DotNet 架构师没有三年或更长时间的编码积累基本上是不可能的.特别是在大型软件项目中,架构师是项目核心成员,承上启下,因此 ...
- pthread_t definition
近期在看google的chromium的代码,认为其基础库base中的对于与平台有关的线程的数据结构的定义与其代码中的凝视部分不匹配. // PlatformThreadHandle should n ...
- 用MVC4+EF改写XXX系统的计划--前言
感觉自己工作了三年,重来没有自己一个人写一个项目,从开始的策划,功能需求,业务逻辑,扩展,性能优化等等方面去做,从今天起准备发比半年时间重写XXX项目,每天中午和晚上分别花半个小时和一个小时开发,周末 ...
- SDK编程模板
#include<Windows.h> LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); int WINAPI WinMain(HINS ...
- [转]MFC 加载其他的应用程序
三个SDK函数 winexec, shellexecute,createprocess可以使用.WinExec 最简单,两个参数,前一个指定路径,后一个指定显示方式.后一个参数值得说一下,比如泥用 S ...
- jQuery工具函数上
1.字符串操作 <!DOCTYPE html> <html> <head lang="en"> <meta charset="U ...
- select语句返回结果的顺序问题 .
今天看到论坛上一个朋友的回帖内容,突然意识到自己好像从来没对SELECT语句做过任何思考,即便SELECT是平时使用最多的语句.自己建了两个测试表,内容如下: SQL> conn scott/t ...