如何在c#quot;中基于android手机sim网络获取用户位置;?
我希望用户的当前位置(纬度和经度)始终只使用基于Sim卡的网络,而不使用任何其他网络(如WiFi、移动数据、GPS和其他网络,甚至所有这些都在移动中处于禁用模式)。不一定是精确的位置,甚至是近似的位置 有可能得到它吗?若你们中有人能解释并包含代码;我在谷歌上搜索,但没有得到正确的相关答案如何在c#quot;中基于android手机sim网络获取用户位置;?,c#,android,geolocation,C#,Android,Geolocation,我希望用户的当前位置(纬度和经度)始终只使用基于Sim卡的网络,而不使用任何其他网络(如WiFi、移动数据、GPS和其他网络,甚至所有这些都在移动中处于禁用模式)。不一定是精确的位置,甚至是近似的位置 有可能得到它吗?若你们中有人能解释并包含代码;我在谷歌上搜索,但没有得到正确的相关答案 using Android.App; using Android.Content; using Android.Runtime; using Android.Views; using Android.Widge
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) { }
}
}
使用Android.App;
使用Android.Content;
使用Android.Runtime;
使用Android.Views;
使用Android.Widget;
使用Android.OS;
使用Android.Locations;
使用Android.Util;
使用制度;
使用System.Collections.Generic;
使用系统文本;
使用System.Linq;
使用System.Xml.Linq;
使用系统线程;
名称空间GPS_Android
{
[活动(Label=“GPS_Android”,MainLauncher=true,Icon=“@drawable/Icon”)]
公共类主活动:活动,ILocationListener
{
私有位置_currentLocation;
私人场所经理(场所经理);;
私有文本视图_locationText;
私有文本视图_addressText;
私有字符串_locationProvider;
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
_addressText=findviewbyd(Resource.Id.address\u text);
_locationText=findviewbyd(Resource.Id.location\u text);
FindViewById(Resource.Id.get\u address\u按钮)。单击+=AddressButton\u OnClick;
初始化ElocationManager();
}
public void InitializeLocationManager()
{
_locationManager=(locationManager)GetSystemService(LocationService);
criteriaForLocationService=新标准
{
准确度=准确度。很好
};
IList acceptableLocationProviders=\u locationManager.GetProviders(criteriaForLocationService,true);
if(acceptableLocationProviders.Any())
{
_locationProvider=acceptableLocationProviders.First();
}
其他的
{
_locationProvider=String.Empty;
}
}
受保护的覆盖void OnResume()
{
base.OnResume();
_locationManager.RequestLocationUpdate(\u locationProvider,0,0,this);
}
受保护的覆盖void OnPause()
{
base.OnPause();
_locationManager.RemoveUpdate(此);
}
async void AddressButton_OnClick(对象发送方,EventArgs EventArgs)
{
如果(_currentLocation==null)
{
_addressText.Text=“无法确定当前位置。”;
返回;
}
Geocoder Geocoder=新的Geocoder(本);
IList addressList=await geocoder.GetFromLocationAsync(_currentLocation.Latitude,_currentLocation.Latitude,10);
Address=addressList.FirstOrDefault();
if(地址!=null)
{
StringBuilder deviceAddress=新StringBuilder();
对于(int i=0;i
您可以使用LocationManager.NETWORK\u提供程序获取纬度和经度,而无需打开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");
}
}
});