using UnityEngine; using System.Collections; using System.Collections.Generic; public class SM_CreateMarkers : MonoBehaviour { private SM_ExchangeSessionInfomation[] exchangeSessions; [SerializeField] private GameObject tooltipPrefab; [SerializeField] public GameObject container; private List lstToolTips = new List(); private List lstMapMarkers = new List(); private UIWidget widget; private UILabel labelNumberOfStarTransfer,labelRateExchange; private GameObject tooltip; private SM_ShowDetailInformationOfExchangeController sm_ShowDetailInformationOfExchangeController; // Use this for initialization void Start () { // OnlineMapsMarkerBase.OnMarkerDrawTooltip += OnMarkerDrawTooltip; // DemoRandomCoordinateOfExchangeSessions(); sm_ShowDetailInformationOfExchangeController = GetComponent(); } // Update is called once per frame private void DemoRandomCoordinateOfExchangeSessions() { OnlineMapsMarker tempMarker; exchangeSessions = new SM_ExchangeSessionInfomation[5]; for (int i = 0; i < exchangeSessions.Length; i++) { exchangeSessions[i] = new SM_ExchangeSessionInfomation(); exchangeSessions[i].dLatitude = Random.Range(1.0f ,2.0f); exchangeSessions[i].dLongitude = Random.Range(1.0f, 2.0f); tempMarker = OnlineMaps.instance.AddMarker(exchangeSessions[i].dLongitude, exchangeSessions[i].dLatitude, "123.234"); tempMarker.OnDrawTooltip += OnDrawTooltip; // Debug.Log(tempMarker.position); tempMarker.OnClick += OnMarkerClick; } } //Function draw all exchange sessions in screen public void DrawExchangeSessions(IList exchangeSessionList) { //Clear all markers and tooltips lstMapMarkers.Clear(); lstToolTips.Clear(); OnlineMapsMarker tempMarker; exchangeSessions = new SM_ExchangeSessionInfomation[exchangeSessionList.Count]; for (int i = 0; i < exchangeSessionList.Count; i++) { IDictionary exchangeInfo = exchangeSessionList[i] as IDictionary; exchangeSessions[i] = new SM_ExchangeSessionInfomation(); //get lattitude and Longtitude and draw marker exchangeSessions[i].dLatitude = GetLatitude(exchangeInfo["axis"].ToString()); exchangeSessions[i].dLongitude = GetLongtitude(exchangeInfo["axis"].ToString()); exchangeSessions[i].exchangeRate = float.Parse(exchangeInfo["rate"].ToString()); exchangeSessions[i].starTransfer = float.Parse(exchangeInfo["statran"].ToString()); tempMarker = OnlineMaps.instance.AddMarker(exchangeSessions[i].dLatitude, exchangeSessions[i].dLongitude,exchangeInfo["advertsid"].ToString()); tempMarker.OnDrawTooltip += OnDrawTooltip; tempMarker.OnClick += OnMarkerClick; } } private double GetLatitude(string axis) { int indexComma = axis.IndexOf(','); return double.Parse(axis.Substring(0,indexComma)); } private double GetLongtitude(string axis) { int indexComma = axis.IndexOf(','); return double.Parse(axis.Substring(indexComma + 1,axis.Length - indexComma - 1)); } private void OnMarkerClick(OnlineMapsMarkerBase marker) { StartCoroutine(sm_ShowDetailInformationOfExchangeController.GetInformationOfExchange(marker.label)); } private void DrawToolTipForMarker(OnlineMapsMarker marker) { // OnlineMapsMarkerBase tooltipMarker = OnlineMaps.instance.tooltipMarker; int markerIndex = GetMarkerIndexInList(marker); if (markerIndex == -1) { lstMapMarkers.Add(marker); } if (marker != null && !string.IsNullOrEmpty(marker.label)) { if (markerIndex == -1) { tooltip = Instantiate(tooltipPrefab) as GameObject; tooltip.transform.parent = container.transform; tooltip.transform.localScale = Vector3.one; lstToolTips.Add(tooltip); markerIndex = lstToolTips.Count - 1; } widget = lstToolTips[markerIndex].GetComponent(); labelNumberOfStarTransfer = lstToolTips[markerIndex].transform.GetChild(1).transform.GetChild(0).GetComponent(); labelRateExchange = lstToolTips[markerIndex].transform.GetChild(2).transform.GetChild(0).GetComponent(); Vector2 screenPosition = OnlineMapsControlBase.instance.GetScreenPosition(marker.position); float ratio = (float)widget.root.activeHeight / Screen.height; float width = Mathf.Ceil(Screen.width * ratio); screenPosition.x = (screenPosition.x / Screen.width - 0.5f) * width; screenPosition.y = (screenPosition.y / Screen.height - 0.5f) * widget.root.activeHeight; labelNumberOfStarTransfer.text = exchangeSessions[markerIndex].starTransfer.ToString(); labelRateExchange.text = exchangeSessions[markerIndex].exchangeRate.ToString(); Vector2 buttonOffset = new Vector2(-widget.width / 2, widget.height); // widget.SetRect(screenPosition.x + buttonOffset.x, screenPosition.y + buttonOffset.y, widget.width, widget.height); widget.SetRect(screenPosition.x + buttonOffset.x, screenPosition.y + 30, widget.width, widget.height); } // else if (tooltip != null) // { // OnlineMapsUtils.DestroyImmediate(tooltip); // tooltip = null; // } } private void OnDrawTooltip(OnlineMapsMarkerBase marker) { // Debug.Log(marker.label); // Here you draw the tooltip for the marker. DrawToolTipForMarker(marker as OnlineMapsMarker); } // private void OnMarkerDrawTooltip(OnlineMapsMarkerBase marker) // { // Debug.Log(marker.label); // // Here you draw the tooltip for the marker. // // DrawToolTipForMarker(marker as OnlineMapsMarker); // } private int GetMarkerIndexInList(OnlineMapsMarker marker) { for (int i = 0; i < lstMapMarkers.Count; i++) { if (marker == lstMapMarkers[i]) return i; } return -1; } }