地图的使用及GPS定位

学习了地图及大头针的使用。发现还是蛮

简单的就能实现对地图的操作。

首先,我们要了解苹果的定位组件:

Wifi定位,通过查询一个Wifi路由器的地理位置的信息。比较省电,iPod touch和iPad也可以采用。

蜂窝基站定位,通过移动运用商基站定位。也适合有3G版本的iPod touch和iPad。

GPS卫星定位,通过3-4颗GPS定位位置定位,最为准确,但是耗电量大,不能遮挡。

他们的好坏都在上面,选择用哪种,就看你自己的需求了。

其次,就是了解定位框架。Core Location是iPhone、iPad等开发定位服务应用程序的框架。我们要在Xcode中添加“CoreLocation.framework”存在的框架。主要使用的类是:CLLocationManager,通过CLLocationManager实现定位服务CLLocationManagerDelegate是定位服务的委托。

常用的位置变化回调方法是:locationManager: didUpdateToLocation: fromLocation: locationManager: didFailWithError:该委托方法不仅可以获得当前位置(newLocation),还可以获得上次的位置(oldLocation ),CLLocation 对象coordinate.latitude属性获得经度,coordinate.longitude属性获得纬度。

这里有比较全面的属性介绍:

CLLocationCoordinate2D coordinate; //以经度和纬度表示的位置信息

CLLocationDistance altitude;  //海拔

CLLocationAccuracy horizontalAccuracy; //水平精度(如:精确到米)

CLLocationAccuracy verticalAccuracy; //垂直精度

CLLocationDirection course; //行驶方向

CLLocationSpeed speed; //速度

//经度和纬度

coordinate.latitude;//纬度

coordinate.longitude; //经度

然后,就是大家最关心的代码部分了。

新建一个大头针类:

.h

#import

@interfaceLocationObject:NSObject{

CLLocationCoordinate2Dcoordinate;

NSString*_titleString;//title值

NSString*_subTitleString;

float_latitude;//经度值

float_longitude;//纬度值

}

@property(nonatomic,readonly)CLLocationCoordinate2Dcoordinate;

@propertyfloat_latitude;//经度值

@propertyfloat_longitude;//纬度值

@property(nonatomic,copy) NSString *_titleString;//title值

@property(nonatomic,copy) NSString *_subTitleString;

-(id)initWithTitle:(NSString*)atitle latitue:(float)alatitude longitude:(float)alongitude;

@end

---------------------

.m

@implementationLocationObject

@synthesizecoordinate,_latitude,_longitude,_titleString,_subTitleString;

-(id)initWithTitle:(NSString*)atitle latitue:(float)alatitude longitude:(float)alongitude

{

if(self=[superinit])

{

self._titleString=atitle;

self._latitude=alatitude;

self._longitude=alongitude;

}

returnself;

}

-(CLLocationCoordinate2D)coordinate;

{

CLLocationCoordinate2DcurrentCoordinate;

currentCoordinate.latitude=self._latitude;

currentCoordinate.longitude=self._longitude;

returncurrentCoordinate;

}

// 重写title和subtitle的set函数

- (NSString*)title

{

returnself._titleString;

}

-(NSString*)subtitle

{

return_subTitleString;

}

@end

然后在你需要添加的mapView控制类里面去创建,添加大头针。

1、创建注解:

LocationObject*aLocationObject=[[LocationObjectalloc]initWithTitle:nameStringlatitue:[latitudeStringfloatValue]longitude:[longitudeStringfloatValue]];

aLocationObject._subTitleString=addressString;

2、添加注解:

先构建一个注解数组NSMutableArray*_mapAnnotations;

然后

[self._mapAnnotationsaddObject:aLocationObject];

[self._mapViewaddAnnotations:self._mapAnnotations];

3、删除注解:

删除注解可执行removeAnnotation:一次只删除一个注解,或者执行removeAnnotation:删除一个数组中的所有项。

如果需要使地图视图回到无注解状态,可执行:

[self._mapViewremoveAnnotations:self._mapView.annotations];

删除其中全部注解,MKMapView的annotations属性获取了所有注解的数组,然后从地图上全部删除。

这样就OK了!还可以为大头针进行自定义哟

转载于:https://juejin.im/post/5a30f795f265da43085e0631

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 引入百度地图API的JS文件 ```html <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=你的AK值"></script> ``` 其中,AK值需要在百度地图开放平台申请获得。 2. 创建地图对象 ```javascript var map = new BMap.Map("container"); // container为地图容器的ID值 ``` 3. 启用GPS定位 ```javascript var geolocation = new BMap.Geolocation(); geolocation.getCurrentPosition(function(r){ if(this.getStatus() == BMAP_STATUS_SUCCESS){ var mk = new BMap.Marker(r.point); map.addOverlay(mk); map.panTo(r.point); } else { alert('failed'+this.getStatus()); } },{enableHighAccuracy: true}) ``` 其中,BMap.Geolocation()为百度地图API提供的GPS定位功能,getCurrentPosition()为获取当前位置信息的方法。如果定位成功,将当前位置标记在地图上,并将地图中心点设置为当前位置。如果定位失败,弹出提示框。 完整代码如下: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>百度地图GPS定位</title> <style type="text/css"> html,body,#container{ width:100%; height:100%; margin:0; font-family:"微软雅黑"; } </style> <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=你的AK值"></script> </head> <body> <div id="container"></div> <script type="text/javascript"> // 创建地图对象 var map = new BMap.Map("container"); // 启用GPS定位 var geolocation = new BMap.Geolocation(); geolocation.getCurrentPosition(function(r){ if(this.getStatus() == BMAP_STATUS_SUCCESS){ var mk = new BMap.Marker(r.point); map.addOverlay(mk); map.panTo(r.point); } else { alert('failed'+this.getStatus()); } },{enableHighAccuracy: true}) </script> </body> </html> ``` 注意事项: 1. 需要在HTTPS环境下使用GPS定位功能。 2. 定位功能需要用户授权,浏览器会弹出提示框,用户需要点击允许才能使用定位功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值