【问题标题】:Zooming to users location [closed]缩放到用户位置[关闭]
【发布时间】:2013-12-24 19:55:48
【问题描述】:

我正在制作这个应用程序,人们可以在其中查看附近的旅游景点,但我有点卡住了!

我需要它在加载应用程序时加载用户的当前位置并缩放到它,但它没有这样做。

抱歉有点生硬,但我真的不知道如何设置它,以便它可以与按钮一起使用并自动加载它们的位置。

https://pbs.twimg.com/media/BcRSTk0CAAEV47Q.jpg

ViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface ViewController : UIViewController{

    MKMapView *mapview;

}

@property (weak, nonatomic) IBOutlet MKMapView *myMapView;
-(IBAction)getlocation;

@end

ViewController.m

#import "ViewController.h"
#import "Annotation.h"

@interface ViewController ()

@end

//Thorpe Park Coordinates
#define THORPE_LATITUDE 51.40395;
#define THORPE_LONGITUDE -0.51433;

//London Eye Coordinates
#define LONDONEYE_LATITUDE 51.50340;
#define LONDONEYE_LONGITUDE -0.11952;

//The New London Dungeons
#define LONDONDUGNEONS_LATITUDE 51.50231;
#define LONDONDUGNEONS_LONGITUDE -0.11965;


//Span
#define THE_SPAN 0.50f;



@implementation ViewController
@synthesize myMapView;

-(IBAction)getlocation {
    mapview.showsUserLocation = YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    //Create Region
    MKCoordinateRegion myRegion;

    //Center
    CLLocationCoordinate2D center;
    center.latitude = LONDONEYE_LATITUDE;
    center.longitude = LONDONEYE_LONGITUDE;

    //Span
    MKCoordinateSpan span;
    span.latitudeDelta = THE_SPAN;
    span.longitudeDelta = THE_SPAN;

    myRegion.center = center;
    myRegion.span = span;



    //Set our mapView
    [myMapView setRegion:myRegion animated:YES];


    //Annotation's
    NSMutableArray * locations = [ [NSMutableArray alloc] init];
    CLLocationCoordinate2D location;
    Annotation *myAnn;

    //London Eye
    myAnn = [[Annotation alloc] init];
    location.latitude = LONDONEYE_LATITUDE;
    location.longitude = LONDONEYE_LONGITUDE;
    myAnn.coordinate = location;
    myAnn.title = @"The London Eye";
    myAnn.subtitle = @"";
    [locations addObject:myAnn];

    //The New London Dungeons
    myAnn = [[Annotation alloc] init];
    location.latitude = LONDONDUGNEONS_LATITUDE;
    location.longitude = LONDONDUGNEONS_LONGITUDE;
    myAnn.coordinate = location;
    myAnn.title = @"The London Dungeons";
    myAnn.subtitle = @"";
    [locations addObject:myAnn];

    //Thorpe Park
    myAnn = [[Annotation alloc] init];
    location.latitude = THORPE_LATITUDE;
    location.longitude = THORPE_LONGITUDE;
    myAnn.coordinate = location;
    myAnn.title = @"Thorpe Park";
    myAnn.subtitle = @"";
    [locations addObject:myAnn];

    [self.myMapView addAnnotations:locations];



    /*
    //1. Create a coordinate to be used with pin
    CLLocationCoordinate2D ThorpeLocation;
    ThorpeLocation.latitude = THORPE_LATITUDE;
    ThorpeLocation.longitude = THORPE_LONGITUDE;

    Annotation * myAnnotation = [Annotation alloc];
    myAnnotation.coordinate = ThorpeLocation;
    myAnnotation.title = @"Thorpe Park";
    myAnnotation.subtitle = @"Theme Park";

    [self.myMapView addAnnotation:myAnnotation];
    */

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

【问题讨论】:

    标签: ios objective-c android-mapview


    【解决方案1】:

    使用这个,别忘了把委托放在你的 .h 文件中,希望它有效;)

        _mapView.userTrackingMode = YES;
    
    - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    
        MKCoordinateRegion region;
        MKCoordinateSpan span;
    
        span.latitudeDelta = 0.00001;
        span.longitudeDelta = 0.00001;
    
        CLLocationCoordinate2D location = _mapView.userLocation.coordinate;
    
        region.span = span;
        region.center = location;
    
        [_mapView setRegion:region animated:TRUE];
        [_mapView regionThatFits:region];
    
    }
    

    【讨论】:

    • 天哪,太棒了,非常感谢!圣诞快乐:D
    • 谢谢!也祝你圣诞快乐! :D
    猜你喜欢
    • 2017-05-02
    • 2011-08-29
    • 2014-04-03
    • 1970-01-01
    • 1970-01-01
    • 2017-10-04
    • 2011-08-11
    • 2013-03-30
    • 1970-01-01
    相关资源
    最近更新 更多