Connecting our Information and Working with Maps
Now that we have gotten our scripts set up, let’s start getting interaction between them all so that we can have the information that we want shared between the scripts, along with starting our work on implementing maps.
First, let’s get the logic to randomly create a case number:
What we have done is within our UIManager, we have set the logic to create our case number at random from here. We will then have it update within our Case class as well. Finally, we are going to have our create case and border panels be activated from here instead, which will allow us to get rid of the OnClick logic with the button for each panel from Unity:
In order for our case number to be updated on the panel, we will have the information set up upon enabling the screen, and pull the information from the UIManager:
Next, let’s change up our switching from the initial name panel to the the location. As of now, we are able to just transition from the create to location panel whenever we want. Instead, we are going to remove that ability and have it only occur when the name fields have been filled out:
What we have done is told Unity that if either of the fields are empty, we want the error to pop up, which in the future we can change to a red notice on the actual panel to let the user know something needs to be filled out. If they are both filled our however, we will have that information populate in the UIManager, along with activating our location panel:
As we can see, once we have entered in all the required information, it will allow us to proceed. As well, on the canvas with the active case, the name information is updated there upon switching our panels.
To get our maps working, we need to go over to google maps and gain access to static map API:
Once we have created an account for it, we can then move onto the next step and get our API key. Once we have created it, we can look at the example given for constructing our URL and start to build it ourselves within our script:
From here, we are going to go into our location panel and start building the URL we need:
As we can see, we need to create variables for the center of the map, using x and y coordinates, along with a zoom, size and attaching our API key. With this set up, we can now look into how we can get our static map downloaded to the panel:
Within the Unity scripting API, we can see how we can build the ability to pull the image from the URL:
As we are going to need to build our process as a coroutine, we will want to build our code within an IEnumerator. From here, we are going to tell unity to use the WWW(url) that we have created and update the raw image that we have with the new image that was pulled from the URL. Once we start the coroutine in the OnEnable, we can see if the image updates when we go to play:
Now that we have the map updated, we can move towards getting it so that we can get the GPS location from the phone:
What we have done here is changed our OnEnable method into an IEnumerator start method. This will allow us to properly build the ability for the user’s location to be grabbed, and gives us the map to look at. To start, we are telling Unity that we want to make sure that as long and location is enabled by the user, we can work to initialize the status, but if it takes longer than 20 seconds, we want to create a time out mechanic to let the user know it was unable to get the information. If the initialization fails, we want to create an error saying that it didn’t work, otherwise what will happen is the x/y cord’s become the latitude and longitude of our GPS location. Once all of this has run, we just want to stop using the coroutine and we are good to go.
Now, in order to finish off this panel, we need to make sure that our case number is properly updated along with being able to fill in our notes:
The case number part is easy, as it is the same process as the previous panel. As for the notes field, we can simply add into our process info field:
For this case, we don’t need to have the restriction to moving to the next panel, so all we are doing to creating the logic to transfer the notes written down, if any, to be added to our case file.