<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Infinity Code Forum — Best practice geofencing]]></title>
	<link rel="self" href="https://forum.infinity-code.com/extern.php?action=feed&amp;tid=1575&amp;type=atom" />
	<updated>2020-08-06T16:40:41Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.infinity-code.com/viewtopic.php?id=1575</id>
		<entry>
			<title type="html"><![CDATA[Re: Best practice geofencing]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=6534#p6534" />
			<content type="html"><![CDATA[<p>Hello.</p><p>Three points about CalculcateDistance method:</p><p>1. DistanceBetweenPoints is a very slow method and it is best to minimize its use. <br />To do this, first calculate the distance in degrees between locations. <br />If the distance in degrees is less than 12 arc minutes (for example, 1/5 degree, ~360 meters) then use DistanceBetweenPoints to calculate the distance in kilometers.</p><p>2. For a correct result, it doesn&#039;t matter whether you calculate the distance in meters or kilometers.<br />If you calculate the distance in kilometers, you will save one multiplication per marker.</p><p>3. By using sqrMagnitude instead of magnitude, you will save Math.Sqrt per marker.</p>]]></content>
			<author>
				<name><![CDATA[Alex Vertax]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=2</uri>
			</author>
			<updated>2020-08-06T16:40:41Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=6534#p6534</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Best practice geofencing]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=6533#p6533" />
			<content type="html"><![CDATA[<p>First working example with the same mindset as what I did with Mapbox:</p><div class="codebox"><pre><code>using UnityEngine;

public class UnlockingManager : MonoBehaviour
{
    [Header(&quot;Variables&quot;)]
    [Tooltip(&quot;Distance needed to unlock points in meters.&quot;)]
    public float distanceNeeded = 25f;

    private OnlineMapsLocationService locationService;
    private CreateMarkers markers;

    private void Start()
    {
        locationService = OnlineMapsLocationService.instance;
        markers = FindObjectOfType&lt;CreateMarkers&gt;();
        locationService.OnLocationChanged += CalculcateDistance;
    }

    private void CalculcateDistance(Vector2 location)
    {
        string closest_location = string.Empty;
        float closest_distance = 9999;
        foreach (OnlineMapsMarker3D marker in markers.spawned_objects)
        {
            float distance = OnlineMapsUtils.DistanceBetweenPoints(location, marker.position).magnitude * 1000;
            if(distance &lt; closest_distance)
            {
                closest_distance = distance;
                closest_location = marker.label;
            }
            if (distanceNeeded &gt; distance) Debug.Log($&quot;{marker.label} unlocked!&quot;);
        }
        Debug.Log($&quot;Closest location is {closest_location}, {closest_distance} meters away!&quot;);
    }
}</code></pre></div><p>I&#039;m pretty sure this isn&#039;t best practice, since it might take a lot of performance to do these calculations (I haven&#039;t tested it yet, let alone test it with a large set (40+ 3d markers)), but for now it works.<br />Still, if there is some kind of best practice I should be looking into I&#039;d love to know!</p>]]></content>
			<author>
				<name><![CDATA[jarne]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=1355</uri>
			</author>
			<updated>2020-08-06T12:42:09Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=6533#p6533</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Best practice geofencing]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=6532#p6532" />
			<content type="html"><![CDATA[<p>I&#039;m wondering what the best practice for geofencing would be.<br />What I want is for when my user to walk a route with 3d markers scattered on it and entering an X radius (10-200m) of said markers that they &quot;unlock&quot; it.</p><p>I&#039;ve seen some geofencing examples and examples with BoxCollider, but I&#039;m wondering what the best practice is for this kind of behaviour? And if there is an example that I can look at to get an idea of what to do.</p><p>I used to do this in Mapbox by calculating the distance between geo location of player and closest marker object in the scene. I&#039;ve tried doing the same in my current project with Online Maps, but it seems that I&#039;m doing something wrong with the OnlineMapsUtils.DistanceBetweenPoints (I keep getting 0 as the distance between two points, I think it&#039;s because they get transferred into doubles and lose the rest of the decimals, which makes the lat/lng &quot;identical&quot;).</p><p>Any help would be appreciated!</p><p>I&#039;m using 3d markers and the LocationServices player marker if that&#039;s important to know.</p><p>p.s. so far I&#039;m loving this plugin, a lot of useful features and examples that I can chop into custom things to use.</p><p>Edit:<br />I&#039;ve found this: <a href="http://infinity-code.com/atlas/online-maps/pages/DistanceAndDirectionExample.html">http://infinity-code.com/atlas/online-m … ample.html</a><br />Which I can rewrite to do what I want to achieve, but it this the best practice?</p>]]></content>
			<author>
				<name><![CDATA[jarne]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=1355</uri>
			</author>
			<updated>2020-08-06T11:28:31Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=6532#p6532</id>
		</entry>
</feed>
