/* INFINITY CODE 2013-2017 */ /* http://www.infinity-code.com */ using UnityEngine; namespace InfinityCode.OnlineMapsExamples { /// /// Example of how to dynamically create a 3D marker in the GPS locations of user. /// [AddComponentMenu("Infinity Code/Online Maps/Examples (API Usage)/Marker3D_GPS_Example")] public class Marker3D_GPS_Example : MonoBehaviour { /// /// Prefab of 3D marker /// public GameObject prefab; public UIButton gpsButton; /// /// Move duration (sec) /// public float time = 3; /// /// Relative position (0-1) between from and to /// private float angle; /// /// Movement trigger /// private bool isMovement; private Vector2 fromPosition; private Vector2 toPosition; private OnlineMapsMarker3D locationMarker; private bool isGpsClicked = false; public void OnGpsClick() { // Gets the current 3D control. OnlineMapsControlBase3D control = OnlineMapsControlBase3D.instance; if (control == null) { Debug.LogError("You must use the 3D control (Texture or Tileset)."); return; } //Create a marker to show the current GPS coordinates. //Instead of "null", you can specify the texture desired marker. locationMarker = control.AddMarker3D(Vector2.zero, prefab); //Hide handle until the coordinates are not received. locationMarker.enabled = false; // Gets Location Service Component. OnlineMapsLocationService ls = OnlineMapsLocationService.instance; if (ls == null) { Debug.LogError( "Location Service not found.\nAdd Location Service Component (Component / Infinity Code / Online Maps / Plugins / Location Service)."); return; } if (!ls.IsLocationServiceRunning()) { gpsButton.defaultColor = GlobalGUI.ColorScheme.btnPressed; ls.StartLocationService(); } else { GlobalGUI.UpdateBtnColor(gpsButton.gameObject); ls.StopLocationService(); } // ls. //Subscribe to the GPS coordinates change ls.OnLocationChanged += OnLocationChanged; ls.OnCompassChanged += OnCompassChanged; //Subscribe to zoom change OnlineMaps.instance.OnChangeZoom += OnChangeZoom; } private void OnChangeZoom() { //Example of scaling object int zoom = OnlineMaps.instance.zoom; if (zoom >= 5 && zoom < 10) { float s = 10f / (2 << (zoom - 5)); Transform markerTransform = locationMarker.transform; if (markerTransform != null) markerTransform.localScale = new Vector3(s, s, s); // show marker locationMarker.enabled = true; } else { // Hide marker locationMarker.enabled = false; } } private void OnCompassChanged(float f) { //Set marker rotation Transform markerTransform = locationMarker.transform; if (markerTransform != null) markerTransform.rotation = Quaternion.Euler(0, f * 360, 0); } //This event occurs at each change of GPS coordinates private void OnLocationChanged(Vector2 position) { //Change the position of the marker to GPS coordinates locationMarker.position = position; MoveToMarker(); //If the marker is hidden, show it if (!locationMarker.enabled) locationMarker.enabled = true; } private void MoveToMarker() { // from current map position fromPosition = OnlineMaps.instance.position; // to GPS position; toPosition = OnlineMapsLocationService.instance.position; // calculates tile positions double fromTileX, fromTileY, toTileX, toTileY; OnlineMaps.instance.projection.CoordinatesToTile(fromPosition.x, fromPosition.y, OnlineMaps.instance.zoom, out fromTileX, out fromTileY); OnlineMaps.instance.projection.CoordinatesToTile(toPosition.x, toPosition.y, OnlineMaps.instance.zoom, out toTileX, out toTileY); // if tile offset < 4, then start smooth movement if (OnlineMapsUtils.Magnitude(fromTileX, fromTileY, toTileX, toTileY) < 4) { // set relative position 0 angle = 0; // start movement isMovement = true; } else // too far { OnlineMaps.instance.position = toPosition; } } private void Update() { // if not movement then return if (!isMovement) return; // update relative position angle += Time.deltaTime / time; if (angle > 1) { // stop movement isMovement = false; angle = 1; } // Set new position OnlineMaps.instance.position = Vector2.Lerp(fromPosition, toPosition, angle); } } }