unity get current location code example
Example: Unity3d GPS code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GPSLocationTest : MonoBehaviour
{
public float Latitude;
public float Longitude;
public float Oldlat;
public float Oldlon;
public AudioSource kickSource;
void Start()
{
kickSource = GetComponent<AudioSource>();
Latitude = Input.location.lastData.latitude;
Longitude = Input.location.lastData.longitude;
int oldLat;
int oldLon;
}
IEnumerator Start()
{
if (!Input.location.isEnabledByUser)
yield break;
Input.location.Start();
int maxWait = 20;
while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
{
yield return new WaitForSeconds(1);
maxWait--;
}
if (maxWait < 1)
{
print("Timed out");
yield break;
}
if (Input.location.status == LocationServiceStatus.Failed)
{
print("Unable to determine device location");
yield break;
}
else
{
print("Location: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude + " " + Input.location.lastData.altitude + " " + Input.location.lastData.horizontalAccuracy + " " + Input.location.lastData.timestamp);
}
Input.location.Stop();
}
void Update()
{
if (Input.location.status == LocationServiceStatus.Running)
{
Latitude = Input.location.lastData.latitude;
Longitude = Input.location.lastData.longitude;
kickSource.Play.sound();
}
do
{
#code to detect movement up
lat=NewLat;
newLat++;
#code to detect movement backwards
lat=NewLat;
newLat--;
#code to detect movement left
lat=NewLon;
newLon++;
#code to detect movement right
lat=NewLon;
newLon--;
}
while(newLat > lat || newLat < lat || newLon > lon || newLon < lon)
}
}
}