Friday, May 18, 2018

Setting Up SQL, PHP, and Sending/Loading Locations

After displaying a map the next thing to do was to let the user send their location and be able to store it. For this I used 000webhost a hosting site that provided me with a MySQL server and PHP scripting to manage it. Using the System.Net.Http Plugin in Xamarin I wrote code that would post the latitude, longitude, and place name in JSON to a php file located on my site. The file would then convert the JSON into variables, stick them into a sql call, and execute the sql against the MySQL database to store them inside the Location table. To do this I needed a way to get the users location on iOS and Android. This functionality was provided by the Xam.Plugin.Geolocator Plugin. Also I needed a way to show a popup asking for the name of the location. For this I used Acr.UserDialogs Plugin. So after putting everything together the user would be able to tap an add button which would poll Geolocator for their location, then type in the name of their location and tap Ok, which would send and store the data in the MySQL server.

Next I needed to be able to get the locations from the server and display them on the map as pins. The process works almost the same way but backwards. You pull the data from the sql database using php encode it in JSON and send it back. The only difference would be decoding the JSON on the other side. Using the plugin Newtonsoft.Json you could decode the JSON string, but what makes the plgin convenient is that you can decode the JSON straight into an Object that will hold the data. For example I created the Object LatLonPair that would hold latitude, logitude and name variables. So using Newtonsoft you could decode a json string into an array of LatLonPairs that hold all your data. From their displaying on the map is easy. just iterate over the array and place the pins on the map.

No comments:

Post a Comment