Map定位,标记位置的使用

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


1 [img]
[img]http://dl.iteye.com/upload/attachment/0078/6515/5becdd2d-9151-313c-a5da-a7e7c62e6057.png[/img]
[/img]


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


1、添加地图

1.1 新一个Single View app ,选择默认项,创建后,在ViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController
<MKMapViewDelegate, CLLocationManagerDelegate> {
MKMapView *map;
CLLocationManager *locationManager;
}
@end


1.2在ViewController.m中添加
- (void)viewDidLoad
{
map = [[MKMapView alloc] initWithFrame:[self.view bounds]];
map.showsUserLocation = YES;
map.mapType = MKMapTypeSatellite;
[self.view addSubview:map];


[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}



运行:

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

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

map.mapType =MKMapTypeStandard;

2[img]
[img]http://dl.iteye.com/upload/attachment/0078/6517/2ba82f86-9fcb-3b7c-80ae-bf4ce9bebfa8.png[/img]
[/img]
注意,如果此时你编译有错误,请拉到博客最后查看 :5、 遇到的问题


2、定位到指定经纬度:
CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(39.915352,116.397105);

float zoomLevel = 0.02;
MKCoordinateRegion region = MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomLevel, zoomLevel));
[map setRegion:[map regionThatFits:region] animated:YES];


这样,就我们就定位的了故宫了。
3 [img]
[img]http://dl.iteye.com/upload/attachment/0078/6519/848a7906-a320-3e5a-817d-b3de6f206d21.png[/img]
[/img]


3、添加标注大头针

3.1 新建一个标注类:CustomAnnotation

按Command+N,继承NSObject。在CustomAnnotation.h 和CustomAnnotation.m文件添加如下代码:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface CustomAnnotation : NSObject
<MKAnnotation>
{
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
-(id) initWithCoordinate:(CLLocationCoordinate2D) coords;

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *subtitle;

@end


#import "CustomAnnotation.h"

@implementation CustomAnnotation
@synthesize coordinate, title, subtitle;

-(id) initWithCoordinate:(CLLocationCoordinate2D) coords
{
if (self = [super init]) {
coordinate = coords;
}
return self;
}
@end



3.2使用大头针,

新建个方法添加大头针的
-(void)createAnnotationWithCoords:(CLLocationCoordinate2D) coords {
CustomAnnotation *annotation = [[CustomAnnotation alloc] initWithCoordinate:
coords];
annotation.title = @"标题";
annotation.subtitle = @"子标题";
[map addAnnotation:annotation];
}


调用
CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(39.915352,116.397105);

float zoomLevel = 0.02;
MKCoordinateRegion region = MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomLevel, zoomLevel));
[map setRegion:[map regionThatFits:region] animated:YES];


[self createAnnotationWithCoords:coords];


这样我们就把大头针定位在故宫了

4 [img]
[img]http://dl.iteye.com/upload/attachment/0078/6521/0e81c070-e591-3619-ae0b-543e2143fc38.png[/img]
[/img]


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

前面我们已经添加了locationManager,现在在DidViewLoad里直接调用
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];


实现协议方法收到定位成功后的经纬度
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
[locationManager stopUpdatingLocation];

NSString *strLat = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.latitude];
NSString *strLng = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.longitude];
NSLog(@"Lat: %@ Lng: %@", strLat, strLng);

}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"locError:%@", error);

}



5 [img]
[img]http://dl.iteye.com/upload/attachment/0078/6523/d003068c-732b-3aed-8c7c-f567200fe971.png[/img]
[/img]


定位后,移动到当前位置:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
[locationManager stopUpdatingLocation];

NSString *strLat = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.latitude];
NSString *strLng = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.longitude];
NSLog(@"Lat: %@ Lng: %@", strLat, strLng);

CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(newLocation.coordinate.latitude,newLocation.coordinate.longitude);
float zoomLevel = 0.02;
MKCoordinateRegion region = MKCoordinateRegionMake(coords,MKCoordinateSpanMake(zoomLevel, zoomLevel));
[map setRegion:[map regionThatFits:region] animated:YES];
}


6 [img]
[img]http://dl.iteye.com/upload/attachment/0078/6525/4253b015-a658-3c83-a6ce-1f81de2b1809.png[/img]
[/img]
定位到了当前位置。


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,添加方法如下:

7 [img]
[img]http://dl.iteye.com/upload/attachment/0078/6527/ff0a581a-c3bc-3671-99ec-51fcf04892eb.png[/img]
[/img]

选择项目,TARGETS ,点加号,添加两个framework
8 [img]
[img]http://dl.iteye.com/upload/attachment/0078/6529/47144cd5-980f-3040-bfac-910faf6ea04d.png[/img],[img]
[img]http://dl.iteye.com/upload/attachment/0078/6549/a21a50b7-911d-3ba6-a15d-256ee8da53f0.png[/img]
[/img]
[/img]
就好了。


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

5.0以上的模拟器才能用这个功能,打开模拟:
9 [img]
[img]http://dl.iteye.com/upload/attachment/0078/6531/bfb202ba-96bd-30d5-a911-4d5412af05d0.png[/img]
[/img]

这样就能发送模拟的当前位置了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值