IOS:百度地图学习2

     使用MapView须符合MKMapViewDelegate协议

    建立单视图工程,再建一个新类,该类是自定义的大头针,.h文件如下:

    

#import <Foundation/Foundation.h>

#import <MapKit/MKAnnotation.h>


@interface CustomAnnotation :NSObject<MKAnnotation>

{

    CLLocationCoordinate2D coordinate_;

NSString *title_;

NSString *subtitle_;

}

@property (nonatomicassignCLLocationCoordinate2D coordinate;

@property (nonatomiccopyNSString *title;

@property (nonatomiccopyNSString *subtitle;


@end



.m 文件如下

#import "CustomAnnotation.h"


@implementation CustomAnnotation

@synthesize coordinate =coordinate_;

@synthesize title = title_;

@synthesize subtitle =subtitle_;

-(void)dealloc

{

[ release];

        [subtitle_ release];

[super dealloc];

}

@end




根视图.h文件:

#import <UIKit/UIKit.h>

#import <MapKit/MapKit.h>

#import "CustomAnnotation.h"

@interface QQViewController :UIViewController<MKMapViewDelegate>

@property (weak, nonatomic) IBOutletMKMapView *map;


@end

根视图.m文件

#import "QQViewController.h"


@interface QQViewController ()


@end


@implementation QQViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

   self.map.delegate =self;

    //设置地图格式

    self.map.mapType = MKMapTypeStandard;

    

    CLLocationCoordinate2D loc =CLLocationCoordinate2DMake(32.0500,118.78333);

    CLLocationDegrees lat = loc.latitude;

    CLLocationDegrees lon = loc.longitude;

    

    CLLocationCoordinate2D theCoordinate;

    //地图中心

    CLLocationCoordinate2D theCenter;

    theCoordinate.latitude = lat;

    theCoordinate.longitude = lon;

    //设置地图坐标显示范围

    MKCoordinateRegion theRegin;

    theCenter.latitude = lat;

    theCenter.longitude = lon;

    theRegin.center = theCenter;

    //地图缩放级别

    MKCoordinateSpan theSpan;

    theSpan.latitudeDelta =0.01;

    theSpan.longitudeDelta =0.01;

    theRegin.span = theSpan;

    [self.mapsetRegion:theRegin];

    [self.mapregionThatFits:theRegin];

    //显示用户位置

    self.map.showsUserLocation =YES;

    //创建大头针

    CustomAnnotation *nan = [[CustomAnnotationalloc]init];

    //大头针标题

    nan.title =@"增加的大头针";

   NSString *locDesc = [NSStringstringWithFormat:@"纬度:%.1f经度:%.1f",lat,lon];

    //标注的位置

    nan.coordinate = loc;

    //大头针副标题

    nan.subtitle = locDesc;

    [self.mapaddAnnotation:nan];

    

// Do any additional setup after loading the view, typically from a nib.

}


#pragma ================MKMapViewDelegate===============================

//改变大头针图表的颜色或者图片

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation

{

   MKAnnotationView *newAnimation = [[MKAnnotationView alloc]initWithAnnotation:annotationreuseIdentifier:annotation.title];

    newAnimation.canShowCallout =YES;

    newAnimation.image = [UIImageimageNamed:@"location"];

   return newAnimation;

}



运行结果:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值