This repository has been archived by the owner on Dec 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
[Map]Adds GoogleMapView #183
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f35dae3
[Map]Add Map APIs (first impl)
jkpu 2cd4339
Modified following to PR review
jkpu 04e9b12
Fix build error
jkpu cb708c6
Modified following to second PR review
jkpu 7b99724
Modified following to third PR review
jkpu 26d9aca
remove unused nuget package
jkpu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
247 changes: 247 additions & 0 deletions
247
src/Tizen.Wearable.CircularUI.Forms.Renderer/GoogleMapViewRenderer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
43
src/Tizen.Wearable.CircularUI.Forms.Renderer/GoogleMaps.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
_apiKey = apiKey; | ||
jkpu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
IsInitialized = true; | ||
} | ||
|
||
public static string GetKey() | ||
{ | ||
return _apiKey; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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