Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Android.Webkit;
- using Android.Locations;
- using Android.App;
- using Android.Widget;
- using Android.OS;
- using System.Text;
- using Android.Util;
- using Android.Content;
- using Org.Json;
- using Android.Graphics;
- namespace NationaleApp
- {
- [Activity (Label = "mapActivity", Theme = "@android:style/Theme.NoTitleBar")]
- public class mapActivity : Activity, ILocationListener
- {
- TextView _locationText, _name;
- int longitude, latitude;
- LocationManager mLocationManager;
- Location myLocation;
- WebView webview;
- Zoeken searched;
- Double searchedLat, searchedLng;
- protected override void OnCreate (Bundle bundle)
- {
- base.OnCreate (bundle);
- searched = Tools.FromJSON<Zoeken>(Intent.GetStringExtra ("data"));
- // Set our view from the "main" layout resource
- SetContentView (Resource.Layout.Map);
- //webview.SetWebViewClient (new CustomWebViewClient ());
- _locationText = FindViewById<TextView> (Resource.Id.location_text);
- _locationText.Text = searched.school.pltsnm;
- _name = FindViewById<TextView> (Resource.Id.testText);
- _name.Text = searched.school.nm;
- myLocation = getLastKnownLocation ();
- createMap ();
- getLocationByPostcode (searched.school.pstcode);
- Console.WriteLine("map.html?longitude=" + myLocation.Longitude + "&latitude=" + myLocation.Latitude + "&longitude2=" + searchedLng + "&latitude=" + searchedLat + "&nm=" + searched.school.nm + "&tel=" + searched.school.telnmr);
- webview.LoadUrl ("file:///android_asset/Website/map.html?longitude=" + myLocation.Longitude + "&latitude=" + myLocation.Latitude + "&longitude2=" + searchedLng + "&latitude=" + searchedLat + "&nm=" + searched.school.nm + "&tel=" + searched.school.telnmr);
- //webview.EvaluateJavascript("drawSearched(" + searchedLat + ", " + searchedLng + ", '" + searched.school.nm + "', " + searched.school.telnmr + ");", null);
- }
- public void OnLocationChanged(Location loc) {
- Toast.MakeText(
- this,
- "Location changed: Lat: " + loc.Latitude + " Lng: "
- + loc.Longitude, ToastLength.Short).Show();
- string longitude = "Longitude: " + loc.Longitude;
- Log.Debug("Longtitude", longitude);
- string latitude = "Latitude: " + loc.Latitude;
- Log.Debug("Latitude", latitude);
- /*------- To get city name from coordinates -------- */
- String cityName = null;
- Geocoder gcd = new Geocoder(this.BaseContext, this.Resources.Configuration.Locale);
- List<Address> addresses;
- try {
- addresses = new List<Address>(gcd.GetFromLocation(loc.Latitude,
- loc.Longitude, 1));
- if (addresses.Count() > 0) {
- Log.Debug("location{0}", addresses.ElementAt(0).Locality);
- //System.out.println(addresses.get(0).getLocality());
- cityName = addresses.ElementAt(0).Locality;
- }
- }
- catch (IndexOutOfRangeException e) {
- e.ToString();
- }
- String s = longitude + "\n" + latitude + "\n\nMy Current City is: "
- + cityName;
- webview.EvaluateJavascript("postionChanged(" + latitude + ", " + longitude + ");", null);
- this.longitude = Convert.ToInt16(loc.Longitude);
- this.latitude = Convert.ToInt16(loc.Latitude);
- }
- public void createMap()
- {
- //LocationManager locationManager = (LocationManager)
- // this.GetSystemService (Context.LocationService);
- ILocationListener locationListener = this;
- mLocationManager.RequestLocationUpdates(
- LocationManager.GpsProvider, 5000, 10, locationListener);
- //string locationProvider = LocationManager.GpsProvider;
- //Location lastKnowLocation = locationManager.GetLastKnownLocation (locationProvider);
- webview = FindViewById<WebView> (Resource.Id.webView1);
- webview.Settings.JavaScriptEnabled = true;
- }
- private void getLocationByPostcode(string pstcode)
- {
- string URL = "http://maps.google.com/maps/api/geocode/json?address=" + pstcode;
- JSONObject jsonobject = null;
- string data = Tools.DownloadDataFromUrl (URL);
- try {
- jsonobject = new JSONObject(data);
- } catch(Exception t) {
- Console.WriteLine ("Could not parse Json: " + t.ToString());
- }
- searchedLat = jsonobject.GetJSONArray("results").GetJSONObject (0).GetJSONObject("geometry").GetJSONObject ("location").GetDouble ("lat");
- searchedLng = jsonobject.GetJSONArray("results").GetJSONObject (0).GetJSONObject("geometry").GetJSONObject ("location").GetDouble ("lng");
- }
- private Location getLastKnownLocation() {
- mLocationManager = (LocationManager)
- this.GetSystemService (Context.LocationService);
- IList<string> providers = mLocationManager.GetProviders(true);
- Location bestLocation = null;
- foreach (string provider in providers) {
- Location l = mLocationManager.GetLastKnownLocation(provider);
- if (l == null) {
- continue;
- }
- if (bestLocation == null || l.Accuracy < bestLocation.Accuracy) {
- // Found best last known location: %s", l);
- bestLocation = l;
- }
- }
- return bestLocation;
- }
- public void OnProviderDisabled(String provider) {}
- public void OnProviderEnabled(String provider) {}
- public void OnStatusChanged(String provider, Availability available, Bundle extras) {}
- public void Dispose() { }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement