Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by bezza010 · May 27, 2012 at 12:22 PM · c#androidmapgps

Problem converting longitude/latitude to screen positions.

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by joe_bar · Jun 08, 2012 at 07:51 PM

This link(Bing Maps) has helpful functions that can be translated to work with OSM. It contains helpful functions for converting Lat/Lon values to pixel values, pixel values to tiles, etc.

link text

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rs · Dec 16, 2015 at 10:13 AM 0
Share

Dead link.

avatar image tanoshimi Rs · Dec 16, 2015 at 01:19 PM 0
Share

https://msdn.microsoft.com/en-us/library/bb259689.aspx

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Ask for Location permission at the start of the App 2 Answers

Get updated values from gps as moving 5 Answers

How should I integrate Photon Network with my GPS application? (MapNav) 0 Answers

Get Android devices current country location 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges