// SpawnPrefabAtGeoLocation(GameObject, Vector2d, Action<List<GameObject>>, Boolean, String) // A Vector2d(Latitude Longitude) object // https://www.mapbox.com/mapbox-unity-sdk/api/unity/Mapbox.Unity.Map.AbstractMap.html private void AddPointToMap(BTCleanPOI bnew) { GameObject prefab = GetPrefab(bnew.POIType); Mapbox.Utils.Vector2d vec = new Mapbox.Utils.Vector2d(bnew.Latitude, bnew.Longitude); string sname = string.Format("{0} - {1}", bnew.ID, bnew.POIType); map.SpawnPrefabAtGeoLocation(prefab, vec, null, true, sname); //if (GameManager.Instance.Setting.CreateWithoutPredefined) //{ // string sname = string.Format("{0} - {1}", bnew.ID, bnew.POIType); // map.SpawnPrefabAtGeoLocation(prefab, vec, null, true, sname); //} //else //{ // if (AddPrefabPOI(vec, bnew) == true) // { // LogManager.Instance.AddLog(string.Format("Success Added in map POI (prefabed) using coordinates - {0}", bnew.ID)); // } // else // { // string sname = string.Format("{0} - {1}", bnew.ID, bnew.POIType); // map.SpawnPrefabAtGeoLocation(prefab, vec, null, true, sname); // } //} }
void InputPOIToMap() { List <BTCleanPOI> btlist = BTService.ListPoints; foreach (BTCleanPOI pp in btlist) { // check if pp is already on map BTCleanPOI found = POIOnMap.Where(x => x.ID == pp.ID).FirstOrDefault(); if (found != null) { continue; } BTCleanPOI bnew = new BTCleanPOI() { ID = pp.ID, Longitude = pp.Longitude, Latitude = pp.Latitude, LastUpdate = pp.LastUpdate, POIType = pp.POIType }; if (pp.Address != null) { bnew.Address = bnew.Address; } LogManager.Instance.AddLog(string.Format("Try to put POI on MAP - {0}", bnew.ID)); AddPointToMap(bnew); POIOnMap.Add(bnew); NewPointsAdded += 1; } }
void MultiUpdatePOI() { string[] POITypes = { "Pole", "Cabinet", "Node", "BillBoard", "Sign" }; for (int i = 0; i < POITypes.Length; i++) { List <BTCleanPOI> lcat = BTPOIManager.Instance.GetListByCategory(POITypes[i]); if (lcat.Count == 0) { continue; } GameObject prefab = GetPrefab(POITypes[i]); Vector2d[] LatLon = new Vector2d[lcat.Count]; for (int j = 0; j < lcat.Count; j++) { LatLon[j] = new Vector2d(lcat[j].Latitude, lcat[j].Longitude); BTCleanPOI bnew = new BTCleanPOI() { ID = lcat[j].ID, Longitude = lcat[j].Longitude, Latitude = lcat[j].Latitude, LastUpdate = lcat[j].LastUpdate, POIType = lcat[j].POIType }; if (lcat[j].Address != null) { bnew.Address = lcat[j].Address; } POIOnMap.Add(bnew); } string sname = string.Format("{0} count {1}", lcat.Count, POITypes[i]); map.SpawnPrefabAtGeoLocation(prefab, LatLon, cclback, true, sname); } }
private string PrepareText(BTCleanPOI poi) { System.Text.StringBuilder sb = new System.Text.StringBuilder(100); sb.Append("POI\r\n"); sb.Append(poi.PrepareFullPOIDescription()); return(sb.ToString()); }
private void InsertNewSpawnPOI(BTCleanPOI found) { int index = -1; if (FindPOITypeIndex(found, out index) == false) { return; } SpawnPrefab(found, POIPrefabs[index]); }
/// <summary> /// This method raises an event with the POI that has full information, after the decision /// of Fuzzy Logic Agent /// </summary> /// <param name="poi">POI with information</param> /// <param name="address">Reverse GPS info for this POI</param> private void callback(BTCleanPOI poi, ReverseAddress address) { // here is a good place to display extra info to user poi.Address = address; // update BTPOIManager BTPOIManager.Instance.AppendPOIAddress(poi); if (AgentInformation != null) // if event is used { AgentInformation(poi); // raise an event to inform UI about agent actions } }
private void buttonclicked() { if (GPSData.Instance.Ready == false) { return; } BTCleanPOI pp = CreateDebugPOI(); BTService.ListPoints.Add(pp); // and debug poi to Service List LogManager.Instance.AddLog(string.Format("Added New POI: {0} at current location", pp.ID)); }
private void SpawnPrefab(BTCleanPOI pp, GameObject prefab) { Vector2d vv = new Vector2d(pp.Latitude, pp.Longitude); _locations.Add(vv); var instance = Instantiate(prefab); instance.transform.localPosition = _map.GeoToWorldPosition(vv, true); instance.transform.localScale = new Vector3(_spawnScale, _spawnScale, _spawnScale); instance.tag = pp.POIType; instance.name = pp.ID.ToString(); _spawnedObjects.Add(instance); }
public void AppendPOIAddress(BTCleanPOI poi) { ReverseAddress address = poi.Address; BTCleanPOI found = mcleanlist.Where(x => x.ID == poi.ID).FirstOrDefault(); if (found == null) { mcleanlist.Add(poi); found = poi; } else { found.Address = address; found.Score = poi.Score; } }
private void LocatePOIByPosition(Transform transform) { Transform pos = transform; // Node is a composite model, so take parent string stype = transform.tag; Vector3 vv = pos.localPosition; //convert to latlong var latlong = map.WorldToGeoPosition(vv); List <BTCleanPOI> lpoi = BTPOIManager.Instance.ListPoints; BTCleanPOI pfound = null; double mindistance = 0; foreach (var pp in lpoi) { if (string.Compare(pp.POIType, stype, true) != 0) { continue; } Vector2d vpp = new Vector2d(pp.Latitude, pp.Longitude); if (pfound == null) { pfound = pp; mindistance = Vector2d.Distance(latlong, vpp); } else { double distance = Vector2d.Distance(latlong, vpp); if (distance < mindistance) { pfound = pp; mindistance = distance; } } } if (pfound != null) { LogManager.Instance.AddLog(string.Format("Found POI to show: {0}", pfound.ID)); cinfo.DisplayCustomText(pfound.GetPoiDescriptionByScore()); } else { LogManager.Instance.AddLog(string.Format("Didn't Found POI to show for tag: {0}", stype)); } }
/// <summary> /// This is a hack on mapbox points of interest /// Scans all entries in PointsOfInterestSublayerList for the map.VectorData /// If Finds a List with the Prefab Type, like eg. Pole /// Then gets the array of POI (string) array and ads a new entry to the end /// </summary> /// <param name="vec"></param> /// <param name="Name"></param> /// <returns></returns> private bool AddPrefabPOI(Mapbox.Utils.Vector2d vec, BTCleanPOI bnew) { if (map.VectorData == null) { return(false); } string Name = null; if (string.Compare(bnew.POIType, "Pole", true) == 0) { Name = "Pole"; } else if (string.Compare(bnew.POIType, "Cabinet", true) == 0) { Name = "Street Cabinet"; } else if (string.Compare(bnew.POIType, "BillBoard", true) == 0) { Name = "Billboard"; } else if (string.Compare(bnew.POIType, "Sign", true) == 0) { Name = "Sign"; } else if (string.Compare(bnew.POIType, "Node", true) == 0) { Name = "Nodes"; } else { Name = "Pole"; } foreach (var ll in map.VectorData.PointsOfInterestSublayerList) { if (string.Compare(ll.prefabItemName, Name, true) == 0) { List <string> sdata = ll.coordinates.ToList(); string snew = vec.x + ", " + vec.y; sdata.Add(snew); ll.coordinates = sdata.ToArray(); return(true); } } return(false); }
public static POIAddress GetPOIFromAddress(ReverseAddress ad, BTCleanPOI poi) { POIAddress po = new POIAddress(); po.ID = poi.ID; po.city = ad.city; po.country = ad.country; po.country_code = ad.country_code; po.county = ad.county; po.house_number = ad.house_number; po.postcode = ad.postcode; po.road = ad.road; po.state = ad.state; po.state_district = ad.state_district; po.suburb = ad.suburb; po.town = ad.town; po.village = ad.village; return(po); }
private void LocatePOIByName(Transform transform) { string sID = transform.name; int ID = 0; // Node is a composite model, so take parent name sID = transform.name; if (int.TryParse(sID, out ID) == false) { return; } List <BTCleanPOI> lpoi = BTPOIManager.Instance.ListPoints; BTCleanPOI pfound = lpoi.Where(x => x.ID == ID).FirstOrDefault(); if (pfound != null) { cinfo.DisplayCustomText(pfound.GetPoiDescriptionByScore()); } }
private bool FindPOITypeIndex(BTCleanPOI found, out int index) { index = -1; for (int i = 0; i < PoiTypes.Length; i++) { if (string.Compare(PoiTypes[i], found.POIType, true) == 0) { index = i; break; } } if (index >= 0) { return(true); } else { return(false); } }
private void InsertManagerPOI() { if (BTPOIManager.Instance.ListPoints.Count == 0) { return; } // if manager has already points, insert them to the map if (BTService.ListPoints.Count > 0) { return; } foreach (BTCleanPOI pp in BTPOIManager.Instance.ListPoints) { BTCleanPOI bnew = new BTCleanPOI() { ID = pp.ID, Longitude = pp.Longitude, Latitude = pp.Latitude, LastUpdate = pp.LastUpdate, POIType = pp.POIType }; if (pp.Address != null) { bnew.Address = bnew.Address; } AddPointToMap(bnew); POIOnMap.Add(bnew); } // activate //if (map.gameObject.activeSelf == false) //{ // LogManager.Instance.AddLog("Map is now Activated! Should display POIs!"); // map.gameObject.SetActive(true); //} //else //{ // LogManager.Instance.AddLog("Map is Reset! Should display POIs!"); // Vector2d vv = new Vector2d(GPSData.Instance.Latitude, GPSData.Instance.Longitude); // //map.Initialize(vv, map.AbsoluteZoom); // map.ResetMap(); // // map.UpdateMap(vv, map.Zoom); //} }
public void ProcessReverseCleanPoint(BTCleanPOI poi, OnSuccessAddressCleanPOI callback) { // if has address already no need to call endpoint if (poi.Address != null) { callback(poi, poi.Address); return; } if (SaveReveseAddressToDatabase) { // check if exists in local database POIAddress ad = db.GetPoiAddressByID(poi.ID); if (ad != null) // if record is found, avoid to call open map service { ReverseAddress rev = POIAddress.GetAddressFromPOI(ad); callback(poi, rev); return; } } StartCoroutine(GetReverseCleanPOI(poi, callback)); }
// Update is called once per frame void Update() { if (Input.GetMouseButtonDown(0)) { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit)) { if (cinfo.IsVisible()) { return; } if (hit.transform.tag == "Pole" || hit.transform.tag == "Node" || hit.transform.tag == "Cabinet") { string sID = hit.transform.parent.name; int ID = 0; if (int.TryParse(sID, out ID) == false) { return; } List <BTCleanPOI> lpoi = BTPOIManager.Instance.ListPoints; BTCleanPOI pfound = lpoi.Where(x => x.ID == ID).FirstOrDefault(); if (pfound != null) { cinfo.DisplayCustomText(pfound.GetPoiDescriptionByScore()); } } else if (hit.transform.tag == "Player") { cinfo.DisplayCustomText("This is Your Position!"); } } } }
BTCleanPOI CreateDebugPOI() { BTCleanPOI pp = new BTCleanPOI(); pp.ID = GameManager.Instance.GetNextCounterValue(); int type = pp.ID % 3; if (type == 0) { pp.POIType = "Pole"; } else if (type == 1) { pp.POIType = "Cabinet"; } else if (type == 2) { pp.POIType = "Node"; } pp.Longitude = GPSData.Instance.Longitude; pp.Latitude = GPSData.Instance.Latitude; pp.LastUpdate = DateTime.Now; return(pp); }
IEnumerator GetPOI() { // Read GPS Position from singleton, using gps sensor of the device if (GPSData.Instance.Ready) { float lat = GPSData.Instance.Latitude; float longt = GPSData.Instance.Longitude; if (lat != 0 || longt != 0) // if pc with no gps avoid it { latitude = lat; longitude = longt; } } // now apply radius from global settings if (GameManager.Instance.Setting.GPSRadius > 0 && radius != GameManager.Instance.Setting.GPSRadius) { radius = GameManager.Instance.Setting.GPSRadius; } string surl = string.Format("{0}query?radius={1}&latitude={2}&longitude={3}", url, radius, GPSPoint.GetDoubleValue(latitude), GPSPoint.GetDoubleValue(longitude)); string authorization = authenticate(user, password); UnityWebRequest www = UnityWebRequest.Get(surl); www.SetRequestHeader("AUTHORIZATION", authorization); www.SetRequestHeader("Content-Type", "application/json"); // pass control until request is completed yield return(www.SendWebRequest()); if (www.isNetworkError || www.isHttpError) { Debug.Log(www.error); LogManager.Instance.AddLog(string.Format("Error calling BT: {0}", www.error)); } else { Debug.Log("Form upload complete! Status Code: " + www.responseCode); byte[] result = www.downloadHandler.data; string POIJSON = System.Text.Encoding.Default.GetString(result); // prepare JSON to comply with Unity JsonUtility System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("{ \"POI\" : "); sb.Append(POIJSON); sb.Append("}"); BTPOIInfo info = JsonUtility.FromJson <BTPOIInfo>(sb.ToString()); ListPoints.Clear(); foreach (BTPOI p in info.POI) { BTCleanPOI cl = new BTCleanPOI(p); ListPoints.Add(cl); // LogManager.Instance.AddLog(string.Format("Added POI: {0}", cl.ID)); } // Debug.LogFormat("Uploaded total {0} POI from BT!", ListPoints.Count); LogManager.Instance.AddLog(string.Format("POI from BT - Total: {0}", ListPoints.Count)); // now update BTManager BTPOIManager.Instance.ApendListData(ListPoints); PrevPoint = new GPSPoint() { latitude = latitude, longitude = longitude }; } }
public void ShowPOIInfo(BTCleanPOI poi) { CheckCountdown = ShutdownInSeconds; POItext.text = PrepareText(poi); panel.SetActive(true); }
IEnumerator GetReverseCleanPOI(BTCleanPOI poi, OnSuccessAddressCleanPOI callback) { if (poi == null) { ready = false; yield return(null); } Succes = false; string surl = string.Format("{0}?format=jsonv2&lat={1}&lon={2}&zoom={3}&addressdetails=1", url, GPSPoint.GetDoubleValue(poi.Latitude), GPSPoint.GetDoubleValue(poi.Longitude), zoomlevel); // email is necessary, for this service to work as stated in it's web site if (string.IsNullOrEmpty(email) == false) { surl = string.Format("{0}&email={1}", surl, email); } UnityWebRequest www = UnityWebRequest.Get(surl); yield return(www.SendWebRequest()); while (!www.isDone) { yield return(null); } if (www.isNetworkError || www.isHttpError) { Succes = false; Debug.Log(www.error); LogManager.Instance.AddLog(string.Format("Error: Calling Reverse GPS Service: {0}", www.error)); ready = false; yield return(null); } byte[] resultdata = www.downloadHandler.data; string POIJSON = System.Text.Encoding.Default.GetString(resultdata); ReverseGPS revgps = JsonUtility.FromJson <ReverseGPS>(POIJSON); Succes = true; ready = true; // update flag from global setting if (SaveReveseAddressToDatabase != GameManager.Instance.Setting.SaveReversePOIToSQLite) { SaveReveseAddressToDatabase = GameManager.Instance.Setting.SaveReversePOIToSQLite; } // now save this to database if (SaveReveseAddressToDatabase) { POIAddress poiad = POIAddress.GetPOIFromAddress(revgps.address, poi); try { if (db != null) { // check if POI already exists in SQLite DB POIAddress ad = db.GetPoiAddressByID(poi.ID); if (ad == null) { db.InsertPoiAddress(poiad); LogManager.Instance.AddLog(string.Format("Success insert to SQLite Poi: {0}", poi.ID)); } } } catch (Exception ex) { LogManager.Instance.AddLog(string.Format("Failed insert to SQLite Poi: {0} - {1}", poi.ID, ex.Message)); } } // update the caller thru callback if (callback != null) { callback(poi, revgps.address); } }
// Run fuzzy logic on the poi // Stores the result of fuzzy logic in score member of BTCleanPOI class private bool RunFuzzyEvaluations(BTCleanPOI poi) { if (GPSData.Instance.Ready == false) { return(false); } if (GameManager.Instance.Setting.DebugMode != IsDebug) { IsDebug = GameManager.Instance.Setting.DebugMode; } if (poi.Score != null) { TimeSpan sp = DateTime.Now - poi.Score.lastupdate; if (sp.Minutes < MinutesIntervalRefresh) // skip run score again, if is too soon { return(false); } } // get distance from device (user) float distancemeters = CalculateDistance(GPSData.Instance.Latitude, poi.Latitude, GPSData.Instance.Longitude, poi.Longitude); // get device speed (user) float speed = GPSData.Instance.Speed; // apply values to fuzzy logic mngr.ApplyVariableValue("Distance", distancemeters); mngr.ApplyVariableValue("Speed", speed); // run fuzzy logic centroid method, get the crisp result double result = mngr.DefuzzifierCentroid(); // convert result to action string sAction = ""; if (result >= 75) { sAction = "All"; } else if (result > 35) { sAction = "Medium"; } else { sAction = "Few"; } if (IsDebug) { LogManager.Instance.AddLog(string.Format("Fuzzy poi:{0} - Distance = {1} Speed = {2} Result = {3} ScoreType={4}", poi.ID, distancemeters, speed, result, sAction)); } // save fuzzy score on POI poi.Score = PrepareScore(result, sAction); // if full metadata is required, call ReversePOI to get this information // if metadata already exist from previous calls, avoid the reverse call if (sAction == "All" && poi.Address == null) { // call reverse Poi to get more information for this POI, and then inform the UI to show this info ReversePOI.ProcessReverseCleanPoint(poi, callback); return(true); } return(false); }