使用特定的纬度和经度 Iphone 初始化 Mapkit

发布于 2024-09-10 14:27:40 字数 1272 浏览 4 评论 0原文

我正在制作一个 iPhone 应用程序,它应该从数组中获取纬度和经度,并使用自定义注释/图钉定位和显示地图。我已经使用了mapkit,具体方法如下:

//MapViewController.h

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

@interface MapViewController : UIViewController  <MKMapViewDelegate> {
 IBOutlet MKMapView *mapView;
 MKPlacemark *mPlacemark;
}

@property (nonatomic, retain) IBOutlet MKMapView *mapView;
@end


//MapViewController.m

#import "MapViewController.h"
#import <MapKit/MapKit.h>

@implementation MapViewController
@synthesize mapView;


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    CLLocationCoordinate2D location = mapView.userLocation.coordinate;
    MKCoordinateRegion region;
    MKCoordinateSpan span;

    location.latitude  = 37.250556;
    location.longitude = -96.358333;

    span.latitudeDelta = 0.05;
    span.longitudeDelta = 0.05;

    region.span = span;
    region.center = location;

    [mapView setRegion:region animated:YES];
    [mapView regionThatFits:region];
}

 - (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    [mapView setNeedsDisplay];    
}


@end

这不会定位纬度和经度的位置。请帮忙。

Iam making an iphone app, which should take in latitude and longitudes from an array and should locate and display the map with a customized annotation/pins. I have used the mapkit here's how:

//MapViewController.h

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

@interface MapViewController : UIViewController  <MKMapViewDelegate> {
 IBOutlet MKMapView *mapView;
 MKPlacemark *mPlacemark;
}

@property (nonatomic, retain) IBOutlet MKMapView *mapView;
@end


//MapViewController.m

#import "MapViewController.h"
#import <MapKit/MapKit.h>

@implementation MapViewController
@synthesize mapView;


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    CLLocationCoordinate2D location = mapView.userLocation.coordinate;
    MKCoordinateRegion region;
    MKCoordinateSpan span;

    location.latitude  = 37.250556;
    location.longitude = -96.358333;

    span.latitudeDelta = 0.05;
    span.longitudeDelta = 0.05;

    region.span = span;
    region.center = location;

    [mapView setRegion:region animated:YES];
    [mapView regionThatFits:region];
}

 - (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    [mapView setNeedsDisplay];    
}


@end

This does not locate the place for the latitude and longitude. Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

渡你暖光 2024-09-17 14:27:40

我用您的代码制作了一个示例项目,它工作正常 - 地图被定位到预期的(看起来)区域。检查您的mapView ivar是否已初始化(IB中的连接设置是否正确?)

编辑:在您的代码中,您仅设置地图的可见区域,但不向其添加任何注释(除了自动显示当前用户位置之外)如果你在模拟器上测试,它总是在库比蒂诺)。要将图钉固定到地图上,您需要创建一个确认 MKAnnotation 协议的对象并将其添加到 mapView:

// Sample example just to show annotation
// in your program you will likely need to use custom annotation objects 
CLLocation* myLocation = [[CLLocation alloc] initWithLatitude:37.250556 longitude:-96.358333];
[mapView addAnnotation:myLocation];

代码上的一些(不那么相关)注释:

  • 您不需要使用以下命令来初始化位置变量:当前位置,因为您立即覆盖其坐标值,
  • 为什么您在 viewDidUnload 方法中调用 [mapView setNeedsDisplay]; ?我不确定这是否会导致严重问题,但您必须使用此方法来清理内存(例如释放保留的插座),而不是重新绘制 UI

I have made a sample project with your code and it works fine - map gets positioned to the expected (it seems) region. Check if your mapView ivar is initialized (are connections set properly in IB?)

Edit: In your code you just set map's visible region but do not add any annotations to it (apart from automatically showing current user position which is always in Cupertino if you test on simulator). To put pin to you map you need to create an object confirming to MKAnnotation protocol and add it to mapView:

// Sample example just to show annotation
// in your program you will likely need to use custom annotation objects 
CLLocation* myLocation = [[CLLocation alloc] initWithLatitude:37.250556 longitude:-96.358333];
[mapView addAnnotation:myLocation];

Some (not so relevant) comments on your code:

  • You don't need to initialize location variable with current location as you overwrite its coordinate values immediately after that
  • Why do you call [mapView setNeedsDisplay]; in viewDidUnload method? I'm not sure if this may cause serious problems but you must use this method for cleaning memory up (e.g. releasing retained outlets), not for redrawing your UI
若水微香 2024-09-17 14:27:40

您是否已将地图设置为跟踪其他地方的用户位置?也许该默认值会否决您将地图手动移动到堪萨斯州的可能性。

编辑
呵,没想到这已经是去年七月了。如果这不是问题的话,也许您可​​以与我们分享您的解决方案

Have you set your map to follow user location somewhere else? maybe that default is overruling your manual movement of the map to Kansas.

edit
Do'h, didn't see this was July last year. Well maybe you could share your solution with us if this wasn't the problem

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文