本文介绍了如何在Xcode中将CLLocation转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SwiftLocation获取坐标:

I use SwiftLocation for get coordinates:

try! SwiftLocation.shared.currentLocation(Accuracy.House, timeout: 20, onSuccess: { (location) -> Void in
    print(location?.description)            
         }) { (error) -> Void in
    print("something went wrong")
         }

在位置吗?说明我明白了:

In location?.description I get it:

我只需要坐标.所以我为此做循环:

And I need to take just coordinates. So I make loop for it:

    while name.characters.last != ">" { // delete all after ">"
        name = String(name.characters.dropLast())
    }
    name = String(name.characters.dropLast()) // delete ">"
    name = String(name.characters.dropFirst()) // delete "<"
    name = String(name.characters.dropFirst()) // delete "+"
    print(name) // get 37.78583400,-122.40641700
    tempMyLat = name
    while tempMyLat.characters.last != "," { // delete all after ","
        tempMyLat = String(tempMyLat.characters.dropLast())
    }
    tempMyLon = name
    while tempMyLon.characters.first != "," { // delete all before ","
        tempMyLon = String(tempMyLon.characters.dropFirst())
    }
    tempMyLon = String(tempMyLon.characters.dropFirst()) // delete ","

但是此代码仅适用于字符串.当我获得位置信息时.说明-无法将其转换为类型-字符串,因为位置"是CLLocation类型.

But this code work ONLY for string. When I get location?.description - I can't convert it for type - string, because "location" is a CLLocation type.

所以,我的问题是:如何将location..description转换为String? 或者 如何仅从 SwiftLocation

So, my question: How convert location?.description to String ? or how get only coordinates from the SwiftLocation

谢谢.

推荐答案

如果要从CLLocation获取位置,则不需要从CLLocation对象转换字符串.您可以直接从CLLocation对象获取位置.这是一个示例:

If you want to get locations from CLLocation you dont need to convert string from CLLocation object. you can get locations directly from CLLocation object.Here is an example :

var locationObj = locationArray.lastObject as CLLocation
var coord = locationObj.coordinate
var longitude = coord.longitude //Latitude & Longitude as String
var latitude = coord.latitude

这篇关于如何在Xcode中将CLLocation转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-20 22:50