如何通过短信文本发送用户的经度和纬度坐标
问题描述:
在我的“共享位置”功能中,我希望用户能够发送以长/纬度坐标形式共享其当前位置的文本。我用我实现的错误是“上下文类型”字符串“不能用于数组文字”。如何或者什么是正确的代码来实现?如何通过短信文本发送用户的经度和纬度坐标
这里是我的代码
@IBAction func shareLocation(sender: AnyObject) {
// Send user coordinates through text
if !MFMessageComposeViewController.canSendText() {
print("SMS services are not available")
var coordinate: CLLocationCoordinate2D
messageVC!.body = [coordinate.latitude, coordinate.longitude];
messageVC!.recipients = [LookoutCell.description()];
messageVC!.messageComposeDelegate = self;
self.presentViewController(messageVC!, animated: false, completion: nil)
}
}
答
有很多的错误与此代码:
- 你检查,如果你不能通过
!MFMessageComposeViewController.canSendText()
发送文本,如果你可以”然后发送t(括号内需要提前结束) - 你声明
var coordinate: CLLocationCoordinate2D
没有值,不会编译 - 经纬度是双倍的,所以你需要字符串格式化来输出这些值
-
body
是一个字符串,您正在尝试向它发送一个数组。 - 试试这个:
messageVC!.body = String(format: "Lat: %.4f, Long: %.4f", coordinate.latitude, coordinate.longitude)
- 你需要考虑格式化引导更多的细节(你可以开始here)
哇,我不知道这么多是不正确的。我还会检查格式指南以获取更多帮助。 – jonB22