Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Latitude and Longitude to a returning Tweet if available. #735

Merged
merged 4 commits into from
Jan 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,16 @@
Text="{x:Bind Text}"
TextTrimming="CharacterEllipsis"
TextWrapping="WrapWholeWords" />
<TextBlock Grid.Row="2"
HorizontalAlignment="Right"
FontWeight="ExtraLight"
Text="{x:Bind CreationDate}" />
<StackPanel Grid.Row="2"
HorizontalAlignment="Right"
Orientation="Horizontal">
<TextBlock FontWeight="ExtraLight"
Text="{x:Bind CreationDate}" />
<TextBlock FontWeight="ExtraLight"
Margin="4,0,0,0"
Text="{x:Bind GeoData.DisplayCoordinates}">
</TextBlock>
</StackPanel>
</Grid>
</Border>
</DataTemplate>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<Compile Include="Services\Facebook\FacebookPlatformImageSource.cs" />
<Compile Include="Services\Twitter\TwitterErrors.cs" />
<Compile Include="Services\Twitter\TwitterException.cs" />
<Compile Include="Services\Twitter\TwitterGeoData.cs" />
<Compile Include="Services\Twitter\TwitterOAuthTokenType.cs" />
<Compile Include="Services\Facebook\FacebookPermissions.cs" />
<Compile Include="Services\Facebook\FacebookDataHost.cs" />
Expand Down
6 changes: 6 additions & 0 deletions Microsoft.Toolkit.Uwp.Services/Services/Twitter/Tweet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public class Tweet : SchemaBase
[JsonProperty("created_at")]
public string CreatedAt { get; set; }

/// <summary>
/// Gets or sets the geographic data (latitude and longitude)
/// </summary>
[JsonProperty("geo")]
public TwitterGeoData GeoData { get; set; }

/// <summary>
/// Gets or sets item Id.
/// </summary>
Expand Down
95 changes: 95 additions & 0 deletions Microsoft.Toolkit.Uwp.Services/Services/Twitter/TwitterGeoData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// ******************************************************************
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.
// ******************************************************************

using System.Globalization;
using Newtonsoft.Json;

namespace Microsoft.Toolkit.Uwp.Services.Twitter
{
/// <summary>
/// A class to contain the latitude and longitude of a tweet.
/// </summary>
public class TwitterGeoData
{
private const int LatitudeIndex = 0;
private const int LongitudeIndex = 1;
private const string PointType = "Point";

/// <summary>
/// Gets or sets the type of data
/// </summary>
[JsonProperty("type")]
public string DataType { get; set; }

/// <summary>
/// Gets the latitude and longitude in a coordinate format.
/// </summary>
public string DisplayCoordinates
{
get
{
string result = null;

if (Coordinates != null)
{
result = $"({Coordinates[LatitudeIndex]}, {Coordinates[LongitudeIndex]})";
}

return result;
}
}

/// <summary>
/// Gets or sets the coordinates of the geographic data
/// </summary>
[JsonProperty("coordinates")]
public string[] Coordinates { get; set; }

/// <summary>
/// Gets the numeric latitude (null if the value could not be converted)
/// </summary>
public double? Latitude
{
get
{
return ParseCoordinate(LatitudeIndex);
}
}

/// <summary>
/// Gets the numeric longitude (null if the value could not be converted)
/// </summary>
public double? Longitude
{
get
{
return ParseCoordinate(LongitudeIndex);
}
}

private double? ParseCoordinate(int index)
{
double? result = null;
double parsed;

if (DataType == PointType
&& Coordinates != null
&& !string.IsNullOrEmpty(Coordinates[index])
&& double.TryParse(Coordinates[index], NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out parsed))
{
result = parsed;
}

return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class TwitterStatus
public bool PossiblySensitive { get; set; }

/// <summary>
/// Gets a the Request parameters
/// Gets the request parameters
/// </summary>
public string RequestParameters
{
Expand Down
12 changes: 11 additions & 1 deletion docs/services/Twitter.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ Copy this from the *Keys and Access Tokens* tab on your application page.
**Callback URI** Enter a unique URI for your application. This must match the *Callback URL* field on the *Application Details* tab in Twitter.
*Example*: http://myapp.company.com - (this does not have to be a working URL)

## Overview

In the code section below the GetUserTimeLineAsync method returns some Tweet objects. The Tweet class returns some basic information along with the tweet text itself.

- **CreatedAt** (string) – The date and time of the Tweet formatted by Twitter
- **Text** (string) – The text of the Tweet
- **Id** (string) – The Twitter status identifier
- **GeoData** (TwitterGeoData) - A class containing the latitude and longitude of the Tweet
- **User** (TwitterUser) - A class containing the user ID, Name, ScreenName, and ProfileImageUrl

## Syntax

```csharp
Expand All @@ -34,7 +44,7 @@ if (!await TwitterService.Instance.LoginAsync())
var user = await TwitterService.Instance.GetUserAsync();
ProfileImage.DataContext = user;

// Get user timeline
// Get user time line
ListView.ItemsSource = await TwitterService.Instance.GetUserTimeLineAsync(user.ScreenName, 50);

// Post a tweet
Expand Down