'Display current GPS Location in unity with Bing Maps SDK
how do i get the gps data displayed on the map? im trying it with the following script but i dont have acess to the latlon values in the mappin script.... i am struggling to access the latlon wrapper so i can change the coordinates. as i am new to unity and c# i dont get how to dynamically change the values. if i copy the script and change the variables it wont get recognized by the sdk and it doesnt do the work i want... getting the gps data worked so far, as you see in the code. i got the gps data and display it in the game as a text. now i just want to give the mappin.cs my gps data so it can place it in the map. normally i would just add the mappin script to a gameobject, which has to be child of the map and then put in the longtitude and latitude, but as the gps data is not something i can now i dont know how to get i done.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Android;
using UnityEngine;
using System.Collections;
using Microsoft.Geospatial;
using Microsoft.Maps.Unity;
public class GPS : MonoBehaviour
{
public Text gpsOut;
public bool isUpdating;
private double latitude;
private double longtitude;
private void Update()
{
if (!isUpdating)
{
StartCoroutine(GetLocation());
isUpdating = !isUpdating;
}
Debug.Log(longtitude + " " + latitude);
LatLonWrapper position = new LatLonWrapper();
position.Latitude = latitude;
position.Longitude = longtitude;
pin._location = longtitude + " " + latitude);
pin.Location = position.ToLatLon();
decalRightSidePos.GetComponent<Decal>().Sprite = decalprefab[index];
}
IEnumerator GetLocation()
{
if (!Permission.HasUserAuthorizedPermission(Permission.FineLocation))
{
Permission.RequestUserPermission(Permission.FineLocation);
Permission.RequestUserPermission(Permission.CoarseLocation);
}
// Check if the user has location service enabled.
if (!Input.location.isEnabledByUser)
yield return new WaitForSeconds(5);
// Starts the location service.
Input.location.Start();
// Waits until the location service initializes
int maxWait = 20;
while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
{
yield return new WaitForSeconds(1);
maxWait--;
}
// If the service didn't initialize in 20 seconds this cancels location service use.
if (maxWait < 1)
{
gpsOut.text = "Timed out";
print("Timed out");
yield break;
}
// If the connection failed this cancels location service use.
if (Input.location.status == LocationServiceStatus.Failed)
{
gpsOut.text = "Unable to determine device location";
print("Unable to determine device location");
yield break;
}
else
{
gpsOut.text = "Location" + Input.location.lastData.latitude + " " + Input.location.lastData.longitude;
// If the connection succeeded, this retrieves the device's current location and displays it in the Console window.
print("Location: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude + " " + Input.location.lastData.altitude + " " + Input.location.lastData.horizontalAccuracy + " " + Input.location.lastData.timestamp);
latitude = Input.location.lastData.latitude;
longtitude = Input.location.lastData.longitude;
}
// Stops the location service if there is no need to query location updates continuously.
isUpdating = !isUpdating;
Input.location.Stop();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
