-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainActivity.cs
315 lines (275 loc) · 13.6 KB
/
MainActivity.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Com.Huawei.Agconnect.Config;
using Android.Content;
using Android.Util;
using Android;
using Android.Support.V4.App;
using Android.Support.V4.Content;
using Android.Content.PM;
using System;
using Android.Graphics;
using Com.Huawei.Hms.Maps;
using Com.Huawei.Hms.Maps.Model;
using Com.Huawei.Hms.Maps.Util;
namespace HmsMapDemo
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity, IOnMapReadyCallback
{
private HuaweiMap hMap;
Button btnPolylineDemo, btnPolygonDemo, btnCircleDemo, btnMarkersDemo, btnOverlayDemo;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
SetContentView(Resource.Layout.activity_main);
btnMarkersDemo = FindViewById<Button>(Resource.Id.btnMarkersDemo);
btnPolylineDemo = FindViewById<Button>(Resource.Id.btnPolylineDemo);
btnPolygonDemo = FindViewById<Button>(Resource.Id.btnPolygonDemo);
btnCircleDemo = FindViewById<Button>(Resource.Id.btnCircleDemo);
btnOverlayDemo = FindViewById<Button>(Resource.Id.btnOverlayDemo);
btnMarkersDemo.Click += btnMarkersDemo_Click;
btnPolylineDemo.Click += btnPolylineDemo_Click;
btnPolygonDemo.Click += btnPolygonDemo_Click;
btnCircleDemo.Click += btnCircleDemo_Click;
btnOverlayDemo.Click += btnOverlayDemo_Click;
checkPermission(new string[] {
Android.Manifest.Permission.WriteExternalStorage,
Android.Manifest.Permission.ReadExternalStorage,
Android.Manifest.Permission.AccessCoarseLocation,
Android.Manifest.Permission.AccessFineLocation,
Android.Manifest.Permission.Internet }, 100);
AGConnectServicesConfig config = AGConnectServicesConfig.FromContext(ApplicationContext);
}
public void checkPermission(string[] permissions, int requestCode)
{
foreach (string permission in permissions)
{
if (ContextCompat.CheckSelfPermission(this, permission) == Permission.Denied)
{
ActivityCompat.RequestPermissions(this, permissions, requestCode);
}
}
SetUpMap();
}
private void SetUpMap()
{
MapFragment mapFragment = FragmentManager.FindFragmentById<MapFragment>(Resource.Id.mapfragment_mapfragmentdemo);
mapFragment.GetMapAsync(this);
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
public void OnMapReady(HuaweiMap huaweiMap)
{
this.hMap = huaweiMap;
hMap.UiSettings.ZoomControlsEnabled = true;
hMap.UiSettings.CompassEnabled = true;
hMap.UiSettings.MyLocationButtonEnabled = true;
hMap.MyLocationEnabled = true;
// hMap.MapType = HuaweiMap.MapTypeNone;
hMap.MarkerClick += OnMarkerClick;
hMap.MarkerDragStart += OnMarkerDragStart;
hMap.MarkerDrag += OnMarkerDrag;
hMap.MarkerDragEnd += OnMarkerDragEnd;
hMap.PolylineClick += OnPolylineClick;
hMap.PolygonClick += OnPolygonClick;
hMap.CircleClick += OnCircleClick;
hMap.GroundOverlayClick += OnGroundOverlayClick;
hMap.SetInfoWindowAdapter(new CustomMapInfoWindow(this));
Toast.MakeText(this, "OnMapReady done.", ToastLength.Short).Show();
MarkersDemo(hMap);
}
private void btnMarkersDemo_Click(object sender, EventArgs e)
{
if (hMap != null)
MarkersDemo(hMap);
else
Toast.MakeText(this, "map not ready", ToastLength.Short).Show();
}
private void btnPolylineDemo_Click(object sender, EventArgs e)
{
if (hMap != null)
PolylineDemo(hMap);
else
Toast.MakeText(this, "map not ready", ToastLength.Short).Show();
}
private void btnPolygonDemo_Click(object sender, EventArgs e)
{
if (hMap != null)
PolygonDemo(hMap);
else
Toast.MakeText(this, "map not ready", ToastLength.Short).Show();
}
private void btnCircleDemo_Click(object sender, EventArgs e)
{
if (hMap != null)
CircleDemo(hMap);
else
Toast.MakeText(this, "map not ready", ToastLength.Short).Show();
}
private void btnOverlayDemo_Click(object sender, EventArgs e)
{
if (hMap != null)
OverlayDemo(hMap);
else
Toast.MakeText(this, "map not ready", ToastLength.Short).Show();
}
private void MarkersDemo(HuaweiMap hMap)
{
hMap.Clear();
Marker marker1;
MarkerOptions marker1Options = new MarkerOptions()
.InvokePosition(new LatLng(41.0083, 28.9784))
.InvokeTitle("Marker Title #1")
.InvokeSnippet("Marker Desc #1");
marker1 = hMap.AddMarker(marker1Options);
Marker marker2;
MarkerOptions marker2Options = new MarkerOptions()
.InvokePosition(new LatLng(41.022231, 29.008118))
.InvokeTitle("Marker Title #2")
.InvokeSnippet("Marker Desc #2");
marker2 = hMap.AddMarker(marker2Options);
Marker marker3;
MarkerOptions marker3Options = new MarkerOptions()
.InvokePosition(new LatLng(41.005784, 28.997364))
.InvokeTitle("Marker Title #3")
.InvokeSnippet("Marker Desc #3");
Bitmap bitmap1 = ResourceBitmapDescriptor.DrawableToBitmap(this, ContextCompat.GetDrawable(this, Resource.Drawable.markerblue));
marker3Options.InvokeIcon(BitmapDescriptorFactory.FromBitmap(bitmap1));
marker3 = hMap.AddMarker(marker3Options);
Marker marker4;
MarkerOptions marker4Options = new MarkerOptions()
.InvokePosition(new LatLng(41.028435, 28.988186))
.InvokeTitle("Marker Title #4")
.InvokeSnippet("Marker Desc #4")
.Anchor(0.9F, 0.9F)
.Draggable(true);
Bitmap bitmap2 = ResourceBitmapDescriptor.DrawableToBitmap(this, ContextCompat.GetDrawable(this, Resource.Drawable.marker));
marker4Options.InvokeIcon(BitmapDescriptorFactory.FromBitmap(bitmap2));
marker4 = hMap.AddMarker(marker4Options);
}
private void PolylineDemo(HuaweiMap hMap)
{
hMap.Clear();
Polyline polyline1;
PolylineOptions polyline1Options = new PolylineOptions()
.Add(new LatLng(41.01929, 28.967267), new LatLng(41.016785, 28.986971), new LatLng(41.001917, 28.978743), new LatLng(41.002298, 28.954132));
polyline1Options.InvokeColor(Color.Blue);
polyline1Options.InvokeWidth(20);
// polyline1Options.InvokeZIndex(2);
polyline1Options.Visible(false);
polyline1Options.Clickable(true);
polyline1 = hMap.AddPolyline(polyline1Options);
Polyline polyline2;
PolylineOptions polyline2Options = new PolylineOptions()
.Add(new LatLng(41.010410, 28.978511), new LatLng(41.035243, 29.026812), new LatLng(41.022122, 29.00653), new LatLng(41.00415, 29.012449), new LatLng(41.001699, 28.978743));
polyline2Options.InvokeColor(Color.Red);
polyline1Options.InvokeZIndex(1);
polyline2Options.Clickable(true);
polyline2 = hMap.AddPolyline(polyline2Options);
}
private void PolygonDemo(HuaweiMap hMap)
{
hMap.Clear();
Polygon polygon1;
PolygonOptions polygon1Options = new PolygonOptions()
.Add(new LatLng(41.01929, 28.967267), new LatLng(41.016785, 28.986971), new LatLng(41.014623, 28.999753), new LatLng(41.001917, 28.978743), new LatLng(41.002298, 28.954132));
polygon1Options.InvokeFillColor(Color.Argb(60, 255, 200, 0));
polygon1Options.InvokeStrokeColor(Color.Yellow);
polygon1Options.InvokeStrokeWidth(30);
polygon1Options.Clickable(true);
polygon1Options.InvokeZIndex(2);
polygon1 = hMap.AddPolygon(polygon1Options);
Polygon polygon2;
PolygonOptions polygon2Options = new PolygonOptions()
.Add(new LatLng(41.035243, 29.026812), new LatLng(41.022122, 29.00653), new LatLng(41.00415, 29.012449), new LatLng(41.001699, 28.978743), new LatLng(41.017384, 28.986827), new LatLng(41.037711, 28.996791));
polygon2Options.InvokeFillColor(Color.Argb(60, 0, 0, 255));
polygon2Options.InvokeStrokeColor(Color.Blue);
polygon2Options.Clickable(true);
polygon2 = hMap.AddPolygon(polygon2Options);
}
private void CircleDemo(HuaweiMap hMap)
{
hMap.Clear();
Circle circle1;
LatLng circle1LatLng = new LatLng(41.01019, 28.974475);
CircleOptions circle1Options = new CircleOptions();
circle1Options.InvokeCenter(circle1LatLng);
circle1Options.InvokeRadius(1600);
circle1Options.InvokeStrokeWidth(5);
circle1Options.InvokeStrokeColor(Color.Blue);
circle1Options.InvokeFillColor(Color.Argb(60, 0, 0, 255));
circle1Options.Clickable(true);
circle1Options.InvokeZIndex(2);
circle1 = hMap.AddCircle(circle1Options);
Circle circle2;
LatLng circle2LatLng = new LatLng(41.01563, 29.052667);
CircleOptions circle2Options = new CircleOptions();
circle2Options.InvokeCenter(circle2LatLng);
circle2Options.InvokeRadius(5000);
circle2Options.InvokeStrokeWidth(10);
circle2Options.InvokeStrokeColor(Color.OrangeRed);
circle2Options.InvokeFillColor(Color.Argb(60, 255, 200, 0));
circle2Options.Clickable(true);
circle2Options.InvokeZIndex(1);
circle2 = hMap.AddCircle(circle2Options);
}
private void OverlayDemo(HuaweiMap hMap)
{
hMap.Clear();
GroundOverlay groundOverlay1;
BitmapDescriptor descriptor1 = BitmapDescriptorFactory.FromResource(Resource.Drawable.hmslogo);
GroundOverlayOptions groundOverlay1Options = new GroundOverlayOptions()
.Position(new LatLng(41.010037, 28.9819363), 2400, 2400)
.InvokeImage(descriptor1);
groundOverlay1Options.Clickable(true);
groundOverlay1 = hMap.AddGroundOverlay(groundOverlay1Options);
GroundOverlay groundOverlay2;
BitmapDescriptor descriptor2 = BitmapDescriptorFactory.FromResource(Resource.Drawable.maplogo);
GroundOverlayOptions groundOverlay2Options = new GroundOverlayOptions()
.Position(new LatLng(41.024204, 29.009406), 2400, 2400)
.InvokeImage(descriptor2);
groundOverlay2Options.Clickable(true);
groundOverlay2 = hMap.AddGroundOverlay(groundOverlay2Options);
}
public void OnMarkerClick(object sender, HuaweiMap.MarkerClickEventArgs e)
{
Toast.MakeText(this, $"Marker Click Marker ID: {e.P0.Id}", ToastLength.Short).Show();
}
public void OnMarkerDragStart(object sender, HuaweiMap.MarkerDragStartEventArgs e)
{
Toast.MakeText(this, $"Drag Start Marker ID: {e.P0.Id}", ToastLength.Short).Show();
}
public void OnMarkerDrag(object sender, HuaweiMap.MarkerDragEventArgs e)
{
Toast.MakeText(this, $"Dragging Marker ID: {e.P0.Id}", ToastLength.Short).Show();
}
public void OnMarkerDragEnd(object sender, HuaweiMap.MarkerDragEndEventArgs e)
{
Toast.MakeText(this, $"Drag End Marker ID: {e.P0.Id}", ToastLength.Short).Show();
}
public void OnPolylineClick(object sender, HuaweiMap.PolylineClickEventArgs e)
{
Toast.MakeText(this, $"Polyline Click Polyline ID: {e.P0.Id}", ToastLength.Short).Show();
}
public void OnPolygonClick(object sender, HuaweiMap.PolygonClickEventArgs e)
{
Toast.MakeText(this, $"Polygon Click Polygon ID: {e.P0.Id}", ToastLength.Short).Show();
}
public void OnCircleClick(object sender, HuaweiMap.CircleClickEventArgs e)
{
Toast.MakeText(this, $"Circle Click Circle ID: {e.P0.Id}", ToastLength.Short).Show();
}
public void OnGroundOverlayClick(object sender, HuaweiMap.GroundOverlayClickEventArgs e)
{
Toast.MakeText(this, $"Overlay Click Overlay ID: {e.P0.Id}", ToastLength.Short).Show();
}
}
}