Skip to content
This repository has been archived by the owner on Dec 28, 2023. It is now read-only.

[Map]Adds GoogleMapView #183

Merged
merged 6 commits into from
Apr 17, 2019
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
15 changes: 15 additions & 0 deletions src/Tizen.Wearable.CircularUI.Forms.Renderer/CircularUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/
using System;
using System.Diagnostics;
using ElmSharp;

namespace Tizen.Wearable.CircularUI.Forms.Renderer
Expand All @@ -29,5 +30,19 @@ public static void Init()
if (IsInitialized) return;
IsInitialized = true;
}

public static void Init(string apiKey)
{
if (!string.IsNullOrEmpty(apiKey))
{
GoogleMaps.Init(apiKey);
}
else
{
Debug.Assert(!string.IsNullOrEmpty(apiKey), "apiKey is null or empty!");
}

Init();
}
}
}
247 changes: 247 additions & 0 deletions src/Tizen.Wearable.CircularUI.Forms.Renderer/GoogleMapViewRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
/*
* Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Flora License, Version 1.1 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://floralicense.org/license/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using System.Text;
using Tizen.Wearable.CircularUI.Forms;
using Tizen.Wearable.CircularUI.Forms.Renderer;
using Xamarin.Forms.Platform.Tizen.Native;
using Xamarin.Forms.Platform.Tizen;
using TChromium = Tizen.WebView.Chromium;
using TWebView = Tizen.WebView.WebView;
using XForms = Xamarin.Forms.Platform.Tizen.Forms;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

[assembly: ExportRenderer(typeof(GoogleMapView), typeof(GoogleMapViewRenderer))]

namespace Tizen.Wearable.CircularUI.Forms.Renderer
{
public class GoogleMapViewRenderer : ViewRenderer<GoogleMapView, WebViewContainer>
{
private const string HtmlStyle = "<html><head><style>html, body {height: 100%; margin: 0; padding: 0;} #map {height: 100%;}</style>";
private const string GoogleMapURL = "http://maps.googleapis.com/maps/api/";
private const double DefaultLatitude = 41.890202;
private const double DefaultLongitude = 12.492049;

private string[] _positions = { "LEFT_TOP", "RIGHT_TOP", "LEFT_CENTER", "RIGHT_CENTER", "LEFT_BOTTOM", "RIGHT_BOTTOM", "TOP_CENTER", "BOTTOM_CENTER" };

string _maphtml;

TWebView NativeWebView => Control.WebView;

protected override void OnElementChanged(ElementChangedEventArgs<GoogleMapView> e)
{
if (Control == null)
{
TChromium.Initialize();
XForms.Context.Terminated += (sender, arg) => TChromium.Shutdown();
SetNativeControl(new WebViewContainer(XForms.NativeParent));
NativeWebView.LoadStarted += OnLoadStarted;
NativeWebView.LoadFinished += OnLoadFinished;
NativeWebView.LoadError += OnLoadError;
}

if (e.OldElement != null)
{
((ObservableCollection<Marker>)e.OldElement.Markers).CollectionChanged -= OnCollectionChanged;
e.OldElement.LoadMapRequested -= OnLoadMapRequested;
}

if (e.NewElement != null)
{
((ObservableCollection<Marker>)e.NewElement.Markers).CollectionChanged += OnCollectionChanged;
e.NewElement.LoadMapRequested += OnLoadMapRequested;
LoadMap();
}
base.OnElementChanged(e);
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
if (Control != null)
{
NativeWebView.StopLoading();
NativeWebView.LoadStarted -= OnLoadStarted;
NativeWebView.LoadFinished -= OnLoadFinished;
NativeWebView.LoadError -= OnLoadError;
}
if (Element != null)
{
((ObservableCollection<Marker>)Element.Markers).CollectionChanged -= OnCollectionChanged;
Element.LoadMapRequested -= OnLoadMapRequested;
}
}
base.Dispose(disposing);
}

private void OnLoadError(object sender, global::Tizen.WebView.SmartCallbackLoadErrorArgs e)
{
string url = e.Url;
Log.Error(FormsCircularUI.Tag, $"OnLoadError() url:{url}");
}

private void OnLoadStarted(object sender, EventArgs e)
{
string url = NativeWebView.Url;
Log.Debug(FormsCircularUI.Tag, "OnLoadStarted()");
}

private void OnLoadFinished(object sender, EventArgs e)
{
string url = NativeWebView.Url;
Log.Debug(FormsCircularUI.Tag, "OnLoadFinished()");
NativeWebView.SetFocus(true);
}

private void LoadMap()
{
_maphtml = CreateMapViewScript();
var baseUrl = default(string);
NativeWebView.LoadHtml(_maphtml, baseUrl);
}

private string CreateMapViewScript()
{
string source = null;
StringBuilder sb = new StringBuilder();
var apiKey = GoogleMaps.GetKey();

sb.Append(HtmlStyle);
sb.AppendLine();
sb.Append("<script src=\"");
sb.Append(GoogleMapURL);
sb.Append("js?key=");
sb.Append(apiKey);
sb.Append("\"></script>");
sb.AppendLine();

sb = CreateMapOptionScript(sb);
sb.AppendLine();
sb.Append("function initialize(){\n var map = new google.maps.Map(document.getElementById(\"map\"), mapProp); ");

if (Element.Markers.Count > 0)
{
sb = CreatePinsScript(sb);
}

sb.Append("\n}\ngoogle.maps.event.addDomListener(window, \'load\', initialize);\n");
sb.Append("</script></head><body><div id =\"map\"></div></body></html>");

source = sb.ToString();
return source;
}

private StringBuilder CreateMapOptionScript(StringBuilder sb)
{
if (Element.MapOption != null)
{
if(Element.MapOption.Center.Latitude == 0 && Element.MapOption.Center.Longitude == 0)
sb.Append($"<script>\nvar myCenter = new google.maps.LatLng({DefaultLatitude}, {DefaultLongitude});");
else
sb.Append($"<script>\nvar myCenter = new google.maps.LatLng({Element.MapOption.Center.Latitude}, {Element.MapOption.Center.Longitude});");

sb.AppendLine();
var type = Element.MapOption.MapType;
var mapTypeId = type.ToString().ToUpper();
sb.Append("var mapProp = {\n center:myCenter,\n");
sb.Append($" zoom: {Element.MapOption.Zoom},\n mapTypeId: google.maps.MapTypeId.{mapTypeId},\n");

if (Element.MapOption.HasGestureEnabled == false)
{
sb.Append(" gestureHandling: 'none',\n");
}

if (Element.MapOption.IsZoomControlVisible == true)
{
sb.Append(" mapTypeControl: false,\n rotateControl: false,\n fullscreenControl: false,\n streetViewControl: false,\n");
sb.Append(" zoomControl: true,\n");
sb.Append(" zoomControlOptions:{\n");
int index = (int)Element.MapOption.ZoomControlPosition;
sb.Append($" position: google.maps.ControlPosition.{_positions[index]},\n");
sb.Append(" }\n");
sb.Append("};");
}
else
{
sb.Append(" disableDefaultUI: true\n};");
}
}
else
{
sb.Append($"<script>\nvar myCenter = new google.maps.LatLng({DefaultLatitude}, {DefaultLongitude});");
sb.AppendLine();
sb.Append("var mapProp = {\n center:myCenter,\n zoom: 10,\n mapTypeId: google.maps.MapTypeId.ROADMAP,\n disableDefaultUI: true\n};");
}

return sb;
}

private StringBuilder CreatePinsScript(StringBuilder _sb)
{
int index = 1;
if (_sb == null || _sb.Length == 0) return _sb;

Log.Debug(FormsCircularUI.Tag, $"Markers count:{Element.Markers.Count}");
foreach (var marker in Element.Markers)
{
_sb.AppendLine();
_sb.Append($" var marker{index} = new google.maps.Marker({{position: new google.maps.LatLng({marker.Position.Latitude}, {marker.Position.Longitude}), title:\"{marker.Description}\" }}); ");
_sb.AppendLine();
_sb.Append($" marker{index}.setMap(map);");
_sb.AppendLine();
_sb.Append($" var infowindow{index} = new google.maps.InfoWindow({{ content: \"{marker.Description}\" }});");
_sb.AppendLine();
if (marker.IsPopupOpened == true)
{
_sb.Append($" infowindow{index}.open(map, marker{index});");
}
else
{
_sb.Append($" marker{index}.addListener('click', function() {{ infowindow{index}.open(map, marker{index});}});");
}
index++;
}

return _sb;
}

private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
Log.Debug(FormsCircularUI.Tag, $"e.Action:{e.Action}");
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
case NotifyCollectionChangedAction.Remove:
case NotifyCollectionChangedAction.Replace:
case NotifyCollectionChangedAction.Reset:
LoadMap();
break;
case NotifyCollectionChangedAction.Move:
//do nothing
break;
}
}

private void OnLoadMapRequested(object sender, EventArgs eventArgs)
{
Log.Debug(FormsCircularUI.Tag, $"Load Requested");
LoadMap();
}
}
}
43 changes: 43 additions & 0 deletions src/Tizen.Wearable.CircularUI.Forms.Renderer/GoogleMaps.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Flora License, Version 1.1 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://floralicense.org/license/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Tizen.Wearable.CircularUI.Forms.Renderer
{
internal static class GoogleMaps
{
private static string _apiKey;

internal static bool IsInitialized { get; private set; }


/// <summary>
/// Initialize Map service with authentication key value.
/// </summary>
public static void Init(string apiKey)
{
if (IsInitialized)
return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there no chance to change apiKey?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. apikey is set first init


_apiKey = apiKey;
jkpu marked this conversation as resolved.
Show resolved Hide resolved
IsInitialized = true;
}

public static string GetKey()
{
return _apiKey;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ void UpdateContent()
{
if (!Xamarin.Forms.Platform.Tizen.Forms.IsInitialized)
{
Console.WriteLine("Tizen Forms is not initialized");
Log.Debug(FormsCircularUI.Tag, "Tizen Forms is not initialized");
return;
}

Expand Down Expand Up @@ -263,7 +263,7 @@ void UpdateFirstButton()

if (_firstButtonBgColor != Color.Default)
{
Console.WriteLine($"TwoButtonPopup set first button background color:{_firstButtonBgColor.ToNative()}");
Log.Debug(FormsCircularUI.Tag, $"TwoButtonPopup set first button background color:{_firstButtonBgColor.ToNative()}");
_firstButton.BackgroundColor = _firstButtonBgColor.ToNative();
}
}
Expand Down Expand Up @@ -309,7 +309,7 @@ void UpdateSecondButton()

if (_secondButtonBgColor != Color.Default)
{
Console.WriteLine($"TwoButtonPopup set second button background color:{_secondButtonBgColor.ToNative()}");
Log.Debug(FormsCircularUI.Tag, $"TwoButtonPopup set second button background color:{_secondButtonBgColor.ToNative()}");
_secondButton.BackgroundColor = _secondButtonBgColor.ToNative();
}

Expand Down
Loading