<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Infinity Code Forum — Let Map contains all Drawing element]]></title>
	<link rel="self" href="https://forum.infinity-code.com/extern.php?action=feed&amp;tid=302&amp;type=atom" />
	<updated>2016-10-18T03:41:53Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.infinity-code.com/viewtopic.php?id=302</id>
		<entry>
			<title type="html"><![CDATA[Re: Let Map contains all Drawing element]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=1375#p1375" />
			<content type="html"><![CDATA[<p>Hi, Alex</p><p>It works!!!&nbsp; Thanks for your amazing fixed!</p>]]></content>
			<author>
				<name><![CDATA[kb9200290tw]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=201</uri>
			</author>
			<updated>2016-10-18T03:41:53Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=1375#p1375</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Let Map contains all Drawing element]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=1372#p1372" />
			<content type="html"><![CDATA[<p>Fixed.<br />Next update will contain this fix:<br /></p><div class="codebox"><pre><code>public static void GetCenterPointAndZoom(OnlineMapsMarkerBase[] markers, out Vector2 center, out int zoom)
{
    center = new Vector2();
    zoom = 3;
    if (markers == null || markers.Length == 0) return;

    OnlineMaps api = OnlineMaps.instance;
    OnlineMapsProjection projection = api.projection;

    double mx, my;
    markers[0].GetPosition(out mx, out my);
    double minX = mx;
    double minY = my;
    double maxX = mx;
    double maxY = my;

    for (int i = 1; i &lt; markers.Length; i++)
    {
        OnlineMapsMarkerBase marker = markers[i];
        marker.GetPosition(out mx, out my);

        double rx = mx - minX;
        if (rx &gt; 180) mx -= 360;
        else if (rx &lt; -180) mx += 360;

        if (mx &lt; minX) minX = mx;
        if (mx &gt; maxX) maxX = mx;

        if (my &lt; minY) minY = my;
        if (my &gt; maxY) maxY = my;
    }

    double sx = maxX - minX;
    double sy = maxY - minY;

    center = new Vector2((float)(sx / 2 + minX), (float)(sy / 2 + minY));

    if (center.x &lt; -180) center.x += 360;
    else if (center.x &gt; 180) center.x -= 360;

    int width = api.width;
    int height = api.height;

    float countX = width / (float)tileSize / 2;
    float countY = height / (float)tileSize / 2;

    bool useZoomMin = false;

    for (int z = 20; z &gt; 4; z--)
    {
        bool success = true;

        double bx, by;
        projection.CoordinatesToTile(center.x, center.y, z, out bx, out by);
        int _mx = 1 &lt;&lt; z;
        int hx = 1 &lt;&lt; (z - 1);

        foreach (OnlineMapsMarkerBase marker in markers)
        {
            marker.GetPosition(out mx, out my);

            double px, py;
            projection.CoordinatesToTile(mx, my, z, out px, out py);

            if (px - bx &lt; -hx) px += _mx;
            else if (px - bx &gt; hx) px -= _mx;

            px -= bx - countX;
            py -= by - countY;

            if (marker is OnlineMapsMarker)
            {
                useZoomMin = true;
                OnlineMapsMarker m = marker as OnlineMapsMarker;
                OnlineMapsVector2i ip = m.GetAlignedPosition(new OnlineMapsVector2i((int)(px * tileSize), (int)(py * tileSize)));
                if (ip.x &lt; 0 || ip.y &lt; 0 || ip.x + m.width &gt; width || ip.y + m.height &gt; height)
                {
                    success = false;
                    break;
                }
            }
            else if (marker is OnlineMapsMarker3D)
            {
                if (px &lt; 0 || py &lt; 0 || px &gt; width || py &gt; height)
                {
                    success = false;
                    break;
                }
            }
            else
            {
                throw new Exception(&quot;Wrong marker type&quot;);
            }
        }
        if (success)
        {
            zoom = z;
            if (useZoomMin) zoom -= 1;
            return;
        }
    }

    zoom = 3;
}

public static void GetCenterPointAndZoom(Vector2[] positions, out Vector2 center, out int zoom)
{
    center = new Vector2();
    zoom = 3;
    if (positions == null || positions.Length == 0) return;

    OnlineMaps api = OnlineMaps.instance;
    OnlineMapsProjection projection = api.projection;

    Vector2 p = positions[0];
    float minX = p.x;
    float minY = p.y;
    float maxX = p.x;
    float maxY = p.y;

    for (int i = 1; i &lt; positions.Length; i++)
    {
        p = positions[i];
        float px = p.x;

        float rx = px - minX;
        if (rx &gt; 180) px -= 360;
        else if (rx &lt; -180) px += 360;

        if (px &lt; minX) minX = px;
        if (px &gt; maxX) maxX = px;

        if (p.y &lt; minY) minY = p.y;
        if (p.y &gt; maxY) maxY = p.y;
    }

    float sx = maxX - minX;
    float sy = maxY - minY;

    center = new Vector2(sx / 2 + minX, sy / 2 + minY);

    if (center.x &lt; -180) center.x += 360;
    else if (center.x &gt; 180) center.x -= 360;

    int width = api.width;
    int height = api.height;

    float countX = width / (float)tileSize / 2;
    float countY = height / (float)tileSize / 2;

    for (int z = 20; z &gt; 4; z--)
    {
        bool success = true;

        int mx = 1 &lt;&lt; z;
        int hx = 1 &lt;&lt; (z - 1);
        double cx, cy;
        projection.CoordinatesToTile(center.x, center.y, z, out cx, out cy);

        foreach (Vector2 pos in positions)
        {
            double px, py;
            projection.CoordinatesToTile(pos.x, pos.y, z, out px, out py);

            if (px - cx &lt; -hx) px += mx;
            else if (px - cx &gt; hx) px -= mx;

            px -= cx - countX;
            py -= cy - countY;

            if (px &lt; 0 || py &lt; 0 || px &gt; width || py &gt; height)
            {
                success = false;
                break;
            }
        }
        if (success)
        {
            zoom = z;
            return;
        }
    }

    zoom = 3;
}</code></pre></div>]]></content>
			<author>
				<name><![CDATA[Alex Vertax]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=2</uri>
			</author>
			<updated>2016-10-17T19:18:17Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=1372#p1372</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Let Map contains all Drawing element]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=1371#p1371" />
			<content type="html"><![CDATA[<p>Hello , </p><p>There are some weird behavior. <br />If my PolyLine cross the International Date Line(180 lat), the center will be place at (0 lat).</p><p>I am looking forward to some solution to solve this, thanks again!</p>]]></content>
			<author>
				<name><![CDATA[kb9200290tw]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=201</uri>
			</author>
			<updated>2016-10-17T17:08:50Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=1371#p1371</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Let Map contains all Drawing element]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=1370#p1370" />
			<content type="html"><![CDATA[<p>Wow!!&nbsp; That&#039;s something I need!!<br />Thanks you, Alex!</p>]]></content>
			<author>
				<name><![CDATA[kb9200290tw]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=201</uri>
			</author>
			<updated>2016-10-17T17:01:41Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=1370#p1370</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Let Map contains all Drawing element]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=1367#p1367" />
			<content type="html"><![CDATA[<p>Hello.</p><p>OnlineMapsUtils.GetCenterPointAndZoom<br /><a href="http://infinity-code.com/doxygen/online-maps/classOnlineMapsUtils.html#a13f0d279fa7fcd3c632aa2cc18081dc8">http://infinity-code.com/doxygen/online … cc18081dc8</a></p>]]></content>
			<author>
				<name><![CDATA[Alex Vertax]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=2</uri>
			</author>
			<updated>2016-10-17T14:03:23Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=1367#p1367</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Let Map contains all Drawing element]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=1365#p1365" />
			<content type="html"><![CDATA[<p>Hi, I am working on adding some lines on OnlineMapsTileset.&nbsp; <br />This time I want to see the whole drawing element( ie. PolyLine, ... ) that Map focus.<br />Like Google Maps Api V3 &#039;s fitbounds(), but somehow OnlineMaps.instance.positionRange not do that tricks.</p><p>As a result, I run through all points in PolyLine and generate a Bounding Box ( bound by lat, lng ).<br />If I get the best fit zoom, I will solve this problem as I can setPositionAndZoom by center of BoundingBox and b-f zoom.</p><p>So, the question is, if there is a solution to let map focus on specific area(latlng bound)?<br />if not, how can I get best fit zoom by bounding box?</p><p>thanks a lot!!!!</p>]]></content>
			<author>
				<name><![CDATA[kb9200290tw]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=201</uri>
			</author>
			<updated>2016-10-17T13:58:03Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=1365#p1365</id>
		</entry>
</feed>
