Topic: Adding Markers and assigned them to the same layer of the Map object ?

Is there already a way to do this ?

If not.  How do we do this without eiditng the source code ?

Re: Adding Markers and assigned them to the same layer of the Map object ?

1. What markers do you use (2D, 3D, Billboard)?
2. What control do you use (Tileset or other)?
3. For what it you need?

Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.

Re: Adding Markers and assigned them to the same layer of the Map object ?

Hello

1. What markers do you use (2D, 3D, Billboard)? 2D.
2. What control do you use (Tileset or other)? Tileset - aimed for mobile.
3. For what it you need? To mask out things/Mixing with other UI elements - we might ablel to work with the default layer but somewhere down the line we may need to put the map and makers on a seperate layer for control.

Re: Adding Markers and assigned them to the same layer of the Map object ?

2D markers is not UI element.
By default, 2D markers are drawn in a single mesh.
You can change the layer for all markers together, but not individually.

If you want to have markers as UI elements, you need to make your own marker system for uGUI.
Example:
http://infinity-code.com/atlas/online-m … ample.html

Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.

Re: Adding Markers and assigned them to the same layer of the Map object ?

I am trying to use this method. I placed uGUICustomMarkerEngineExample.cs on the map object. Made my Canvas my Rect and created a prefab that is simply an image uGUI element with a textfield as a child. Set everything on that Object.

I can launch the prefab just fine and it appears in the scene but then it starts throwing them same error over and over again. which is from line 54 of uGUICustomMarkerEngineExample  _instance.UpdateMarker(marker); & Line 136 int countX = api.width / OnlineMapsUtils.tileSize;

Am I missing something else that is suppose to be added? It says to place this script on the Map object, but that is it.

Re: Adding Markers and assigned them to the same layer of the Map object ?

Very interesting why nobody wrote about this problem before.

Try it:

/*     INFINITY CODE 2013-2016      */
/*   http://www.infinity-code.com   */

using System.Collections.Generic;
using UnityEngine;

namespace InfinityCode.OnlineMapsExamples
{
    [AddComponentMenu("Infinity Code/Online Maps/Examples (API Usage)/uGUICustomMarkerEngineExample")]
    public class uGUICustomMarkerEngineExample : MonoBehaviour 
    {
        private static uGUICustomMarkerEngineExample _instance;
        private static List<uGUICustomMarkerExample> markers;

        public RectTransform markerContainer;
        public GameObject markerPrefab;

        private GameObject container;
        private bool needUpdateMarkers;
        private Canvas canvas;
        private OnlineMaps map;

        public static uGUICustomMarkerEngineExample instance
        {
            get { return _instance; }
        }

        private Camera worldCamera
        {
            get
            {
                if (canvas.renderMode == RenderMode.ScreenSpaceOverlay) return null;
                return canvas.worldCamera;
            }
        }

        public static uGUICustomMarkerExample AddMarker(Vector2 position, string text)
        {
            return AddMarker(position.x, position.y, text);
        }

        public static uGUICustomMarkerExample AddMarker(double lng, double lat, string text)
        {
            GameObject markerGameObject = Instantiate(_instance.markerPrefab) as GameObject;
            (markerGameObject.transform as RectTransform).SetParent(_instance.markerContainer);
            markerGameObject.transform.localScale = Vector3.one;
            uGUICustomMarkerExample marker = markerGameObject.GetComponent<uGUICustomMarkerExample>();
            marker.text = text;
            marker.lng = lng;
            marker.lat = lat;
            markers.Add(marker);
            _instance.UpdateMarker(marker);
            return marker;
        }

        private void OnEnable()
        {
            _instance = this;
            markers = new List<uGUICustomMarkerExample>();
            canvas = markerContainer.GetComponentInParent<Canvas>();
        }

        public static void RemoveAllMarkers()
        {
            foreach (uGUICustomMarkerExample marker in markers)
            {
                marker.Dispose();
                OnlineMapsUtils.DestroyImmediate(marker.gameObject);
            }
            markers.Clear();
        }

        public static void RemoveMarker(uGUICustomMarkerExample marker)
        {
            OnlineMapsUtils.DestroyImmediate(marker.gameObject);
            marker.Dispose();
            markers.Remove(marker);
        }

        public static void RemoveMarkerAt(int index)
        {
            if (index < 0 || index >= markers.Count) return;

            uGUICustomMarkerExample marker = markers[index];
            OnlineMapsUtils.DestroyImmediate(marker.gameObject);
            marker.Dispose();
            markers.RemoveAt(index);
        }

        private void Start ()
        {
            map = OnlineMaps.instance;
            map.OnMapUpdated += UpdateMarkers;

            if (OnlineMapsControlBase.instance is OnlineMapsControlBase3D) OnlineMapsControlBase3D.instance.OnCameraControl += UpdateMarkers;

            AddMarker(new Vector2(), "12");
        }

        public void GetPosition(double lng, double lat, double tlx, double tly, out double px, out double py)
        {
            double dx, dy, dtx, dty;
            map.projection.CoordinatesToTile(lng, lat, map.buffer.apiZoom, out dx, out dy);
            map.projection.CoordinatesToTile(tlx, tly, map.buffer.apiZoom, out dtx, out dty);
            dx -= dtx;
            dy -= dty;
            int maxX = 1 << map.zoom;
            if (dx < maxX / -2) dx += maxX;
            px = dx * OnlineMapsUtils.tileSize;
            py = dy * OnlineMapsUtils.tileSize;
        }

        public Vector2 GetScreenPosition(double lng, double lat, double tlx, double tly)
        {
            double mx, my;
            GetPosition(lng, lat, tlx, tly, out mx, out my);
            mx /= map.width;
            my /= map.height;
            Rect mapRect = OnlineMapsControlBase.instance.GetRect();
            mx = mapRect.x + mapRect.width * mx;
            my = mapRect.y + mapRect.height - mapRect.height * my;
            return new Vector2((float)mx, (float)my);
        }

        private void UpdateMarkers()
        {
            foreach (uGUICustomMarkerExample marker in markers) UpdateMarker(marker);
        }

        private void UpdateMarker(uGUICustomMarkerExample marker)
        {
            double tlx, tly, brx, bry;

            int countX = map.width / OnlineMapsUtils.tileSize;
            int countY = map.height / OnlineMapsUtils.tileSize;

            double px, py;
            map.projection.CoordinatesToTile(map.buffer.apiPosition.x, map.buffer.apiPosition.y, map.buffer.apiZoom, out px, out py);

            px -= countX / 2f;
            py -= countY / 2f;

            map.projection.TileToCoordinates(px, py, map.buffer.apiZoom, out tlx, out tly);

            px += countX;
            py += countY;

            map.projection.TileToCoordinates(px, py, map.buffer.apiZoom, out brx, out bry);

            px = marker.lng;
            py = marker.lat;

            if (px < tlx || px > brx || py < bry || py > tly)
            {
                marker.gameObject.SetActive(false);
                return;
            }

            Vector2 screenPosition = GetScreenPosition(px, py, tlx, tly);

            RectTransform markerRectTransform = marker.transform as RectTransform;

            if (!marker.gameObject.activeSelf) marker.gameObject.SetActive(true);

            screenPosition.y += markerRectTransform.rect.height / 2;

            Vector2 point;
            RectTransformUtility.ScreenPointToLocalPointInRectangle(markerRectTransform.parent as RectTransform, screenPosition, worldCamera, out point);
            markerRectTransform.localPosition = point;
        }
    }
}
Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.

7 (edited by IIIDefektIII 2016-10-24 18:44:30)

Re: Adding Markers and assigned them to the same layer of the Map object ?

***EDITED***

I have it all figured out now. I was using tileset with it. I had to look at your SpriteRenderer to figure out how to set up the UIImage Control and now it seems to be working fine.

Thanks for the reply! That fixed the update issue. I still think I'm lost on the set up of this and which components need to work with it.

Do I add a canvas component to the Map object or can I use a separate Canvas object?
Is there a different component that I need other then the OnlineMapsTileSetControl? Since I'm using uGUI?

As is I have the following components on the Map object.
OnlineMaps
OnlineMapsTileSetControl
uGUICustomMarkerEngineExample
LockPositionAndZoomExample

In the Hierarchy I have:
Main Camera
Map
Canvas

When I run it I have it set to long press add a marker using:
Vector3 mouseGeoLocation = OnlineMapsControlBase.instance.GetCoords(mapPosition);
uGUICustomMarkerEngineExample.AddMarker(mouseGeoLocation, "Text");

mapPosition is the Input.MousePosition or Touch Position;

This Adds the prefab UIImage with the uGUICustomMarkerExample component on it to the Canvas. Which it gets added to the canvas, lat and long look correct, but it's always placed in the bottom left corner of the canvas and doesn't move with the map in the correct position. I know I'm doing something wrong but don't know what.

Re: Adding Markers and assigned them to the same layer of the Map object ?

To fix it need to add this method into «OnlineMapsControlBase3D»:

public override Vector2 GetScreenPosition(double lng, double lat)
{
    double px, py;
    GetPosition(lng, lat, out px, out py);
    px /= api.width;
    py /= api.height;
    Vector3 worldPos = new Vector3();

    if (this is OnlineMapsTileSetControl)
    {
        worldPos = transform.position + transform.rotation * new Vector3((float)(-api.tilesetSize.x * px * transform.lossyScale.x), 0, (float)(api.tilesetSize.y * py * transform.lossyScale.z));
    }
    else
    {
        Bounds bounds = cl.bounds;
        worldPos.x = (float)(bounds.max.x - bounds.size.x * px);
        worldPos.y = bounds.min.y;
        worldPos.z = (float)(bounds.min.z + bounds.size.z * py);
    }

    Camera cam = activeCamera ?? Camera.main;
    return cam.WorldToScreenPoint(worldPos);
}

And use a new version of example (attached).
The next version of Online Maps will contain these changes.

Post's attachments

Attachment icon uGUICustomMarkerEngineExample.cs 5.33 kb, 280 downloads since 2016-10-25 

Attachment icon uGUICustomMarkerExample.cs 744 b, 343 downloads since 2016-10-25 

Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.