Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Location Services in Unity3D

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 3.11k
    Comment on it

    Hello Guys,

     

    Today I am going to share a very interesting topic with you. We all use this in our normal daily life, But at that time non of us try to know how we can do this. How I will check this. How I come to know about this. 

    Yes, I am talking about the GPS system. GPS shows complete information about your location, area and all.

     

    Now in these days people have had started creating games with GPS. So I am sure in everyone's mind there will be a question, how I can do this? How can I implement GPS in my game or app?

    So here is the solution:

    We can now easily add GPS in our games or app bu following code packet. We can check the location very easily.

    using UnityEngine;
    using System.Collections;
    
    public class Sps : MonoBehaviour
    {
        IEnumerator Start()
        {
            // First, check if user has location service enabled
            if (!Input.location.isEnabledByUser)
                yield break;
    
            // Start service before querying location
            Input.location.Start();
    
            // Wait until service initializes
            int maxWait = 20;
            while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
            {
                yield return new WaitForSeconds(1);
                maxWait--;
            }
    
            // Service didn't initialize in 20 seconds
            if (maxWait < 1)
            {
                print("Timed out");
                yield break;
            }
    
            // Connection has failed
            if (Input.location.status == LocationServiceStatus.Failed)
            {
                print("Unable to determine device location");
                yield break;
            }
            else
            {
                // Access granted and location value could be retrieved
                print("Location: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude + " " + Input.location.lastData.altitude + " " + Input.location.lastData.horizontalAccuracy + " " + Input.location.lastData.timestamp);
            }
    
            // Stop service if there is no need to query location updates continuously
            Input.location.Stop();
        }
    }

     

    Try this and let me know if you have any query.

    Till then keep coding..

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: