- Home /
how to convert gps coordinates to unity
hello. first, my english skill is not good. sorry.. anyway i want to convert lat/lon to unity x,y coordinates. i already read some another questions but not solved..
i made c# script to get the gps data and it works well. i also made a object in the unity world, and made another c# script to control camera rotation with gyro. and!! last!! i just want to move my unity camera according to gps lat/lon data. so i convert my lat/lon data. like this page https://stackoverflow.com/questions/32311647/converting-gps-coordinates-to-x-y-z-coordinates but, my camera is not moved as i wanted. problem is this. lat, lon is correct, but converted data(x/z) is incorrect.
float latToZ (double lat){
lat = (lat - 53.178469) / 0.00001 * 0.12179047095976932582726898256213;
double z = lat;
return (float)z;
}
float lonToX (double lon){
lon = (lon - 6.503091) / 0.000001 * 0.00728553580298947812081345114627;
double x = lon;
return (float)x;
} i think in this function, the equation is little wrong.. please someone make me know how can i convert that.
thanks and have a good day
Answer by NorthStar79 · Aug 17, 2017 at 06:10 AM
if I understand StackOverflow answer correct, that means these " 6.503091 & 53.178469" has nothing to do with you, and you should calculate this numbers for yourself.
I suggest,
1- pin point your position on a "real map", and mark that position in "unity map",
2- make public variables for these numbers ( 6.503091 & 53.178469) and make a UI slider for changing them
3- Build and install, hit play, try to adjust your camera view with those UI sliders you made for 'these numbers'
4- once you satisfy for your position, note that sliders value.
5- Go back to unity and enter numbers that you note into the equations.
and don't forget to notify us about results.
I hope this will help you.
Thanks for your answer. right 6.503091 & 53.178469 these number is nothing to me, so i initialize that numbers to my gps coordinates. and also i need gps coordinates regularly, gps receive data in Update() function. i am wondering how he(in stackoverflows person) get or calculate this number 0.00728553580298947812081345114627.
i saw 1 meter is 1 unity unit from a website. so, i am trying below: 1. convert longitude and latitude to meter(1 latitude is 110979.309 meter like this) 2. move camera to new position
function is here first, check the gps data correct
public bool isGPSDataCorrect() { if (($$anonymous$$athf.Abs(lat - oldLat) > 0.001) | ($$anonymous$$athf.Abs(lon - oldLon) > 0.001) ) { return false; } else { //double x = (lon - oldLon) / 0.000001 0.00728553580298947812081345114627 40; //double z = (lat - oldLat) / 0.00001 0.12179047095976932582726898256213 40; double x = (lon - oldLon) 88907.949; double z = (lat - oldLat) 110979.309; moveX = (float)x; moveZ = (float)z; return true; } }
and then, move camera to new position private void Update () {
camPos.text = "x: " + cameraContainer.transform.position.x.ToString() +
"\ny: " + cameraContainer.transform.position.y.ToString() +
"\nz: " + cameraContainer.transform.position.z.ToString();
if (gyroSupported && startY == 0)
{
ResetGyroRotation();
}
transform.localRotation = gyro.attitude * rotFix;
if (PipeLine.Instance.isGPSDataCorrect())
{
cameraContainer.transform.position += new Vector3(PipeLine.Instance.moveX, 0f, -PipeLine.Instance.moveZ);
}
}
i just show you a part of my sources cause more complex. now, i am trying and i hope like these questions and answers will be helpful unity and me.. me.......meeee.... Thank you!!
i tried but failed 1. camera is moved but just little. i can see my camera position but it moved just little. 2. so i modify to * 10, but it also not good. it is like turn around lat and long.
Answer by maxima06 · Nov 14, 2019 at 10:18 PM
I need help with that. If you have solved the problem, can you write the solution here?
Your answer
![](https://koobas.hobune.stream/wayback/20220612132859im_/https://answers.unity.com/themes/thub/images/avi.jpg)