c# - 如何在 c# 中基于 android 中的移动 sim 网络获取用户位置”?

标签 c# android geolocation

我想始终仅使用基于 Sim 的网络获取用户的当前位置(纬度和经度),而不是使用任何其他网络(如 WiFi、移动数据、GPS 和其他网络,甚至所有这些都处于禁用模式)移动的)。不一定是确切的位置,甚至是大概的位置。

有没有可能得到它?如果你们中的任何人可以解释并包含代码;我在 Google 中搜索但没有得到正确的相关答案。

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Locations;
using Android.Util;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Xml.Linq;
using System.Threading;




namespace GPS_Android
{
    [Activity(Label = "GPS_Android", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity,ILocationListener
        {
        private Location _currentLocation;
        private LocationManager _locationManager;
        private TextView _locationText;
        private TextView _addressText;
        private string _locationProvider;


        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);

            _addressText = FindViewById<TextView>(Resource.Id.address_text);
            _locationText = FindViewById<TextView>(Resource.Id.location_text);

            FindViewById<TextView>(Resource.Id.get_address_button).Click += AddressButton_OnClick;
            InitializeLocationManager();


        }

        public void InitializeLocationManager()
        {
            _locationManager = (LocationManager)GetSystemService(LocationService);

            Criteria criteriaForLocationService = new Criteria
            {
                Accuracy = Accuracy.Fine
            };
            IList<string> acceptableLocationProviders = _locationManager.GetProviders(criteriaForLocationService, true);

            if (acceptableLocationProviders.Any())
            {
                _locationProvider = acceptableLocationProviders.First();
            }
            else
            {
                _locationProvider = String.Empty;
            }
        }


        protected override void OnResume()
        {
            base.OnResume();
            _locationManager.RequestLocationUpdates(_locationProvider, 0, 0, this);
        }


        protected override void OnPause()
        {
            base.OnPause();
            _locationManager.RemoveUpdates(this);
        }

        async void AddressButton_OnClick(object sender, EventArgs eventArgs)
        {
            if (_currentLocation == null)
            {
                _addressText.Text = "Can't determine the current location.";
                return;
            }

            Geocoder geocoder = new Geocoder(this);
            IList<Address> addressList = await geocoder.GetFromLocationAsync(_currentLocation.Latitude, _currentLocation.Longitude, 10);

            Address address = addressList.FirstOrDefault();
            if (address != null)
            {
                StringBuilder deviceAddress = new StringBuilder();
                for (int i = 0; i < address.MaxAddressLineIndex; i++)
                {
                    deviceAddress.Append(address.GetAddressLine(i))
                        .AppendLine(",");
                }
                _addressText.Text = deviceAddress.ToString();
            }
            else
            {
                _addressText.Text = "Unable to determine the address.";
            }

     }  

        public void OnLocationChanged(Location location)
        {
            _currentLocation = location;
            if (_currentLocation == null)
            {
                _locationText.Text = "Unable to determine your location.";
            }
            else
            {
                _locationText.Text = String.Format("{0},{1}", _currentLocation.Latitude, _currentLocation.Longitude);
            }

        }

        public void OnProviderDisabled(string provider) { }

        public void OnProviderEnabled(string provider) { }

        public void OnStatusChanged(string provider, Availability status, Bundle extras) { }
    }
}

最佳答案

不用打开GPS就可以使用LocationManager.NETWORK_PROVIDER获取经纬度

    btnNWShowLocation.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {

            Location nwLocation = appLocationService
                    .getLocation(LocationManager.NETWORK_PROVIDER);

            if (nwLocation != null) {
                double latitude = nwLocation.getLatitude();
                double longitude = nwLocation.getLongitude();
                Toast.makeText(
                        getApplicationContext(),
                        "Mobile Location (NW): \nLatitude: " + latitude
                                + "\nLongitude: " + longitude,
                        Toast.LENGTH_LONG).show();

            } else {
                showSettingsAlert("NETWORK");
            }

        }
    });

关于c# - 如何在 c# 中基于 android 中的移动 sim 网络获取用户位置”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27100002/

相关文章:

c# - 在 3 层系统中在哪里抛出异常

c# - 是否可以从 .net 应用程序禁用 c++ 断言

javascript - 如何从 Google 版本 map 中拖动的标记获取格式化地址

javascript - Firefox 和 IE 之间 GeoRSS 叠加的加载时间不一致

c# - 获取子记录列表

c# - Json 属性作为节点生成

java - 如何检索放置在从媒体扫描中排除的文件夹中的视频缩略图

android - 应用程序处于后台时服务暂停 && 屏幕被锁定

android - OpenGL ES 1.1 安卓立方体贴图

javascript - 谷歌浏览器地理定位不起作用