Example #1
0
    public override void TileToCoordinates(double tx, double ty, int zoom, out double lng, out double lat)
    {
        double mapSize = OnlineMapsUtils.tileSize << zoom;

        lng = 360 * (OnlineMapsUtils.Repeat(tx * OnlineMapsUtils.tileSize, 0, mapSize - 1) / mapSize - 0.5);
        lat = 90 - 360 * Math.Atan(Math.Exp((OnlineMapsUtils.Clip(ty * OnlineMapsUtils.tileSize, 0, mapSize - 1) / mapSize - 0.5) * PI2)) / Math.PI;
    }
    public override void CoordinatesToTile(double lng, double lat, int zoom, out double tx, out double ty)
    {
        lat = OnlineMapsUtils.Clip(lat, -85, 85);
        lng = OnlineMapsUtils.Repeat(lng, -180, 180);

        double rLon = lng * DEG2RAD;
        double rLat = lat * DEG2RAD;

        double a = 6378137;
        double k = 0.0818191908426;

        double z  = Math.Tan(PID4 + rLat / 2) / Math.Pow(Math.Tan(PID4 + Math.Asin(k * Math.Sin(rLat)) / 2), k);
        double z1 = Math.Pow(2, 23 - zoom);

        tx = (20037508.342789 + a * rLon) * 53.5865938 / z1 / 256;
        ty = (20037508.342789 - a * Math.Log(z)) * 53.5865938 / z1 / 256;
    }
    /// <summary>
    /// Updates billboard markers.
    /// </summary>
    protected void UpdateMarkersBillboard()
    {
        if (markersGameObjects == null)
        {
            InitMarkersMesh(0);
        }
        if (markerBillboards == null)
        {
            markerBillboards = new Dictionary <int, OnlineMapsMarkerBillboard>();
        }
        //  Debug.Log("update " + markerBillboards.Count());
        double tlx, tly, brx, bry;

        map.GetCorners(out tlx, out tly, out brx, out bry);
        if (brx < tlx)
        {
            brx += 360;
        }

        int maxX = (2 << map.buffer.apiZoom) / 2;

        double px, py;

        map.projection.CoordinatesToTile(tlx, tly, map.zoom, out px, out py);

        float yScale = GetBestElevationYScale(tlx, tly, brx, bry);

        Bounds  mapBounds      = cl.bounds;
        Vector3 positionOffset = transform.position - mapBounds.min;
        Vector3 size           = mapBounds.size;

        size = transform.rotation * size;
        if (map.target == OnlineMapsTarget.tileset)
        {
            positionOffset.x -= size.x;
        }

        foreach (KeyValuePair <int, OnlineMapsMarkerBillboard> billboard in markerBillboards)
        {
            billboard.Value.used = false;
        }

        foreach (OnlineMapsMarker marker in map.markers)
        {
            //   Debug.Log("mảk");
            if (!marker.enabled || !marker.range.InRange(map.zoom))
            {
                continue;
            }
            //    Debug.Log("mảk enable");
            double mx, my;
            marker.GetPosition(out mx, out my);

            if (!(((mx > tlx && mx < brx) || (mx + 360 > tlx && mx + 360 < brx) ||
                   (mx - 360 > tlx && mx - 360 < brx)) &&
                  my < tly && my > bry))
            {
                continue;
            }

            int markerHashCode = marker.GetHashCode();
            OnlineMapsMarkerBillboard markerBillboard;

            if (!markerBillboards.ContainsKey(markerHashCode))
            {
                //  Debug.Log("mảk hashcode");
                markerBillboard = OnlineMapsMarkerBillboard.Create(marker);
                markerBillboard.transform.parent = markersGameObjects[0].transform;
                markerBillboard.gameObject.layer = markersGameObjects[0].layer;

                markerBillboards.Add(markerHashCode, markerBillboard);
            }
            else
            {
                markerBillboard = markerBillboards[markerHashCode];
            }

            if (markerBillboard == null)
            {
                continue;
            }

            float sx = size.x / map.width * marker2DSize * marker.scale;
            float sz = size.z / map.height * marker2DSize * marker.scale;
            float s  = Mathf.Max(sx, sz);

#if !UNITY_4_6 && !UNITY_4_7 && !UNITY_5_0 && !UNITY_5_1 && !UNITY_5_2
            markerBillboard.transform.localScale = new Vector3(s, s, s);
#else
            markerBillboard.transform.localScale = new Vector3(-s, s, s);
#endif

            double mpx, mpy;
            map.projection.CoordinatesToTile(mx, my, map.buffer.apiZoom, out mpx, out mpy);

            mpx  = OnlineMapsUtils.Repeat(mpx - px, 0, maxX);
            mpy -= py;

            float x = (float)(-mpx / map.width * OnlineMapsUtils.tileSize * size.x + positionOffset.x);
            float z = (float)(mpy / map.height * OnlineMapsUtils.tileSize * size.z - positionOffset.z);

            float y = GetElevationValue(x, z, yScale, tlx, tly, brx, bry);

            markerBillboard.transform.localPosition = transform.rotation * new Vector3(x, y, z);
            markerBillboard.used = true;
        }

        List <int> keysForRemove = new List <int>();

        foreach (KeyValuePair <int, OnlineMapsMarkerBillboard> billboard in markerBillboards)
        {
            if (!billboard.Value.used)
            {
                billboard.Value.Dispose();
                keysForRemove.Add(billboard.Key);
            }
        }

        foreach (int key in keysForRemove)
        {
            markerBillboards.Remove(key);
        }
    }
    private void OnDrawMarkers()
    {
        if (markersGameObjects == null)
        {
            InitMarkersMesh(0);
        }
        if (markerBillboards == null)
        {
            markerBillboards = new Dictionary <int, OnlineMapsMarkerBillboard>();
        }

        double tlx, tly, brx, bry;

        map.GetCorners(out tlx, out tly, out brx, out bry);
        if (brx < tlx)
        {
            brx += 360;
        }

        int maxX = 1 << map.buffer.renderState.zoom;

        double px, py;

        map.projection.CoordinatesToTile(tlx, tly, map.zoom, out px, out py);

        float yScale = OnlineMapsElevationManagerBase.GetBestElevationYScale(tlx, tly, brx, bry);

        Bounds  mapBounds      = control.cl.bounds;
        Vector3 positionOffset = control.transform.position - mapBounds.min;
        Vector3 size           = mapBounds.size;

        size = control.transform.rotation * size;
        if (!control.resultIsTexture)
        {
            positionOffset.x -= size.x;
        }

        float zoomCoof = map.buffer.renderState.zoomCoof;

        foreach (KeyValuePair <int, OnlineMapsMarkerBillboard> billboard in markerBillboards)
        {
            billboard.Value.used = false;
        }

        foreach (OnlineMapsMarker marker in OnlineMapsMarkerManager.instance)
        {
            if (!marker.enabled || !marker.range.InRange(map.zoom))
            {
                continue;
            }

            double mx, my;
            marker.GetPosition(out mx, out my);

            if (!((mx > tlx && mx < brx || mx + 360 > tlx && mx + 360 < brx ||
                   mx - 360 > tlx && mx - 360 < brx) &&
                  my < tly && my > bry))
            {
                continue;
            }

            int markerHashCode = marker.GetHashCode();
            OnlineMapsMarkerBillboard markerBillboard;

            if (!markerBillboards.ContainsKey(markerHashCode))
            {
                markerBillboard = OnlineMapsMarkerBillboard.Create(marker);
                markerBillboard.transform.parent = markersGameObjects[0].transform;
                markerBillboard.gameObject.layer = markersGameObjects[0].layer;

                markerBillboards.Add(markerHashCode, markerBillboard);
            }
            else
            {
                markerBillboard = markerBillboards[markerHashCode];
            }

            if (markerBillboard == null)
            {
                continue;
            }

            float sx = size.x / map.buffer.renderState.width * marker2DSize * marker.scale;
            float sz = size.z / map.buffer.renderState.height * marker2DSize * marker.scale;
            float s  = Mathf.Max(sx, sz);

#if UNITY_5_2
            markerBillboard.transform.localScale = new Vector3(-s, s, s);
#else
            markerBillboard.transform.localScale = new Vector3(s, s, s);
#endif

            double mpx, mpy;
            map.projection.CoordinatesToTile(mx, my, map.buffer.renderState.zoom, out mpx, out mpy);

            mpx  = OnlineMapsUtils.Repeat(mpx - px, 0, maxX);
            mpy -= py;

            float x = (float)(-mpx / map.width * OnlineMapsUtils.tileSize * size.x / zoomCoof + positionOffset.x);
            float z = (float)(mpy / map.height * OnlineMapsUtils.tileSize * size.z / zoomCoof - positionOffset.z);

            float y = OnlineMapsElevationManagerBase.instance != null?OnlineMapsElevationManagerBase.GetElevation(x, z, yScale, tlx, tly, brx, bry) : 0;

            markerBillboard.transform.localPosition = control.transform.rotation * new Vector3(x, y, z);
            markerBillboard.used = true;
        }

        List <int> keysForRemove = new List <int>();

        foreach (KeyValuePair <int, OnlineMapsMarkerBillboard> billboard in markerBillboards)
        {
            if (!billboard.Value.used)
            {
                billboard.Value.Dispose();
                keysForRemove.Add(billboard.Key);
            }
        }

        foreach (int key in keysForRemove)
        {
            markerBillboards.Remove(key);
        }
    }