我试图获得没有gps的纬度和经度,但它给了我null
。但是当我在我的gps上时,它给出了lat long的正确值。我想通过wifi获得经纬度。我怎么解决这个问题?这是我的网络提供商代码:
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
return null;
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return null;
}
//updates will be send according to these arguments
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return null;
}
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
答案 0 :(得分:1)
尝试使用WifiManager类。
如文档中所述,
此类提供用于管理Wi-Fi连接的所有方面的主要API。通过调用
Private Sub Command15_Click() Dim oExcel As Object Dim oExcelWrkBk As Object Dim oExcelWrSht As Object Dim bExcelOpened As Boolean 'Start Excel On Error Resume Next Set oExcel = GetObject(, "Excel.Application") 'Bind to existing instance of Excel If Err.Number <> 0 Then 'Could not get instance of Excel, so create a new one Err.Clear On Error GoTo Error_Handler Set oExcel = CreateObject("excel.application") bExcelOpened = False Else 'Excel was already running bExcelOpened = True End If On Error GoTo Error_Handler oExcel.ScreenUpdating = False oExcel.Visible = False 'Keep Excel hidden until we are done with our manipulation 'Set oExcelWrkBk = oExcel.Workbooks.Add() 'Start a new workbook Set oExcelWrkBk = oExcel.Workbooks.Open("C:\test.xlsx") 'Open an existing Excel file Set oExcelWrSht = oExcelWrkBk.Sheets(1) 'which worksheet to work with 'Start copying over your form values to the Excel Spreadsheet 'Cells(8, 3) = 8th row, 3rd column oExcelWrSht.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0) = Me.1 oExcelWrSht.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0) = Me.2 oExcelWrSht.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0) = Me.3 oExcelWrSht.Cells(Rows.Count, 4).End(xlUp).Offset(1, 0) = Me.4 oExcelWrSht.Cells(Rows.Count, 5).End(xlUp).Offset(1, 0) = Me.5 oExcelWrSht.Cells(Rows.Count, 6).End(xlUp).Offset(1, 0) = Me.6 oExcelWrSht.Cells(Rows.Count, 7).End(xlUp).Offset(1, 0) = Me.7 oExcelWrSht.Cells(Rows.Count, 8).End(xlUp).Offset(1, 0) = Me.8 oExcelWrSht.Cells(Rows.Count, 9).End(xlUp).Offset(1, 0) = Me.9 '... and so on ... oExcelWrSht.Range("A1").Select 'Return to the top of the page ' oExcelWrkBk.Close True, sFileName 'Save and close the generated workbook ' 'Close excel if is wasn't originally running ' If bExcelOpened = False Then ' oExcel.Quit ' End If Error_Handler_Exit: On Error Resume Next oExcel.Visible = True 'Make excel visible to the user Set oExcelWrSht = Nothing Set oExcelWrkBk = Nothing oExcel.ScreenUpdating = True Set oExcel = Nothing Exit Sub Error_Handler: MsgBox "The following error has occured" & vbCrLf & vbCrLf & _ "Error Number: " & Err.Number & vbCrLf & _ "Error Source: Export2XLS" & vbCrLf & _ "Error Description: " & Err.Description _ , vbOKOnly + vbCritical, "An Error has Occured!" Resume Error_Handler_Exit End Sub
获取此类的实例。这是执行Wi-Fi特定操作时使用的API。
另外,检查此SO帖子中给出的解决方案 - Android: How to Enable/Disable Wifi or Internet Connection Programmatically。希望它有所帮助!
答案 1 :(得分:0)
请询问是否启用了网络提供程序。您可以使用以下命令检查network_provider是否已启用:locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
您可以使用以下方式请求位置更新: locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,this);
您可以使用以下方式获取上一个已知位置: 位置lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
我在我的github repo中有一个示例应用程序,它曾经工作过。虽然没有最近的更新。这是链接:https://github.com/sauravpradhan/TrackMe-Ver-2.0
如果有帮助,请回来。
答案 2 :(得分:0)
提供的链接包含所有信息。但是要从Lat获取位置,我们需要使用GeoCoder。
Geocoder g1= new Geocoder(getApplicationContext());
List<Address> locale= null;
String add = null;
try
{
locale = g1.getFromLocation(location.getLatitude(), location.getLongitude(), 3);
}
catch(Exception e)
{
Log.d("Saurav_log", "Cant fetch address");
}
/* finally
{
Log.d("Saurav_log", "Finally Exiting");
}*/
TextView textView2 = (TextView)findViewById(R.id.textView2);
if(locale != null)
{
add =locale.get(0).getAddressLine(0);
}
textView2.setText("From GPS:Actual Address \n"+add+locale.get(0).getLocality()+" \n"+locale.get(0).getCountryName()+" \n"+locale.get(0).getSubLocality());