zoukankan      html  css  js  c++  java
  • iPhone之获取当前位置

    来自:http://blog.sina.com.cn/s/blog_4adf31ea010176hq.html

    首先,加入地图包

    接口代码:

    #import <UIKit/UIKit.h>
    #import <MapKit/MapKit.h>
    
    @interface View30 : UIViewController <CLLocationManagerDelegate,MKMapViewDelegate>
    {
         MKMapView *map;
    
    }
    @end

    实现代码:

    - (void)viewDidLoad {
    
        [super viewDidLoad];
    
    //创建位置管理器
    
    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    
    //设置代理
    
    locationManager.delegate=self;
    
    //指定需要的精度级别
    
    locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    
    //设置距离筛选器
    
    locationManager.distanceFilter=500.0f;
    
    //启动位置管理器
    
    [locationManager startUpdatingLocation];
    
     
    
    MKCoordinateRegion theRegion = { {0.0, 0.0 }, { 0.0, 0.0 } };
    
    theRegion.center=[[locationManager location] coordinate];
    
    [locationManager release];
    
     
    
    //设置地图
    
    map=[[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    
    map.delegate=self;
    
    //是否显示用户的位置信息
    
    map.showsUserLocation=YES;
    
    //向上滚动 
    
    [map setZoomEnabled:YES];
    
    //横向滚动
    
    [map setScrollEnabled:YES];
    
    //设置地图范围  越小越精确
    
    theRegion.span.longitudeDelta = 0.05f;
    
    theRegion.span.latitudeDelta = 0.05f;
    
    [map setRegion:theRegion animated:YES];
    
    [self.view addSubview:map];
    
     
    
    }
    
    
    - (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
    
    {//注释指针
    
    MKPinAnnotationView *pinView = nil;
    
     
    
    static NSString *defaultPinID = @"mylocation";
    
    pinView = (MKPinAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    
    if ( pinView == nil ) 
    
    {
    
    pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
    
    }
    
    pinView.pinColor = MKPinAnnotationColorRed;
    
    pinView.canShowCallout = YES;
    
    pinView.animatesDrop = YES;
    
    [map.userLocation setTitle:@"我的位置"];
    
    [map.userLocation setSubtitle:@"小标题"];
    
    return pinView;
    
    }

  • 相关阅读:
    nowcoderD Xieldy And His Password
    Codeforces681D Gifts by the List
    nowcoder80D applese的生日
    Codeforces961E Tufurama
    Codeforces957 Mahmoud and Ehab and yet another xor task
    nowcoder82E 无向图中的最短距离
    nowcoder82B 区间的连续段
    Codeforces903E Swapping Characters
    Codeforces614C Peter and Snow Blower
    Codeforces614D Skills
  • 原文地址:https://www.cnblogs.com/appwgh/p/2528032.html
Copyright © 2011-2022 走看看