I’m currently having a huge problem trying to plot longitude and latitude points to a map on screen.
Basically I’m grabbing a map from OpenStreetMap of the devices location and I’ll be adding objects around the player on the map for them to collect and interact with. I am currently having a problem being able to plot longitude and latitude positions onto the onscreen map. I have played around with a few different formulas and the closest I have got it is that the first dot gets plotted to the devices location (the center of the screen every map update) the second dot should appear on the map on a field behind my house but instead appears a few pixels away from the center. Here are the important bits of code:
Update method:
void Update()
{
if (canUpdate)
{
float lastLon = lon;
float lastLat = lat;
float lastZoom = zoom;
lon = GpsLocation.lon;
lat = GpsLocation.lat;
zoom = GpsLocation.zoom;
if (lastLon != lon || lastLat != lat || lastZoom != zoom)
StartCoroutine("LoadMap");
}
}
Load map method:
IEnumerator LoadMap()
{
canUpdate = false;
GpsLocation.status = "Begining new map download";
lon2x(lon);
string url = System.String.Format("http://staticmap.openstreetmap.de/staticmap.php?center={0},{1}&zoom={2}&size={3}x{4}", lat, lon, zoom, Screen.width, Screen.height);
WWW mapSite = new WWW(url);
yield return mapSite;
GpsLocation.status = "Map downloaded, waiting for update delay";
tex = mapSite.texture;
yield return new WaitForSeconds(1f);
GpsLocation.status = "Map downloaded, a new update cycle is ready";
canUpdate = true;
}
Draw code:
void OnGUI()
{
GUI.depth = 2;
if (tex)
GUI.DrawTexture(new Rect(Screen.width/2 - tex.width/2, Screen.height/2 - tex.height/2, tex.width, tex.height), tex);
Debug.Log(lat2y(lat));
GUI.DrawTexture(new Rect((lon2x(lon) + Screen.width/2) - dot.width / 2, (lat2y(lat) + Screen.height/2) - dot.height / 2, dot.width, dot.height), dot);
GUI.DrawTexture(new Rect((lon2x(-1.9117015600204468f) + Screen.width/2) - dot.width / 2, (lat2y(52.31723170064912f) + Screen.height/2) - dot.height / 2, dot.width, dot.height), dot);
}
And last but not least, the lon/lat to x/y methods:
float lon2x(float longitude)
{
osx = (longitude+180)/360*Mathf.Pow(2,zoom);
return (((longitude+180)/360*Mathf.Pow(2,zoom))-osx) * Screen.width;
//return (Mathf.FloorToInt((Screen.width/360) * (180 + lon) * Mathf.Pow(2, zoom)));
}
float lat2y(float latitude)
{
osy = (1-Mathf.Log(Mathf.Tan(latitude*Mathf.PI/180) + 1/Mathf.Cos(latitude*Mathf.PI/180))/Mathf.PI)/2 *Mathf.Pow(2,zoom);
return (((1 - Mathf.Log(Mathf.Tan(latitude * Mathf.PI / 180) + 1 / Mathf.Cos(latitude * Mathf.PI / 180)) / Mathf.PI) / 2 * Mathf.Pow(2, zoom)) - osy) * Screen.height;
}
Any help on this would be really appreciated.