<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Infinity Code Forum — Draw polyline with Long/la coordinates without Google Api]]></title>
	<link rel="self" href="https://forum.infinity-code.com/extern.php?action=feed&amp;tid=2128&amp;type=atom" />
	<updated>2023-06-16T13:24:04Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.infinity-code.com/viewtopic.php?id=2128</id>
		<entry>
			<title type="html"><![CDATA[Re: Draw polyline with Long/la coordinates without Google Api]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=9075#p9075" />
			<content type="html"><![CDATA[<p>You want to set up a view on the map that shows all your points, right?!<br />Use OnlineMapsUtils.GetCenterPointAndZoom for these to get the center point and zoom for that.</p>]]></content>
			<author>
				<name><![CDATA[Alex Vertax]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=2</uri>
			</author>
			<updated>2023-06-16T13:24:04Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=9075#p9075</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Draw polyline with Long/la coordinates without Google Api]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=9073#p9073" />
			<content type="html"><![CDATA[<p>Thanks a lot for the support, I post the code, in case it can be useful to someone.</p><p>Now I want to ask if someone know how to make the automatic zoom of the map, based on the coordinates entered? </p><div class="codebox"><pre><code>using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Coordinate;
using System.Globalization;
public delegate void MyEventHandler();


public class CtrlCoordinate : MonoBehaviour
{
    // delegate per richiamare un evento, una volta terminato di caricare la lista di punti

    public event MyEventHandler OnEventTriggered;

    string coordinateString = &quot;(8.93266215617905 44.40604327943526, 8.932524131901832 44.40601228591227, 8.932359430658943 44.405996835286025, 8.93207661034279 44.40596474551083)&quot;;
    public List&lt;CoordinateStruct&gt; coordinateList = new List&lt;CoordinateStruct&gt;();
    public List&lt;GameObject&gt; PuntiList = new List&lt;GameObject&gt;();
    public List&lt;Texture&gt; cubeTextures = new List&lt;Texture&gt;();
    private static float scale = 10750000f;
    public GameObject referenceObject;

    public GameObject markerPrefab;
    public Transform markerParent;

    OnlineMapsMarkerManager onlineMaps;

    // Start is called before the first frame update
    void Start()
    {
        onlineMaps = GameObject.Find(&quot;Map&quot;).GetComponent&lt;OnlineMapsMarkerManager&gt;();
        // All of this stuff is used to convert the string to float and serialize the class Coordinate for eac value from the array!
        string modifiedCoordinaateString = coordinateString.Replace(&quot;(&quot;, &quot;&quot;).Replace(&quot;)&quot;, &quot;&quot;); 
        string[] coordinateArray = modifiedCoordinaateString.Split(new string[] { &quot;, &quot; }, StringSplitOptions.None);
        foreach (string coordinatePair in coordinateArray)
        {
            string[] coordinateValues = coordinatePair.Split(&#039; &#039;);
            decimal decimLongitude = decimal.Parse(coordinateValues[0], CultureInfo.InvariantCulture);
            decimal decimLatitude = decimal.Parse(coordinateValues[1], CultureInfo.InvariantCulture);
            float longitude = (float)decimal.Round(decimLongitude, 6);
            float latitude = (float)decimal.Round(decimLatitude, 6);
            CoordinateStruct coordinate = new CoordinateStruct(latitude, longitude);
            coordinateList.Add(coordinate);
            //Debug.Log(coordinate.latitude);
            
        }
        InstantiatePoints();
    }

    // use this method to instanciate the gameobjects!
    void InstantiatePoints()
    {
        float distanzaTotale=0;
        float distanzaTotaleGeografica = 0;
        for (int i = 0; i &lt; coordinateList.Count; i++)
        {
            CoordinateStruct coordinate = coordinateList[i];
            GameObject pointObject = Instantiate(referenceObject);
            Vector2 coordinateAttuali = new Vector2(coordinate.longitude, coordinate.latitude);
            //Vector3 position = FromLongLat(coordinateAttuali);
            Vector3 position = AlbersProjection(coordinate.longitude, coordinate.latitude);
            pointObject.transform.position = position;
            PuntiList.Add(pointObject);
            if (i &gt; 0)
            {
                float geographicDistance = CalculateGeographicDistance(coordinateList[i], coordinateList[i-1]);
                float distance = Vector3.Distance(PuntiList[i].transform.position, PuntiList[i-1].transform.position);
                Debug.Log($&quot;distanza geografica = {geographicDistance}&quot;);
                Debug.Log($&quot;distnza vettori = {distance}&quot;);
                distanzaTotale += distance;
                distanzaTotaleGeografica += geographicDistance;
            }
            
        }
        OnEventTriggered.Invoke();
        Debug.Log(distanzaTotale);
        Debug.Log(distanzaTotaleGeografica);
        List&lt;Vector2&gt; line = new List&lt;Vector2&gt;();
            for(int i = 0; i&lt;= coordinateList.Count-1; i++)
        {
            Vector2 vector2 = new Vector2(coordinateList[i].longitude, coordinateList[i].latitude);
            line.Add(vector2);
        }
        OnlineMapsDrawingElementManager.AddItem(new OnlineMapsDrawingLine(line, Color.magenta, 5));
        InstantiateMarkers(coordinateList);

    }

    // creiamo i marker sulla mappa in base alla mia lista di punti 

    public void InstantiateMarkers(List&lt;CoordinateStruct&gt; coordinates)
    {

        foreach (CoordinateStruct coordinate in coordinates)
        {
            //GameObject markerObject = Instantiate(OnlineMapsMarkerManager. markerPrefab, markerParent);;
            OnlineMapsMarkerManager.CreateItem(coordinate.longitude, coordinate.latitude, onlineMaps.defaultTexture);
        }
    }

    public static Vector3 AlbersProjection(float latitude, float longitude)
    {
        // Coordinate di riferimento per Genova
        float centralMeridian = 8.9463f; // Longitudine di riferimento
        float standardParallel1 = 44f; // Primo parallelo standard
        float standardParallel2 = 45f; // Secondo parallelo standard

        // Conversione da gradi a radianti
        float latRad = Mathf.Deg2Rad * latitude;
        float longRad = Mathf.Deg2Rad * longitude;
        float centralMeridianRad = Mathf.Deg2Rad * centralMeridian;
        float stdParallel1Rad = Mathf.Deg2Rad * standardParallel1;
        float stdParallel2Rad = Mathf.Deg2Rad * standardParallel2;

        // Parametri per la proiezione di Albers
        float n = (Mathf.Sin(stdParallel1Rad) + Mathf.Sin(stdParallel2Rad)) / 2f;
        float c = Mathf.Pow(Mathf.Cos(stdParallel1Rad), 2f) + 2f * n * Mathf.Sin(stdParallel1Rad);
        float rho0 = Mathf.Sqrt(c - 2f * n * Mathf.Sin(stdParallel1Rad)) / n;

        // Calcolo della proiezione di Albers
        float rho = Mathf.Sqrt(c - 2f * n * Mathf.Sin(latRad)) / n;
        float theta = n * (longRad - centralMeridianRad);

        float x = scale * (rho * Mathf.Sin(theta));
        float z = scale * (rho0 - rho * Mathf.Cos(theta));

        return new Vector3(ScaleCoordinates(x)/2, 0, ScaleCoordinates(z)/2);
    }
    public static Vector3 GPSToMapCoords(float latitude, float longitude)
    {
        float radius = 6371000f; // raggio della Terra in metri
        float longitude0 = 0f; // longitudine di riferimento

        float latRad = Mathf.Deg2Rad * latitude;
        float longRad = Mathf.Deg2Rad * longitude;

        float x = scale*(radius * (longRad - Mathf.Deg2Rad * longitude0));
        float y = 0;
        float z = scale*(radius * Mathf.Log(Mathf.Tan(Mathf.PI / 4f + latRad / 2f)));

        return new Vector3(ScaleCoordinates(x), y, ScaleCoordinates(z));
    }


    //use this method to reduce the size of the number cause u really dont need all the firs part of the number, and smaller one can readed easily from unity transform!
    public static float ScaleCoordinates(float num)
    {
        // Converti il numero in una stringa
        string numString = num.ToString();

        // Rimuovi le prime quattro cifre decimali
        string scaledString = numString.Substring(2);

        // Converti la stringa risultante in float
        float scaledNum = float.Parse(scaledString);

        // Restituisce il numero con le cifre decimali ridotte
        return scaledNum;
    }

    public static float CalculateGeographicDistance(CoordinateStruct coordinate1, CoordinateStruct coordinate2)
    {
        float earthRadius = 6371000f;

        float lat1Rad = Mathf.Deg2Rad * coordinate1.latitude;
        float lat2Rad = Mathf.Deg2Rad * coordinate2.latitude;
        float lon1Rad = Mathf.Deg2Rad * coordinate1.longitude;
        float lon2Rad = Mathf.Deg2Rad * coordinate2.longitude;

        float deltaLat = lat2Rad - lat1Rad;
        float deltaLon = lon2Rad - lon1Rad;

        float a = Mathf.Sin(deltaLat / 2) * Mathf.Sin(deltaLat / 2) + Mathf.Cos(lat1Rad) * Mathf.Cos(lat2Rad) * Mathf.Sin(deltaLon / 2) * Mathf.Sin(deltaLon / 2);
        float c = 2 * Mathf.Atan2(Mathf.Sqrt(a), Mathf.Sqrt(1 - a));
        float distance = earthRadius * c;

        return distance;
    }

    
}</code></pre></div>]]></content>
			<author>
				<name><![CDATA[andrea88.cristina]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=2614</uri>
			</author>
			<updated>2023-06-16T10:18:37Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=9073#p9073</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Draw polyline with Long/la coordinates without Google Api]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=9071#p9071" />
			<content type="html"><![CDATA[<p>Really Thanks! It was really helpfull. </p><p>I managed to draw the straight line with my coordinates!<br />Tommorow i will try to create the marker with my Vector2List!</p>]]></content>
			<author>
				<name><![CDATA[andrea88.cristina]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=2614</uri>
			</author>
			<updated>2023-06-15T15:59:13Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=9071#p9071</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Draw polyline with Long/la coordinates without Google Api]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=9070#p9070" />
			<content type="html"><![CDATA[<p>No. These:<br /><a href="https://infinity-code.com/atlas/online-maps/pages/DrawingAPI_Example.html">https://infinity-code.com/atlas/online- … ample.html</a><br /><a href="https://infinity-code.com/atlas/online-maps/pages/MarkerClickExample.html">https://infinity-code.com/atlas/online- … ample.html</a></p>]]></content>
			<author>
				<name><![CDATA[Alex Vertax]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=2</uri>
			</author>
			<updated>2023-06-15T14:10:57Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=9070#p9070</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Draw polyline with Long/la coordinates without Google Api]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=9069#p9069" />
			<content type="html"><![CDATA[<p>i mean this script here ?</p><p><a href="https://infinity-code.com/atlas/online-maps/pages/FindDirectionExample.html">https://infinity-code.com/atlas/online- … ample.html</a></p>]]></content>
			<author>
				<name><![CDATA[andrea88.cristina]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=2614</uri>
			</author>
			<updated>2023-06-15T13:55:45Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=9069#p9069</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Draw polyline with Long/la coordinates without Google Api]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=9068#p9068" />
			<content type="html"><![CDATA[<p>Hi.</p><p>Yes, markers and Drawing API are built-in features of Online Maps and do not depend on the provider used.<br />You can find examples of use in the atlas of examples:<br /><a href="https://infinity-code.com/atlas/online-maps/index.html">https://infinity-code.com/atlas/online-maps/index.html</a></p>]]></content>
			<author>
				<name><![CDATA[Alex Vertax]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=2</uri>
			</author>
			<updated>2023-06-15T10:35:54Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=9068#p9068</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Draw polyline with Long/la coordinates without Google Api]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=9066#p9066" />
			<content type="html"><![CDATA[<p>Hello everyone, I&#039;m new to the forum and I&#039;m already ready with the first of a long series of questions XD.<br />I state that I have just started studying the package, but I&#039;m not understanding much.</p><p>I just bought the Online maps package and I will have to work with a series of coordinates (latitude and longitude) that will be returned to me by reading a json.</p><p>I was wondering if there is a way to instantiate markers based on coordinates and if there was a way to draw a polyline based on coordinates, but without using the google API, perhaps using the Open Street Maps ones</p><p>Thank you all!!!</p>]]></content>
			<author>
				<name><![CDATA[andrea88.cristina]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=2614</uri>
			</author>
			<updated>2023-06-15T09:11:55Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=9066#p9066</id>
		</entry>
</feed>
