- Home /
Connect Longitude and Latitude with Map
Hi all,
I am currently working on a small project. My goal is to show your location on a map. I use mapbox and a script that gives you the longitude and latitude of your device's location. For the map am I using Mapbox. Mapbox just released a Unity SDK that I have installed, and i added my own mapstyle to it. You can adjust the longitude and the latitude of the map manual, but what i'm trying to do is connecting my GPS script (the script that gives the longitude and latitude of your device's location) with Mapbox. So when you start the app the GPS script will send the longitude and the latitude to the Mapbox script and the map should be automatically changed the location to the location of your device, just like any other gps map.
I hope somebody could help me to solve this problem.
Thanks!
Script GPS:
public Text latitude;
public Text longitude;
public Text altitude;
public Text horizontalAcccuracy;
public Text verticalAcccuracy;
public Text lastUpdated;
public Text status;
public Text isEnabled;
private string desiredAccuracy = "0.01";
private string updateDistance = "0.01";
private float screenWidth = 0;
private float screenHeight = 0;
private float buttonWidthPart = 0.7f;
private float screenHalfWidth
{
get
{
return screenWidth / 2;
}
}
private float screenHalfHeight
{
get
{
return screenHeight / 2;
}
}
// Use this for initialization
void Start()
{
if (longitude == null)
Debug.Log("longitude is null");
screenHeight = Screen.height;
screenWidth = Screen.width;
}
// Update is called once per frame
void Update()
{
}
void OnGUI()
{
if (GUI.Button(new Rect(screenHalfWidth + (screenHalfWidth * (1 - buttonWidthPart)),
0,
screenHalfWidth * buttonWidthPart,
screenHalfHeight),
"Start"))
{
Input.location.Start(float.Parse(desiredAccuracy),
float.Parse(updateDistance));
}
if (GUI.Button(new Rect(screenHalfWidth + (screenHalfWidth * (1 - buttonWidthPart)),
screenHalfHeight,
screenHalfWidth * buttonWidthPart,
screenHalfHeight),
"Stop"))
{
Input.location.Stop();
}
LocationInfo location = Input.location.lastData;
longitude.text = location.longitude.ToString();
latitude.text = location.latitude.ToString();
}
Script Mapbox
[SerializeField]
ForwardGeocodeUserInput _geocodeInput;
[SerializeField]
GameObject _tilePrefab;
[SerializeField]
int _mapWidth;
[SerializeField]
int _mapHeight;
[SerializeField]
float _tileSize;
[SerializeField]
[Range(1, 19)]
int _zoom;
[SerializeField]
float _latitude;
[SerializeField]
float _longitude;
Tile.Parameters _parameters;
UnwrappedTileId _unwrappedTileId;
List<GameObject> _instantiatedTiles = new List<GameObject>();
public Text latitude;
void Awake()
{
_geocodeInput.OnGeocoderResponse += GeocodeInput_OnGeocoderResponse;
}
void OnDestroy()
{
if (_geocodeInput != null)
{
_geocodeInput.OnGeocoderResponse -= GeocodeInput_OnGeocoderResponse;
}
}
void Start()
{
MapboxConvenience.Instance.TileScale = _tileSize;
Build();
}
void Build()
{
foreach (var tile in _instantiatedTiles)
{
Destroy(tile);
}
_instantiatedTiles.Clear();
_unwrappedTileId = MapboxConvenience.LatitudeLongitudeToTileId(_latitude, _longitude, _zoom);
_parameters = new Tile.Parameters
{
Fs = MapboxConvenience.Instance.FileSource,
Id = new CanonicalTileId(_zoom, _unwrappedTileId.X, _unwrappedTileId.Y)
};
Initialize(_parameters);
}
protected override void OnInitialized(Tile.Parameters parameters)
{
for (int i = 0; i < _mapWidth; i++)
{
for (int j = 0; j < _mapHeight; j++)
{
var instance = Instantiate<GameObject>(_tilePrefab);
_instantiatedTiles.Add(instance);
instance.transform.SetParent(transform, false);
instance.transform.localPosition = new Vector3(_tileSize * i, 0, _tileSize * j);
// Unity plane UVs are "backwards."
var tiles = instance.GetInterfaces<ITile>();
foreach (var tile in tiles)
{
parameters.Id = new CanonicalTileId(_zoom, _unwrappedTileId.X - i, _unwrappedTileId.Y + j);
tile.Initialize(parameters);
}
}
}
}
void GeocodeInput_OnGeocoderResponse(object sender, System.EventArgs e)
{
_latitude = (float)_geocodeInput.Coordinate.Latitude;
_longitude = (float)_geocodeInput.Coordinate.Longitude;
Build();
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612111338im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
GPS Locate.permission Android? 0 Answers
MapBox - Javascript API work in unity? 1 Answer
Assigning UV Map to model at runtime 0 Answers
Mapbox AR : Set map location in code 0 Answers
GPS Position im my custom 3d map 1 Answer