使用特定的纬度和经度 Iphone 初始化 Mapkit
我正在制作一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我用您的代码制作了一个示例项目,它工作正常 - 地图被定位到预期的(看起来)区域。检查您的mapView ivar是否已初始化(IB中的连接设置是否正确?)
编辑:在您的代码中,您仅设置地图的可见区域,但不向其添加任何注释(除了自动显示当前用户位置之外)如果你在模拟器上测试,它总是在库比蒂诺)。要将图钉固定到地图上,您需要创建一个确认
MKAnnotation
协议的对象并将其添加到 mapView:代码上的一些(不那么相关)注释:
viewDidUnload
方法中调用[mapView setNeedsDisplay];
?我不确定这是否会导致严重问题,但您必须使用此方法来清理内存(例如释放保留的插座),而不是重新绘制 UII 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:Some (not so relevant) comments on your code:
[mapView setNeedsDisplay];
inviewDidUnload
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您是否已将地图设置为跟踪其他地方的用户位置?也许该默认值会否决您将地图手动移动到堪萨斯州的可能性。
编辑
呵,没想到这已经是去年七月了。如果这不是问题的话,也许您可以与我们分享您的解决方案
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