简体   繁体   English

如何在iOS中存储GPS坐标?

[英]How to Store a GPS Coordinate in iOS?

I have a handful of points-of-interest and want to hard-code them but there doesn't seem to be a clean facility in objective-c to do so. 我有几个兴趣点,想对它们进行硬编码,但在Objective-C中似乎没有一个干净的工具可以做到这一点。

I want to associate two numbers to a string: @"cool spot", 37.77794,-122.41933 我想将两个数字关联到一个字符串:@“ cool spot”,37.77794,-122.41933

Do I need an NSDictionary of NSStrings and convert each float to a string? 我是否需要NStringictionary的NSStrings并将每个float转换为字符串? It just seems like too much overhead to group all this? 似乎很难将所有这些分组在一起? Should I make a c-struct? 我应该做一个C结构吗? This situation HAS to come up a lot but cannot think of what a clean solution for this might be. 这种情况已经出现了很多,但是却没有想到可能有一个干净的解决方案。

Thanks 谢谢

How are you intending on storing it? 您打算如何存储它? If you're using something like core-data then your easiest option is to use 2 separate numbers for longitude and latitude, and then simply create a coordinate when you access the data (CLLocationCoordinate2D as tom mentioned) 如果您使用的是核心数据之类的东西,那么最简单的选择是对经度和纬度使用两个单独的数字,然后在访问数据时简单地创建一个坐标(如tom所述,为CLLocationCoordinate2D)

It is very dependent on your storage method as iOS only allows saving of certain types of objects for different methods. 它非常取决于您的存储方法,因为iOS仅允许为不同的方法保存某些类型的对象。

If you're not actually storing it to file and just using it in memory, then as tom said, something like CLLocationCoordinate2D would work, which is a struct rather than an object. 如果您实际上不是将其存储到文件中而是仅在内存中使用它,那么正如汤姆所说,像CLLocationCoordinate2D这样的东西将起作用,它是一个结构而不是一个对象。 If you want an actual object, then use CLLocation which contains a CLLocationCoordinate2D object internally. 如果要使用实际对象,请使用内部包含CLLocationCoordinate2D对象的CLLocation。

You can create a CLLocationCoordinate2D object with : 您可以使用以下命令创建CLLocationCoordinate2D对象:

CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(latitude, longitude);

Or if you need the object wrapper, then create a CLLocation object with : 或者,如果您需要对象包装器,请使用以下命令创建一个CLLocation对象:

CLLocation *loc = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];

看一下CLLocationCoordinate2DCLLocationCoordinate2D-Apple Developer Docs ),它具有一个latitude字段和一个longitude字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM