Skip to content
This repository was archived by the owner on Mar 24, 2023. It is now read-only.

Commit 9ac3db1

Browse files
committed
[MapBoxQs] Add Mapbox API service + Update demo page
1 parent b0574f5 commit 9ac3db1

File tree

8 files changed

+216
-26
lines changed

8 files changed

+216
-26
lines changed

Droid/MainActivity.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ protected override void OnCreate(Bundle savedInstanceState)
1515

1616
base.OnCreate(savedInstanceState);
1717

18-
Com.Mapbox.Mapboxsdk.Mapbox.GetInstance(this, "sk.eyJ1IjoiamVzcGVyZGF4IiwiYSI6ImNpemo2ajloNTAwMmwyd3I0NWoxNHZoNTYifQ.TnTUuIPwpZzGfS47cr0YMw");
18+
Com.Mapbox.Mapboxsdk.Mapbox.GetInstance(this, MapBoxQs.Services.MapBoxService.AccessToken);
1919

2020
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
2121

MapBoxQs/MainPageViewModel.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using Naxam.Controls.Forms;
3+
using System.Runtime.CompilerServices;
4+
using System.ComponentModel;
5+
using System.Windows.Input;
6+
using Xamarin.Forms;
7+
8+
namespace MapBoxQs
9+
{
10+
public class MainPageViewModel : INotifyPropertyChanged
11+
{
12+
public event PropertyChangedEventHandler PropertyChanged;
13+
14+
public MainPageViewModel()
15+
{
16+
}
17+
18+
private MapStyle _CurrentMapStyle;
19+
20+
public MapStyle CurrentMapStyle
21+
{
22+
get { return _CurrentMapStyle; }
23+
set
24+
{
25+
_CurrentMapStyle = value;
26+
OnPropertyChanged("CurrentMapStyle");
27+
}
28+
}
29+
30+
private double _ZoomLevel;
31+
32+
public double ZoomLevel
33+
{
34+
get { return _ZoomLevel; }
35+
set {
36+
_ZoomLevel = value;
37+
OnPropertyChanged("ZoomLevel");
38+
}
39+
}
40+
41+
private ICommand _ZoomCommand;
42+
43+
public ICommand ZoomCommand
44+
{
45+
get {
46+
return _ZoomCommand = _ZoomCommand
47+
?? new Command<int>( (int step) => ZoomLevel += step);
48+
}
49+
set {
50+
_ZoomCommand = value;
51+
OnPropertyChanged("ZoomLevel");
52+
}
53+
}
54+
55+
private void OnPropertyChanged(string propertyName = null)
56+
{
57+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
58+
59+
}
60+
}
61+
}

MapBoxQs/MapBoxQs.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
<DependentUpon>MapBoxQsPage.xaml</DependentUpon>
4040
</Compile>
4141
<Compile Include="Properties\AssemblyInfo.cs" />
42+
<Compile Include="Services\MapboxService.cs" />
43+
<Compile Include="MainPageViewModel.cs" />
4244
</ItemGroup>
4345
<ItemGroup>
4446
<Reference Include="Newtonsoft.Json">
@@ -53,6 +55,9 @@
5355
<Reference Include="Xamarin.Forms.Xaml">
5456
<HintPath>..\packages\Xamarin.Forms.2.3.4.247\lib\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+Xamarin.iOS10+xamarinmac20\Xamarin.Forms.Xaml.dll</HintPath>
5557
</Reference>
58+
<Reference Include="ModernHttpClient">
59+
<HintPath>..\packages\Ideine.ModernHttpClient.3.0.2\lib\Portable-Net45+WinRT45+WP8+WPA81\ModernHttpClient.dll</HintPath>
60+
</Reference>
5661
</ItemGroup>
5762
<ItemGroup>
5863
<None Include="packages.config" />
@@ -63,6 +68,9 @@
6368
<Name>Naxam.Mapbox.Forms</Name>
6469
</ProjectReference>
6570
</ItemGroup>
71+
<ItemGroup>
72+
<Folder Include="Services\" />
73+
</ItemGroup>
6674
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
6775
<Import Project="..\packages\Xamarin.Forms.2.3.4.247\build\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+Xamarin.iOS10+xamarinmac20\Xamarin.Forms.targets" Condition="Exists('..\packages\Xamarin.Forms.2.3.4.247\build\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+Xamarin.iOS10+xamarinmac20\Xamarin.Forms.targets')" />
6876
</Project>

MapBoxQs/MapBoxQsPage.xaml

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<ContentPage
3-
xmlns="http://xamarin.com/schemas/2014/forms"
4-
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
5-
xmlns:local="clr-namespace:Naxam.Controls.Forms;assembly=Naxam.Mapbox.Forms"
6-
x:Class="MapBoxQs.MapBoxQsPage">
2+
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:Naxam.Controls.Forms;assembly=Naxam.Mapbox.Forms" x:Class="MapBoxQs.MapBoxQsPage">
73
<StackLayout Orientation="Vertical">
8-
<Grid Margin="40" VerticalOptions="Start">
9-
<Button
10-
x:Name="btnChangeLocation"
11-
Text="Change location"
12-
Grid.Column="0"
13-
/>
14-
<Button Clicked="ReloadStyle" Text="Reload Style"
15-
Grid.Column="1"/>
4+
<Grid Margin="20" VerticalOptions="Start">
5+
<Button x:Name="btnChangeLocation" Text="Change location" Grid.Column="0" />
6+
<Button Clicked="ReloadStyle" Text="Reload Style" Grid.Column="1" />
7+
<Button Clicked="ShowStylesPicker" Text="Change style" Grid.Column="2" />
8+
<Button Command="{Binding ZoomCommand}" CommandParameter="{x:Int32 1}" Text="Zoom in" Grid.Row="1" Grid.Column="0" />
9+
<Button Command="{Binding ZoomCommand}" CommandParameter="{x:Int32 -1}" Text="Zoom out" Grid.Row="1" Grid.Column="1" />
1610
</Grid>
17-
18-
<local:MapView ZoomLevel="16" x:Name="map" VerticalOptions="FillAndExpand">
19-
<local:MapView.Center>
11+
<local:MapView x:Name="map" VerticalOptions="FillAndExpand" MapStyle="{Binding CurrentMapStyle}" ZoomLevel="{Binding ZoomLevel}">
12+
<!-- <local:MapView.Center>
2013
<local:Position Lat="21.0333" Long="105.8500"/>
21-
</local:MapView.Center>
14+
</local:MapView.Center>-->
2215
</local:MapView>
23-
</StackLayout>
24-
</ContentPage>
16+
</StackLayout>
17+
</ContentPage>

MapBoxQs/MapBoxQsPage.xaml.cs

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
using Newtonsoft.Json;
44
using Xamarin.Forms;
55
using System.Linq;
6+
using System.Collections.Generic;
67

78
namespace MapBoxQs
89
{
910
public partial class MapBoxQsPage : ContentPage
1011
{
12+
13+
public MapBoxQs.Services.IMapBoxService _MBService = new MapBoxQs.Services.MapBoxService();
14+
MainPageViewModel _ViewModel = new MainPageViewModel();
1115
public MapBoxQsPage()
1216
{
1317
InitializeComponent();
@@ -29,10 +33,6 @@ public MapBoxQsPage()
2933
map.Center = positions[random.Next(2)%2];
3034
};
3135

32-
map.DidFinishLoadingStyleCommand = new Command<MapStyle>((MapStyle obj) =>
33-
{
34-
map.ResetPositionFunc.Execute(null);
35-
});
3636

3737
map.DidTapOnMapCommand = new Command<Tuple<Position, Point>>((Tuple<Position, Point> obj) =>
3838
{
@@ -46,16 +46,67 @@ public MapBoxQsPage()
4646
});
4747
map.DidFinishLoadingStyleCommand = new Command<MapStyle>((obj) =>
4848
{
49+
map.ResetPositionFunc.Execute(null);
4950
foreach (Layer layer in obj.OriginalLayers)
5051
{
5152
System.Diagnostics.Debug.WriteLine(layer.Id);
5253
}
53-
});
54+
55+
});
56+
map.ZoomLevel = Device.RuntimePlatform == Device.Android ? 4 : 10;
57+
58+
59+
BindingContext = _ViewModel;
60+
}
61+
62+
protected override void OnAppearing()
63+
{
64+
base.OnAppearing();
65+
GetAllStyles();
66+
}
67+
68+
async void ShowStylesPicker(object sender, EventArgs args)
69+
{
70+
if (_Styles == null || _Styles.Length == 0) {
71+
await DisplayAlert("Error", "No style available", "Dismiss");
72+
return;
73+
}
74+
var buttons = _Styles.Select((arg) => arg.Name).ToArray();
75+
76+
var result = await DisplayActionSheet("Change style", "Cancel", null, buttons);
77+
if (!string.IsNullOrEmpty(result) && buttons.Contains(result)) {
78+
_ViewModel.CurrentMapStyle = _Styles.FirstOrDefault((arg) => arg.Name == result);
79+
}
80+
}
81+
82+
private async void GetAllStyles()
83+
{
84+
IsBusy = true;
85+
_Styles = await _MBService.GetAllStyles();
86+
IsBusy = false;
87+
88+
}
89+
90+
public MapStyle[] _Styles
91+
{
92+
get;
93+
set;
5494
}
5595

5696
void ReloadStyle(object sender, EventArgs args)
5797
{
5898
map.ReloadStyleFunc?.Execute(sender);
5999
}
100+
101+
void ZoomIn(object sender, EventArgs args)
102+
{
103+
map.ZoomLevel += 1.0f;
104+
}
105+
106+
void ZoomOut(object sender, EventArgs args)
107+
{
108+
map.ZoomLevel -= 1.0f;
109+
}
110+
60111
}
61112
}

MapBoxQs/Services/MapboxService.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System;
2+
using System.Net.Http;
3+
using System.Threading.Tasks;
4+
using ModernHttpClient;
5+
using Naxam.Controls.Forms;
6+
using Newtonsoft.Json;
7+
8+
namespace MapBoxQs.Services
9+
{
10+
public interface IMapBoxService
11+
{
12+
Task<MapStyle[]> GetAllStyles();
13+
Task<MapStyle> GetStyleDetails(string id, string owner = null);
14+
}
15+
16+
public class MapBoxService : IMapBoxService
17+
{
18+
HttpClient client;
19+
20+
private static string BaseURL = "https://api.mapbox.com/";
21+
public static string AccessToken = "sk.eyJ1IjoibmF4YW10ZXN0IiwiYSI6ImNqNWtpb2d1ZzJpMngyd3J5ZnB2Y2JhYmQifQ.LEvGqQkAqM4MO3ZtGbQrdw";
22+
public static string Username = "naxamtest";
23+
24+
25+
public MapBoxService()
26+
{
27+
client = new HttpClient(new NativeMessageHandler())
28+
{
29+
MaxResponseContentBufferSize = 256000
30+
};
31+
}
32+
33+
public async Task<MapStyle[]> GetAllStyles()
34+
{
35+
var urlFormat = BaseURL + "styles/v1/{0}?access_token={1}";
36+
var uri = new Uri(string.Format(urlFormat, Username, AccessToken));
37+
var response = await client.GetAsync(uri);
38+
if (response.IsSuccessStatusCode)
39+
{
40+
var content = await response.Content.ReadAsStringAsync();
41+
System.Diagnostics.Debug.WriteLine(content);
42+
try
43+
{
44+
return JsonConvert.DeserializeObject<MapStyle[]>(content);
45+
}
46+
catch (Exception ex)
47+
{
48+
System.Diagnostics.Debug.WriteLine("[EXCEPTION] " + ex.Message);
49+
}
50+
}
51+
return null;
52+
}
53+
54+
public async Task<MapStyle> GetStyleDetails(string id, string owner = null)
55+
{
56+
var urlFormat = BaseURL + "styles/v1/{0}/{1}?access_token={2}";
57+
var uri = new Uri(string.Format(urlFormat, owner ?? Username, id, AccessToken));
58+
var response = await client.GetAsync(uri);
59+
if (response.IsSuccessStatusCode)
60+
{
61+
var content = await response.Content.ReadAsStringAsync();
62+
System.Diagnostics.Debug.WriteLine(content);
63+
try
64+
{
65+
var output = JsonConvert.DeserializeObject<MapStyle>(content);
66+
return output;
67+
}
68+
catch (Exception ex)
69+
{
70+
System.Diagnostics.Debug.WriteLine("[EXCEPTION] " + ex.Message);
71+
}
72+
}
73+
return null;
74+
}
75+
}
76+
}

MapBoxQs/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3+
<package id="Ideine.ModernHttpClient" version="3.0.2" targetFramework="portable45-net45+win8+wpa81" />
34
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="portable45-net45+win8+wpa81" />
45
<package id="Xamarin.Forms" version="2.3.4.247" targetFramework="portable45-net45+win8+wpa81" />
56
</packages>

iOS/AppDelegate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsAppli
88
{
99
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
1010
{
11-
Mapbox.MGLAccountManager.AccessToken = "<MAPBOX ACCESS TOKEN>";
11+
Mapbox.MGLAccountManager.AccessToken = MapBoxQs.Services.MapBoxService.AccessToken;
1212
new Naxam.Controls.Platform.iOS.MapViewRenderer();
1313

1414
global::Xamarin.Forms.Forms.Init();

0 commit comments

Comments
 (0)