using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
namespace InfinityCode.OnlineMapsExamples
{
[AddComponentMenu("Infinity Code/Online Maps/Examples (API Usage)/GroupMarkersExample")]
public class GetTargetDatabase : MonoBehaviour {
///
/// Base texture for grouping marker.
/// On top of this texture will be drawn numbers.
///
public Texture2D groupTexture;
///
/// The number of markers that will be created in the scene.
///
public int countMarkers = 100;
///
/// The minimum distance between the markers.
///
public float distance = 30f / OnlineMapsUtils.tileSize; // pixels / 256
///
/// Texture with numbers (2 rows: 1-5, 6-0).
///
public Texture2D font;
public float myTlt;
public float myTln;
private List markers;
public float tgtLt;
public float tgtLn;
public string helpString;
//public Vector2[] latlns;
public string getURL = "http://gigi.creativetechno.co.uk/display.php";
public string[] items;
public Vector2[] latlns;
public bool loaded = false;
public void Start(){
latlns = new Vector2[100];
StartCoroutine(LoadName());
}
public IEnumerator LoadName(){
//Debug.Log ("Getting name from " + getURL);
//SceneManager.LoadScene ("PlayerScore");
//print out the data returned here
WWW itemsName = new WWW (getURL);
//wait for a response
yield return itemsName;
//Debug.Log ("getting" + testName.text);
string itemsNameString = itemsName.text;
//print ("ItemsNameString " + itemsNameString);
items = itemsNameString.Split (';');
//print ("Items length " + items.Length);
int cc = 0;
for (int i = 0; i < items.Length - 1; i++) {
//for (int i = 0; i <= items.Length; i++) {
//Debug.Log ("items " + items[i]);
markers = new List ();
string helpString = "";
helpString = helpString + items [i] + " ";
i++;
string ScoreName = "";
string ScoreLat = "";
string ScoreLon = "";
ScoreName = helpString;
Debug.Log ("help s " + helpString);
//Getting Lon from mySQL database
ScoreLon = items [i];
string tln = ScoreLon;
tgtLn = float.Parse (tln);
i++;
//Getting Lat from mySQL database
ScoreLat = items [i];
string tlt = ScoreLat;
tgtLt = float.Parse (tlt);
i++;
print ("tgtLn " + tgtLn);
loaded = true;
Vector2 tempV = new Vector2 (tgtLn, tgtLt);
latlns [cc] = tempV;
//print ("tgtLn " + tgtLn);
OnlineMapsMarker marker = OnlineMaps.instance.AddMarker (latlns [cc]);
//Give names to the Markers
marker.label = helpString;
//Add the markers
markers.Add (marker);
/*
for (int k = 0; k < countMarkers; k++)
{
OnlineMapsMarker marker = OnlineMaps.instance.AddMarker (latlns [cc]);
//Give names to the Markers
marker.label = helpString;
//Add the markers
markers.Add (marker);
//Debug.Log ("tgtLn" + tgtLn + "tgtLt" + tgtLt );
//print ("... " + tgtLn);
//print ("myTln " + tgtLt);
//print ("tempV " + tempV.x + " " + tempV.y);
//print ("latlns " + latlns[1]);
}
*/
}
//GroupMarkers ();
}
private void GroupMarkers()
{
List groups = new List();
for (int zoom = 20; zoom >= 3; zoom--)
{
List ms = markers.Select(m => m).ToList();
for (int j = 0; j < ms.Count - 1; j++)
{
OnlineMapsMarker marker = ms[j];
MarkerGroup group = null;
Vector2 pos = OnlineMapsUtils.LatLongToTilef(marker.position, zoom);
int k = j + 1;
while (k < ms.Count)
{
OnlineMapsMarker marker2 = ms[k];
Vector2 pos2 = OnlineMapsUtils.LatLongToTilef(marker2.position, zoom);
if ((pos - pos2).magnitude < distance)
{
if (group == null)
{
group = new MarkerGroup(zoom, groupTexture);
groups.Add(group);
group.Add(marker);
if (marker.range.min == 3) marker.range.min = zoom + 1;
}
group.Add(marker2);
if (marker2.range.min == 3) marker2.range.min = zoom + 1;
ms.RemoveAt(k);
pos = group.tilePosition;
}
else k++;
}
}
}
foreach (MarkerGroup g in groups) g.Apply(font);
}
private class MarkerGroup
{
public List markers;
public OnlineMapsMarker instance;
public Vector2 center;
public Vector2 tilePosition;
public int zoom;
public MarkerGroup(int zoom, Texture2D texture)
{
markers = new List();
this.zoom = zoom;
instance = OnlineMaps.instance.AddMarker(Vector2.zero, texture);
instance.align = OnlineMapsAlign.Center;
instance.range = new OnlineMapsRange(zoom, zoom);
}
public void Add(OnlineMapsMarker marker)
{
markers.Add(marker);
center = markers.Aggregate(Vector2.zero, (current, m) => current + m.position) / markers.Count;
instance.position = center;
tilePosition = OnlineMapsUtils.LatLongToTilef(center, zoom);
instance.label = "Group. Count: " + markers.Count;
}
public void Apply(Texture2D font)
{
int width = instance.texture.width;
int height = instance.texture.height;
Texture2D texture = new Texture2D(width, height, TextureFormat.ARGB32, false);
Color[] colors = instance.texture.GetPixels();
char[] cText = markers.Count.ToString().ToCharArray();
Color[] fontColors = font.GetPixels();
int cw = font.width / 5;
int ch = font.height / 2;
int sx = (int) (width / 2f - cText.Length / 2f * cw);
int sy = (int) (height / 2f - ch / 2f);
for (int i = 0; i < cText.Length; i++)
{
int co = cText[i] - '0' - 1;
if (co < 0) co += 10;
int fx = (co % 5) * cw;
int fy = (1 - co / 5) * ch;
for (int x = 0; x < cw; x++)
{
for (int y = 0; y < ch; y++)
{
int fi = (fy + y) * font.width + fx + x;
int ci = (sy + y) * width + sx + x + i * cw;
Color fc = fontColors[fi];
colors[ci] = Color.Lerp(colors[ci], new Color(fc.r, fc.g, fc.b, 1), fc.a);
}
}
}
texture.SetPixels(colors);
texture.Apply();
instance.texture = texture;
}
}
string GetDataValue(string data, string name){
string value = data.Substring (data.IndexOf (name) + name.Length);
if (value.Contains ("|"))
value = value.Remove (value.IndexOf ("|"));
return value;
}
}
}