Forum Xamarin.Android

Why speed doesn't work ...... Although longitude and latitude work successfully ?

YoussefYacoubYoussefYacoub USMember
edited March 2016 in Xamarin.Android

I use android manifest permission which is:

[assembly: UsesPermission(Android.Manifest.Permission.AccessCoarseLocation)]
[assembly: UsesPermission(Android.Manifest.Permission.AccessFineLocation)]
[assembly: UsesPermission(Android.Manifest.Permission.Internet)]

and this is my code and i don't know why speed doesn't working when i drive
Speed always equals to zero.

using System;
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Locations;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Android.Util;
using Android.Content;

namespace Get_Location
{
[Activity(Label = "Get_Location", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity, ILocationListener
{
LocationManager locMgr;
string tag = "MainActivity";
Button button;
TextView latitude;
TextView longitude;
TextView provider;
TextView speed;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        Log.Debug(tag, "OnCreate called");

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);
        button = FindViewById<Button>(Resource.Id.loc_button);
        latitude = FindViewById<TextView>(Resource.Id.latitude);
        longitude = FindViewById<TextView>(Resource.Id.longitude);
        provider = FindViewById<TextView>(Resource.Id.provider);
        speed = FindViewById<TextView>(Resource.Id.speed);
    }

    protected override void OnStart()
    {
        base.OnStart();
        Log.Debug(tag, "OnStart called");
    }

    protected override void OnResume()
    {
        base.OnResume();
        Log.Debug(tag, "OnResume called");

        // initialize location manager
        locMgr = GetSystemService(Context.LocationService) as LocationManager;

        button.Click += delegate {
            button.Text = "Location Service Running";

            if (locMgr.AllProviders.Contains(LocationManager.NetworkProvider)
                && locMgr.IsProviderEnabled(LocationManager.NetworkProvider))
            {
                locMgr.RequestLocationUpdates(LocationManager.NetworkProvider, 1000, 1, this);

            }
            else
            {


                var locationCriteria = new Criteria();
                locationCriteria.Accuracy = Accuracy.High;
                locationCriteria.PowerRequirement = Power.Medium;
                string locationProvider = locMgr.GetBestProvider(locationCriteria, true);
                Log.Debug(tag, "Starting location updates with " + locationProvider.ToString());
                locMgr.RequestLocationUpdates(locationProvider, 1000, 1, this);
            }
        };
    }

    protected override void OnPause()
    {
        base.OnPause();
        locMgr.RemoveUpdates(this);
        Log.Debug(tag, "Location updates paused because application is entering the background");
    }

    protected override void OnStop()
    {
        base.OnStop();
        Log.Debug(tag, "OnStop called");
    }

    public void OnLocationChanged(Location location)
    {
        Log.Debug(tag, "Location changed");
        latitude.Text = "Latitude: " + location.Latitude.ToString();
        longitude.Text = "Longitude: " + location.Longitude.ToString();
        provider.Text = "Provider: " + location.Provider.ToString();
        speed.Text = "Speed: " + location.Speed.ToString();
    }
    public void OnProviderDisabled(string provider)
    {
        Log.Debug(tag, provider + " disabled by user");
    }
    public void OnProviderEnabled(string provider)
    {
        Log.Debug(tag, provider + " enabled by user");
    }
    public void OnStatusChanged(string provider, Availability status, Bundle extras)
    {
        Log.Debug(tag, provider + " availability has changed to " + status.ToString());
    }
}

}

Thanks in advance :)))))

Answers

Sign In or Register to comment.