Advertisement
Guest User

HOI

a guest
Dec 18th, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Android.Webkit;
  5. using Android.Locations;
  6. using Android.App;
  7. using Android.Widget;
  8. using Android.OS;
  9. using System.Text;
  10. using Android.Util;
  11. using Android.Content;
  12. using Org.Json;
  13. using Android.Graphics;
  14.  
  15. namespace NationaleApp
  16. {
  17.     [Activity (Label = "mapActivity", Theme = "@android:style/Theme.NoTitleBar")]          
  18.     public class mapActivity : Activity, ILocationListener
  19.     {
  20.         TextView _locationText, _name;
  21.         int longitude, latitude;
  22.         LocationManager mLocationManager;
  23.         Location myLocation;
  24.         WebView webview;
  25.         Zoeken searched;
  26.         Double searchedLat, searchedLng;
  27.  
  28.         protected override void OnCreate (Bundle bundle)
  29.         {
  30.             base.OnCreate (bundle);
  31.  
  32.             searched = Tools.FromJSON<Zoeken>(Intent.GetStringExtra ("data"));
  33.             // Set our view from the "main" layout resource
  34.             SetContentView (Resource.Layout.Map);
  35.        
  36.             //webview.SetWebViewClient (new CustomWebViewClient ());
  37.  
  38.             _locationText = FindViewById<TextView> (Resource.Id.location_text);
  39.             _locationText.Text = searched.school.pltsnm;
  40.  
  41.             _name = FindViewById<TextView> (Resource.Id.testText);
  42.             _name.Text = searched.school.nm;
  43.  
  44.             myLocation = getLastKnownLocation ();
  45.             createMap ();
  46.  
  47.             getLocationByPostcode (searched.school.pstcode);
  48.  
  49.             Console.WriteLine("map.html?longitude=" + myLocation.Longitude + "&latitude=" + myLocation.Latitude + "&longitude2=" + searchedLng + "&latitude=" + searchedLat + "&nm=" + searched.school.nm + "&tel=" + searched.school.telnmr);
  50.             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);
  51.             //webview.EvaluateJavascript("drawSearched(" + searchedLat + ", " + searchedLng + ", '" + searched.school.nm + "', " + searched.school.telnmr + ");", null);
  52.         }
  53.  
  54.         public void OnLocationChanged(Location loc) {  
  55.             Toast.MakeText(
  56.                 this,
  57.                 "Location changed: Lat: " + loc.Latitude + " Lng: "
  58.                 + loc.Longitude, ToastLength.Short).Show();
  59.             string longitude = "Longitude: " + loc.Longitude;
  60.             Log.Debug("Longtitude", longitude);
  61.             string latitude = "Latitude: " + loc.Latitude;
  62.             Log.Debug("Latitude", latitude);
  63.  
  64.             /*------- To get city name from coordinates -------- */
  65.             String cityName = null;
  66.             Geocoder gcd = new Geocoder(this.BaseContext, this.Resources.Configuration.Locale);
  67.             List<Address> addresses;
  68.             try {
  69.                 addresses = new List<Address>(gcd.GetFromLocation(loc.Latitude,
  70.                     loc.Longitude, 1));
  71.                 if (addresses.Count() > 0) {
  72.                     Log.Debug("location{0}", addresses.ElementAt(0).Locality);
  73.                     //System.out.println(addresses.get(0).getLocality());
  74.                     cityName = addresses.ElementAt(0).Locality;
  75.                 }
  76.             }
  77.             catch (IndexOutOfRangeException e) {
  78.                 e.ToString();
  79.             }
  80.             String s = longitude + "\n" + latitude + "\n\nMy Current City is: "
  81.                 + cityName;
  82.             webview.EvaluateJavascript("postionChanged(" + latitude + ", " + longitude + ");", null);
  83.             this.longitude = Convert.ToInt16(loc.Longitude);
  84.             this.latitude = Convert.ToInt16(loc.Latitude);
  85.         }
  86.  
  87.         public void createMap()
  88.         {
  89.             //LocationManager locationManager = (LocationManager)
  90.             //  this.GetSystemService (Context.LocationService);
  91.  
  92.             ILocationListener locationListener = this;
  93.             mLocationManager.RequestLocationUpdates(
  94.                 LocationManager.GpsProvider, 5000, 10, locationListener);
  95.  
  96.             //string locationProvider = LocationManager.GpsProvider;
  97.             //Location lastKnowLocation = locationManager.GetLastKnownLocation (locationProvider);
  98.  
  99.             webview = FindViewById<WebView> (Resource.Id.webView1);
  100.             webview.Settings.JavaScriptEnabled = true;
  101.         }
  102.  
  103.  
  104.         private void getLocationByPostcode(string pstcode)
  105.         {
  106.             string URL = "http://maps.google.com/maps/api/geocode/json?address=" + pstcode;
  107.             JSONObject jsonobject = null;
  108.             string data = Tools.DownloadDataFromUrl (URL);
  109.             try {
  110.                 jsonobject = new JSONObject(data);
  111.  
  112.             } catch(Exception t) {
  113.                 Console.WriteLine ("Could not parse Json: " + t.ToString());
  114.             }
  115.                
  116.             searchedLat = jsonobject.GetJSONArray("results").GetJSONObject (0).GetJSONObject("geometry").GetJSONObject ("location").GetDouble ("lat");
  117.             searchedLng = jsonobject.GetJSONArray("results").GetJSONObject (0).GetJSONObject("geometry").GetJSONObject ("location").GetDouble ("lng");
  118.         }
  119.  
  120.         private Location getLastKnownLocation() {
  121.             mLocationManager = (LocationManager)
  122.                 this.GetSystemService (Context.LocationService);
  123.             IList<string> providers =  mLocationManager.GetProviders(true);
  124.             Location bestLocation = null;
  125.             foreach (string provider in providers) {
  126.                 Location l = mLocationManager.GetLastKnownLocation(provider);
  127.                 if (l == null) {
  128.                     continue;
  129.                 }
  130.                 if (bestLocation == null || l.Accuracy < bestLocation.Accuracy) {
  131.                     // Found best last known location: %s", l);
  132.                     bestLocation = l;
  133.                 }
  134.             }
  135.             return bestLocation;
  136.         }
  137.  
  138.         public void OnProviderDisabled(String provider) {}
  139.  
  140.         public void OnProviderEnabled(String provider) {}
  141.  
  142.         public void OnStatusChanged(String provider, Availability available, Bundle extras) {}
  143.  
  144.         public void Dispose() { }
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement