Map,定位,标记位置的使用

IOS上使用地图比Android要方便,只需要新建一个MKMapView,addSubView即可。这次要实现的效果如下:

有标注(大头针),定位,地图。

 

1、添加地图

1.1 新一个Single View app ,选择默认项,创建后,在ViewController.h 

  1. #import <UIKit/UIKit.h>  
  2. #import <MapKit/MapKit.h>  
  3. #import <CoreLocation/CoreLocation.h>  
  4.   
  5. @interface ViewController : UIViewController   
  6. <MKMapViewDelegate, CLLocationManagerDelegate> {  
  7.     MKMapView *map;  
  8.     CLLocationManager *locationManager;  
  9. }  
  10. @end  

1.2在ViewController.m中添加

  1. - (void)viewDidLoad  
  2. {  
  3.     map = [[MKMapView alloc] initWithFrame:[self.view bounds]];  
  4.     map.showsUserLocation = YES;  
  5.     map.mapType = MKMapTypeSatellite;  
  6.     [self.view addSubview:map];  
  7.   
  8.   
  9.     [super viewDidLoad];  
  10.     // Do any additional setup after loading the view, typically from a nib.  
  11. }  
运行:

OMG,看到的是世界地图。怎么定位到指定的位置呢?比如定位回来伟大的祖国首都?

这里map.mapType =MKMapTypeSatellite;我用到是卫星地图,可以使用标准的地图,

map.mapType =MKMapTypeStandard;


注意,如果此时你编译有错误,请拉到博客最后查看 :5、 遇到的问题

2、定位到指定经纬度

  1. CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(39.915352,116.397105);  
  2.       
  3.     float zoomLevel = 0.02;  
  4.     MKCoordinateRegion region = MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomLevel, zoomLevel));  
  5.     [map setRegion:[map regionThatFits:region] animated:YES];  
  6.           

这样,就我们就定位的了故宫了。

3、添加标注大头针

3.1 新建一个标注类:CustomAnnotation

按Command+N,继承NSObject。在CustomAnnotation.h 和CustomAnnotation.m文件添加如下代码:

  1. #import <Foundation/Foundation.h>  
  2. #import <MapKit/MapKit.h>  
  3.   
  4. @interface CustomAnnotation : NSObject   
  5. <MKAnnotation>  
  6. {  
  7.     CLLocationCoordinate2D coordinate;  
  8.     NSString *title;  
  9.     NSString *subtitle;  
  10. }  
  11. -(id) initWithCoordinate:(CLLocationCoordinate2D) coords;  
  12.   
  13. @property (nonatomic, readonly) CLLocationCoordinate2D coordinate;  
  14. @property (nonatomic, retain) NSString *title;  
  15. @property (nonatomic, retain) NSString *subtitle;  
  16.   
  17. @end  
  1. #import "CustomAnnotation.h"  
  2.   
  3. @implementation CustomAnnotation  
  4. @synthesize coordinate, title, subtitle;  
  5.   
  6. -(id) initWithCoordinate:(CLLocationCoordinate2D) coords  
  7. {  
  8.     if (self = [super init]) {  
  9.         coordinate = coords;  
  10.     }  
  11.     return self;  
  12. }  
  13. @end  

3.1 使用大头针,

新建个方法添加大头针的

  1. -(void)createAnnotationWithCoords:(CLLocationCoordinate2D) coords {  
  2.     CustomAnnotation *annotation = [[CustomAnnotation alloc] initWithCoordinate:   
  3.                                     coords];  
  4.     annotation.title = @"标题";  
  5.     annotation.subtitle = @"子标题";  
  6.     [map addAnnotation:annotation];  
  7. }  

调用
  1. CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(39.915352,116.397105);  
  2.       
  3.     float zoomLevel = 0.02;  
  4.     MKCoordinateRegion region = MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomLevel, zoomLevel));  
  5.     [map setRegion:[map regionThatFits:region] animated:YES];  
  6.   
  7.       
  8.     [self createAnnotationWithCoords:coords];  
这样我们就把大头针定位在故宫了


4、定位到当前位置并获取当前经纬度

前面我们已经添加了locationManager,现在在DidViewLoad里直接调用

  1. locationManager = [[CLLocationManager alloc] init];  
  2.     locationManager.delegate = self;  
  3.     [locationManager startUpdatingLocation];  

 

实现协议方法收到定位成功后的经纬度
  1. - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {  
  2.     [locationManager stopUpdatingLocation];  
  3.       
  4.     NSString *strLat = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.latitude];  
  5.     NSString *strLng = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.longitude];  
  6.     NSLog(@"Lat: %@  Lng: %@", strLat, strLng);  
  7.   
  8. }  
  9.   
  10. - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {  
  11.     NSLog(@"locError:%@", error);  
  12.   
  13. }  

运行,允许获取当前位置,打印log

  1. 2012-06-28 23:58:32.237 MapDemo[8202:11603] Lat: 39.9011  Lng: 116.3000  
如果不允许:打印出错误日志
  1. 2012-06-28 23:25:03.109 MapDemo[7531:11603] locError:Error Domain=kCLErrorDomain Code=1 "The operation couldn’t be completed. (kCLErrorDomain error 1.)"  

定位后,移动到当前位置:

  1. - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {  
  2.     [locationManager stopUpdatingLocation];  
  3.       
  4.     NSString *strLat = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.latitude];  
  5.     NSString *strLng = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.longitude];  
  6.     NSLog(@"Lat: %@  Lng: %@", strLat, strLng);  
  7.       
  8.     CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(newLocation.coordinate.latitude,newLocation.coordinate.longitude);  
  9.     float zoomLevel = 0.02;  
  10.     MKCoordinateRegion region = MKCoordinateRegionMake(coords,MKCoordinateSpanMake(zoomLevel, zoomLevel));  
  11.     [map setRegion:[map regionThatFits:region] animated:YES];  
  12. }  


定位到了当前位置。

5、会遇到的问题:

运行是发现了编译错误:

Undefined symbols for architecture i386:

  "_CLLocationCoordinate2DMake", referenced from:

      -[ViewController viewDidLoad] in ViewController.o

      -[ViewController locationManager:didUpdateToLocation:fromLocation:] in ViewController.o

  "_OBJC_CLASS_$_MKMapView", referenced from:

      objc-class-ref in ViewController.o

  "_OBJC_CLASS_$_CLLocationManager", referenced from:

      objc-class-ref in ViewController.o

ld: symbol(s) not found for architecture i386

clang: error: linker command failed with exit code 1 (use -v to see invocation)

这是为什么呢?没有添加对应的FrameWork。我使用的是4.3.2版本的XCode,添加方法如下:

选择项目,TARGETS ,点加号,添加两个framework

就好了。

如何发送IOS模拟器经纬度?

5.0以上的模拟器才能用这个功能,打开模拟:

这样就能发送模拟的当前位置了。

 

 

例子代码:http://download.csdn.net/detail/totogo2010/4400001点击打开链接

https://github.com/schelling/YcDemo/tree/master/MapDemo


转载于:https://my.oschina.net/jackyyang/blog/67216

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 微信小程序是一种轻量化的应用程序,可以在微信直接使用。它具有快速开发、轻便易用等优点。其,微信小程序map组件可以用于定位当前位置。 首先,在小程序开发工具map组件,可以利用微信提供的API进行对地图的操作。然后,在页面上添按钮或者其他交互方式,触发定位功能。可以通过微信提供的wx.getLocation接口获取当前地理位置,将其坐标转换为经纬度等信息,再在地图上显示出来。 微信小程序map组件还可以入自定义的标记点、路线等功能。比如,可以在地图上设置一个目标地点,当用户到达该地点时进行提醒。除此之外,还可以将地图与其他组件联动,实现更多实用性功能。 总之,通过微信小程序map组件定位当前位置可以在很多场景下得到运用,例如共享单车、打车等领域,也为用户带来更便利和舒适的出行体验。 ### 回答2: 微信小程序的定位功能可以让用户在地图上查看自己的当前位置,并且可以进行定位导航等一系列应用操作。 要使用微信小程序的定位功能,首先需要获取用户的授权。用户可以通过点击相关按钮进行授权,同时小程序需要向用户说明获取授权的作用和用途。 获取授权后,就可以通过小程序调用微信地图API,实现对用户当前位置定位功能。用户当前位置会在地图上进行标注,并且可以实现定位导航、查询周边信息等功能。 除了获取授权和调用API之外,开发者还需要考虑到用户体验和隐私保护等方面。比如,在页面显示用户当前位置时要注意保护用户隐私,使用合适的地图标识等。 总体来说,微信小程序的定位功能可以方便用户在不同场景下使用地图,同时也增了小程序的实用性。对开发者而言,需要注意用户隐私和开发规范等方面的问题,以保证用户体验和小程序功能的完善。 ### 回答3: 微信小程序提供了一种方便的方法,可以使用map组件定位当前位置。这个组件可以用来显示地图和标记位置,还可以选择地图的种类和放大程度。在小程序,我们只需要在wxml文件map组件,然后在js文件调用API即可进行位置定位。 首先,我们需要引入wx.getLocation() API,这个API可以获得当前用户的地理位置。通过这个API获得的经纬度信息,我们可以通过地图定位到用户的当前位置。在js文件如下代码,就可以实现定位操作: ``` wx.getLocation({ type: 'gcj02', success(res) { const latitude = res.latitude const longitude = res.longitude const speed = res.speed const accuracy = res.accuracy } }) ``` 其,type参数可以设置定位方式,gcj02表示国测局密经纬度坐标,还有其他选项,具体可以查看文档。通过上述代码,我们得到用户的经纬度位置,并可以通过map组件显示在地图上。 ``` <map latitude="{{latitude}}" longitude="{{longitude}}" scale="{{scale}}" markers="{{markers}}" show-location="{{true}}" /> ``` 最后,在wxml文件map组件即可。其,latitude和longitude参数分别表示经纬度位置,scale参数表示显示比例尺,markers参数可以设置地图标记,show-location参数表示是否显示定位标记。 总之,通过微信小程序的map组件和wx.getLocation() API,我们可以轻松实现定位当前位置的操作。这一功能在很多小程序都会用到,比如地图导航、外卖送餐等等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值