如何在c#中获取基于android中移动sim网络的用户位置?
发布时间:2020-12-15 22:09:54 所属栏目:百科 来源:网络整理
导读:我想让用户的当前位置(经度和纬度)始终只使用基于Sim的网络,而不是使用任何其他网络(如WiFi,移动数据,GPS和其他网络,甚至所有这些都在移动设备中处于禁用模式).不一定是确切的位置,但甚至大致的位置 有没有可能得到它?如果您有任何人可以解释并包含代码;我
我想让用户的当前位置(经度和纬度)始终只使用基于Sim的网络,而不是使用任何其他网络(如WiFi,移动数据,GPS和其他网络,甚至所有这些都在移动设备中处于禁用模式).不一定是确切的位置,但甚至大致的位置
有没有可能得到它?如果您有任何人可以解释并包含代码;我在谷歌搜索但没有得到正确的相关答案. 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,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) { } } } 解决方法
您可以使用LocationManager.NETWORK_PROVIDER获取纬度和经度而无需打开GPS
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"); } } }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |