using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
public class UIBubblePopupFromCVS : MonoBehaviour
{
Encoding encoding;
private string filename;
private string[] lines;
private int index = 0; //ダブルクォートをカウント
private bool addFlag; //ダブルクォートだったら加えない
char targetChar = '"';
char kanma = ',';
///
/// Root canvas
///
public Canvas canvas;
///
/// Bubble popup
///
public GameObject bubble;
///
/// Title text
///
public Text title;
///
/// Address text
///
public Text address;
///
/// Photo RawImage
///
public RawImage photo;
public List datas;
///
/// Reference to active marker
///
private OnlineMapsMarker targetMarker;
///
/// This method is called when downloading photo texture.
///
/// Photo texture
private void OnDownloadPhotoComplete(OnlineMapsWWW www)
{
Texture2D texture = new Texture2D(1, 1);
www.LoadImageIntoTexture(texture);
// Set place texture to bubble popup
photo.texture = texture;
}
///
/// This method is called by clicking on the map
///
private void OnMapClick()
{
// Remove active marker reference
targetMarker = null;
// Hide the popup
bubble.SetActive(false);
}
///
/// This method is called by clicking on the marker
///
/// The marker on which clicked
private void OnMarkerClick(OnlineMapsMarkerBase marker)
{
// Set active marker reference
targetMarker = marker as OnlineMapsMarker;
// Get a result item from instance of the marker
CData data = marker["data"] as CData;
if (data == null) return;
// Show the popup
bubble.SetActive(true);
// Set title and address
title.text = data.title;
//address.text = data.address;
// Destroy the previous photo
if (photo.texture != null)
{
OnlineMapsUtils.Destroy(photo.texture);
photo.texture = null;
}
//**SO
//OnlineMapsWWW www = new OnlineMapsWWW(data.photo_url);
//www.OnComplete += OnDownloadPhotoComplete;
// Initial update position
UpdateBubblePosition();
}
///
/// Start is called on the frame when a script is enabled just before any of the Update methods are called the first time.
///
private void Start()
{
// Subscribe to events of the map
OnlineMaps.instance.OnChangePosition += UpdateBubblePosition;
OnlineMaps.instance.OnChangeZoom += UpdateBubblePosition;
OnlineMapsControlBase.instance.OnMapClick += OnMapClick;
if (OnlineMapsControlBaseDynamicMesh.instance != null)
{
OnlineMapsControlBaseDynamicMesh.instance.OnMeshUpdated += UpdateBubblePosition;
}
if (OnlineMapsCameraOrbit.instance != null)
{
OnlineMapsCameraOrbit.instance.OnCameraControl += UpdateBubblePosition;
}
if (datas == null) datas = new List();
ReadCVS();
foreach (CData data in datas)
{
OnlineMapsMarker marker = OnlineMapsMarkerManager.CreateItem(data.longitude, data.latitude);
marker["data"] = data;
marker.OnClick += OnMarkerClick;
}
// Initial hide popup
bubble.SetActive(false);
}
private void ReadCVS()
{
filename = Application.persistentDataPath + "/poi_en.csv";
if (!File.Exists(filename)) return;
lines = File.ReadAllLines(filename, Encoding.UTF8);
Debug.Log(lines.Length);
Debug.Log(lines[2]);
//**SO
//string[] test = lines;
for (int i = 1; i < lines.Length; i++)
{
//string test[100];
//test[i] = lines[i];
StringBuilder sb = new StringBuilder(lines[i].Length);
foreach (char c in lines[i])
{
addFlag = true;
char ch = c;
if (c == targetChar) //ダブルクォートだったら
{
addFlag = false;
index++;
}
if (c == kanma) //カンマだったら
{
if (index % 2 != 0) //ダブルクオートの中にあるカンマ
{
ch = '|'; //別の文字に変換
}
}
if (addFlag)
{
sb.Append(ch);
}
}
lines[i] = sb.ToString();
Debug.Log(lines[i]);
}
for (int i = 1; i < lines.Length; i++)
{
string[] parts = lines[i].Split(',');
for (int j = 0; j < parts.Length; j++)
{
parts[j] = parts[j].Replace('|', ','); //別の文字に変換したカンマを元に
Debug.Log(parts[j]);
}
CData data = new CData
{
longitude = double.Parse(parts[12], OnlineMapsUtils.numberFormat),
latitude = double.Parse(parts[11], OnlineMapsUtils.numberFormat),
title = parts[7],
address = parts[9],
status = parts[1],
tradition = parts[2],
photo_url = parts[5],
webpage = parts[19],
description = parts[20],
};
datas.Add(data);
}
}
///
/// Updates the popup position
///
private void UpdateBubblePosition()
{
// If no marker is selected then exit.
if (targetMarker == null) return;
// Hide the popup if the marker is outside the map view
if (!targetMarker.inMapView)
{
if (bubble.activeSelf) bubble.SetActive(false);
}
else if (!bubble.activeSelf) bubble.SetActive(true);
// Convert the coordinates of the marker to the screen position.
Vector2 screenPosition = OnlineMapsControlBase.instance.GetScreenPosition(targetMarker.position);
// Add marker height
screenPosition.y += targetMarker.height;
// Get a local position inside the canvas.
Vector2 point;
RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, screenPosition, null, out point);
// Set local position of the popup
(bubble.transform as RectTransform).localPosition = point;
}
[Serializable]
public class CData
{
public string title;
public string status;
public string tradition;
public string address;
public string photo_url;
public string webpage;
public string description;
public double longitude;
public double latitude;
}
}