开发者

String as a GeoCoordinate

I am developing an application which is storing places in database开发者_如何学运维.

The problem is:

I have a main view of map, then I click add place button. The button goes to standard form which contains name, description and coordinates. Coordinates I am taking from map and changing to string. Then it saves in SQLite database.

But I have no idea how use geocoordinates from DB, because they are strings, not a GeoCoordinates, and simply I can't use them to for example pushpin with binding data


Considering there is no GeoCoordinate.Parse() method and assuming your string representation comes from GeoCoordinate.ToString(), you could use something like this:

public GeoCoordinate ParseGeoCoordinate(string input)
{
    if (String.IsNullOrEmpty(input))
    {
        throw new ArgumentException("input");
    }

    if (input == "Unknown")
    {
        return GeoCoordinate.Unknown;
    }

    // GeoCoordinate.ToString() uses InvariantCulture, so the doubles will use '.'
    // for decimal placement, even in european environments
    string[] parts = input.Split(',');

    if (parts.Length != 2)
    {
        throw new ArgumentException("Invalid format");
    }

    double latitude = Double.Parse(parts[0], CultureInfo.InvariantCulture);
    double longitude = Double.Parse(parts[1], CultureInfo.InvariantCulture);

    return new GeoCoordinate(latitude, longitude);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜