将CLLocationCoordinate2D类型转换为数字或字符串 [英] converting a CLLocationCoordinate2D type to number or string
问题描述
我想知道如何将CLLocationCoordinate2D的纬度和经度值转换为数字或字符串值。
Iver尝试了几种不同的方式,但是他们不工作:
CLLocationCoordinate2D centerCoord;
centerCoord.latitude = self.locModel.userLocation.coordinate.latitude;
centerCoord.longitude = self.locModel.userLocation.coordinate.longitude;
NSString * tmpLat = [[NSString alloc] initWithFormat:@%g,centerCoord.latitude];
NSString * tmpLong = [[NSString alloc] initWithFormat:@%g,centerCoord.longitude];
NSLog(用户的纬度为:%@,tmpLat);
NSLog(用户的经度是:%@,tmpLong);
这会返回编译器发出的警告。
警告是
警告:从不兼容的指针类型传递NSLog的参数1
如何做到这一点?
任何帮助将不胜感激。 p>
感谢
解决方案你没有提到警告,很可能是因为您忘记了NSLog字符串前面的
@
:NSLog(@用户的纬度是:%f,self.locModel.userLocation.coordinate.latitude);
NSLog(@用户的经度是:%f,self.locModel.userLocation.coordinate.longitude);
您更新的代码应为:
NSLog(@用户的纬度是:%@,tmpLat);
NSLog(@用户的经度是:%@,tmpLong);
NSLog需要一个NSString参数,需要@符号。没有@符号,字符串是一个纯C字符串而不是NSString对象。
I was wondering how to convert latitude and longitude values of CLLocationCoordinate2D to numbers or string values. Iver tried a few different ways but they arene't working:
CLLocationCoordinate2D centerCoord; centerCoord.latitude = self.locModel.userLocation.coordinate.latitude ; centerCoord.longitude = self.locModel.userLocation.coordinate.longitude; NSString *tmpLat = [[NSString alloc] initWithFormat:@"%g", centerCoord.latitude]; NSString *tmpLong = [[NSString alloc] initWithFormat:@"%g", centerCoord.longitude]; NSLog("User's latitude is: %@", tmpLat); NSLog("User's longitude is: %@", tmpLong);
This returns a warning by the compiler.
The warning is
warning: passing argument 1 of 'NSLog' from incompatible pointer type
How do I do this?
Any help would be appreciated.
thanks
解决方案You haven't mentioned what the warning is but it's most likely because you forgot the
@
in front of the NSLog strings:NSLog(@"User's latitude is: %f", self.locModel.userLocation.coordinate.latitude ); NSLog(@"User's longitude is: %f", self.locModel.userLocation.coordinate.longitude );
Your updated code should be:
NSLog(@"User's latitude is: %@", tmpLat); NSLog(@"User's longitude is: %@", tmpLong);
NSLog expects an NSString parameter which needs an @ sign in front. Without the @ sign, the string is a plain C string not an NSString object.
这篇关于将CLLocationCoordinate2D类型转换为数字或字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!