gpt4 book ai didi

ios - map 无法显示我的位置

转载 作者:搜寻专家 更新时间:2023-10-30 20:19:25 24 4
gpt4 key购买 nike

我想显示 map 并显示位置。我的代码运行良好,但位置无法显示在 map 上。我不知道该怎么做。

这是我的类(class):

ViewController.h

@interface ViewController : 
UIViewController<AGSMapViewLayerDelegate,CLLocationManagerDelegate>
{
AGSMapView *_mapView;
AGSGraphicsLayer *_graphicsLayer;
AGSTiledMapServiceLayer *_tiledLayer;
CLLocationManager *locationManager;
CLLocation *startingPoint;
}

@property (nonatomic,retain) IBOutlet AGSMapView *mapView;
@property (nonatomic, retain) AGSTiledMapServiceLayer *tiledLayer;
@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) AGSGraphicsLayer *graphicsLayer;
@property (nonatomic, retain) CLLocation *startingPoint;

ViewController.m

@interface ViewController()

@end

@implementation ViewController

@synthesize mapView = _mapView;
@synthesize graphicsLayer = _graphicsLayer;
@synthesize tiledLayer = _tiledLayer;
@synthesize locationManager;
@synthesize startingPoint;

- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView.layerDelegate =self;
self.graphicsLayer = [AGSGraphicsLayer graphicsLayer];
[self.mapView addMapLayer:self.graphicsLayer withName:@"graphicsLayer"];

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self; // send loc updates to myself
self.locationManager.distanceFilter = 1000.0f; // 1 kilometer
self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
}

- (void)mapViewDidLoad:(AGSMapView *)mapView{
[self.locationManager startUpdatingLocation]; //启用位置监控
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
self.startingPoint = [locations lastObject];
[self.locationManager stopUpdatingLocation]; //获取位置信息

CGPoint coord;
coord.x = startingPoint.coordinate.longitude;
coord.y = startingPoint.coordinate.latitude;

//将坐标用AGSPoint来表示
AGSPoint *mappoint = [[AGSPoint alloc]initWithX:coord.x y:coord.y spatialReference:nil];
//[self.graphicsLayer removeAllGraphics];
AGSPictureMarkerSymbol *pt;
pt = [AGSPictureMarkerSymbol pictureMarkerSymbolWithImageNamed:@"LocationDisplay.png"];
AGSGraphic *LocationDisplay = [[AGSGraphic alloc] initWithGeometry:mappoint symbol:pt attributes:nil infoTemplateDelegate:nil];

// 添加要素到GraphicsLayer
[self.graphicsLayer addGraphic:LocationDisplay];
[self.graphicsLayer refresh];

//漫游到指定级别
[self.mapView centerAtPoint:mappoint animated:YES];
int levelToZoomTo = 12;
AGSLOD* lod = [self.tiledLayer.mapServiceInfo.tileInfo.lods objectAtIndex:levelToZoomTo];
float zoomFactor = lod.resolution/self.mapView.resolution;
AGSMutableEnvelope *newEnv = [AGSMutableEnvelope envelopeWithXmin:self.mapView.visibleAreaEnvelope.xmin
ymin:self.mapView.visibleAreaEnvelope.ymin
xmax:self.mapView.visibleAreaEnvelope.xmax
ymax:self.mapView.visibleAreaEnvelope.ymax
spatialReference:self.mapView.spatialReference];
[newEnv expandByFactor:zoomFactor];
[self.mapView zoomToEnvelope:newEnv animated:YES];
}

最佳答案

当您从 CLLocation 坐标创建 AGSPoint 时,您需要提供空间引用。使用 [AGSSpatialReference wgs84SpatialReference]

如果 map 未使用 WGS84 空间引用,则您需要先将该点重新投影到 map 空间引用,然后再使用几何引擎将其添加到 map 。

您可以使用 AGSMapView 内置的 locationDisplay 属性,通过设置 mapView.locationDisplay.startDataSource 来显示您的当前位置。

请参阅 AGSMapView 的文档

关于ios - map 无法显示我的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16771526/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com