Download Code

Xamarin MYSQL Coordinates Map Android

In this tutorial you will learn how to retrieve location coordinates from mysql database.Plot on Google Map using xamarin mysql coordinates map android application.You will use JSON Parser and HTTP REQUEST Components to get the coordinates.

You must go through these tutorials before starting this tutorial.

Server End

  • Start XAMPP Server and go to “localhost/phpmyAdmin”.
  • Create new database “codeclogin”.
  • Create new table “societylocations”.

xamarin mysql coordinates map android,xamarin mysql,xamarin mysql coordinates,xamarin mysql coordinates map,mysql coordinates map android, xamarin load coordinates from mysql on Google Map Android,

Find the PHP API File under Coordinates directory.Put it in XAMPP > htdocs.

Create new xamarin mysql coordinates map android application project.Go to Main.axml file and add Fragment with MapFragment class.

capture-20160830-010500

Next MainActivity.cs.Do the following Steps.

  • Create Google Map class variable.
  • Refer Fragment from XML.
  • Perform HTTPWEBREQUEST request to get co-ordinates.
  • Parse JSON String with JSON Array and JSON Object.
  • Then pass values of latitude and longitude to addMarker method.
  • Finally animate camera to particular location.
namespace MyMap
{
	[Activity(Label = "MyMap", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity, IOnMapReadyCallback
	{
		private GoogleMap google;
		// latitude and longitude
		double latitude = 33.562031;
		double longitude = 73.071249;
		MapFragment mapFrag;


		protected override void OnCreate(Bundle savedInstanceState)
		{
			base.OnCreate(savedInstanceState);

			// Set our view from the "main" layout resource
			SetContentView(Resource.Layout.Main);
			getJSONString();


		}
		public void getJSONString()
		{
			var request = HttpWebRequest.Create(string.Format(@"http://your_server_ip/Coordinates/locations.php"));
			request.ContentType = "application/json";
			request.Method = "GET";
			var content = "";
			using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
			{
				if (response.StatusCode != HttpStatusCode.OK)
					Console.Out.WriteLine("Error fetching data. Server returned status code: {0}", response.StatusCode);
				using (StreamReader reader = new StreamReader(response.GetResponseStream()))
				{
					content = reader.ReadToEnd();
					if (string.IsNullOrWhiteSpace(content))
					{
						Console.Out.WriteLine("Response contained empty body...");
					}
					else {
						Console.Out.WriteLine("Response Body: \r\n {0}", content);
					}
					Assert.NotNull(content);
				}
			}
			Toast.MakeText(this, content, ToastLength.Short).Show();
			initializeMap(content); //calling map
		}

		/**
 		* 	function to load map.
 		* */

		public void initializeMap(String content)
		{
			LatLng location = new LatLng(latitude, longitude);  // setting latitude and longitude
																// getting MapFragment
			mapFrag = (MapFragment)FragmentManager.FindFragmentById(Resource.Id.map);
			// setting MapFragment to googleMap variable
			google = mapFrag.Map;


			if (google != null)
			{
				google.MapType = GoogleMap.MapTypeNormal;  // setting map type

				// creating marker
				try
				{
					JSONArray json = new JSONArray(content);
					for (int i = 0; i < json.Length(); i++)
					{
						JSONObject obj = json.GetJSONObject(i);
						String lati = obj.GetString("latitude");
						String longi = obj.GetString("longitude");
						MarkerOptions marker = new MarkerOptions()
							.SetPosition(new LatLng(Double.Parse(lati), Double.Parse(longi)))
					.SetTitle("Android Codec");
						google.AddMarker(marker);    // add marker to google Map

					}
				}
				catch (JSONException e)
				{
					e.StackTrace.ToString();
				}


				//positioning camera according to marker
				CameraPosition.Builder builder = CameraPosition.InvokeBuilder();
				builder.Target(location);
				builder.Zoom(15);
				builder.Bearing(155);

				CameraPosition cameraPosition = builder.Build();
				CameraUpdate cameraUpdate = CameraUpdateFactory.NewCameraPosition(cameraPosition);

				google.MoveCamera(cameraUpdate);  // move camera to marker



			}

		}

		public void OnMapReady(GoogleMap googleMap)
		{
			google = googleMap;
		}

	}
}

Android Manifest.xml

xamarin mysql coordinates map android,xamarin mysql,xamarin mysql coordinates,xamarin mysql coordinates map,mysql coordinates map android, xamarin load coordinates from mysql on Google Map Android,

Run your xamarin mysql coordinates map android application project.

xamarin mysql coordinates map android,xamarin mysql,xamarin mysql coordinates,xamarin mysql coordinates map,mysql coordinates map android, xamarin load coordinates from mysql on Google Map Android,

That’s it for xamarin mysql coordinates map android tutorial.

For Java example visit Android Codec.

Thanks.