我想在我的应用程序启动时启用GPS 30秒(当oncreate()方法调用时).30秒后转为禁用模式.
这是我使用放置位置字符串的代码:
public static String locationStringFromLocation(final Location location) {
return String.format("%.3f %.3f", location.getLatitude(), location.getLongitude());
}
Run Code Online (Sandbox Code Playgroud)
从其他一些设备上,我有时会得到:
-7.2900002123788E-4 7.270000060088933E-4作为位置字符串,而不是-7.290 7.270例如。
编辑
更新的代码。这会解决问题吗?
DecimalFormat decimalFormat = new DecimalFormat("#.###");
if (location != null) {
final String latitude = decimalFormat.format(Float.valueOf(Location.convert(location.getLatitude(), Location.FORMAT_DEGREES)));
final String longitude = decimalFormat.format(Float.valueOf(Location.convert(location.getLongitude(), Location.FORMAT_DEGREES)));
return latitude + " " + longitude;
}
return decimalFormat.format(0.0F) + " " + decimalFormat.format(0.0F);
Run Code Online (Sandbox Code Playgroud) 我想要获得latitude,longitude但有时network is available但我没有得到latitude和的价值longitude.我正在使用MyLocationListener类并将条件设置为所有但是有些时间value没有得到.
protected void showCurrentLocation()
{
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null)
{
counter++;
latitude=location.getLatitude();
longitude=location.getLongitude();
altitude=location.getAltitude();
}
}
private class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location location)
{
counter++;
latitude=location.getLatitude();
longitude=location.getLongitude();
altitude=location.getAltitude();
}
@Override
public void onStatusChanged(String s, int i, Bundle b)
{
}
@Override
public void onProviderDisabled(String s)
{
}
@Override
public void onProviderEnabled(String s)
{
}
}
Run Code Online (Sandbox Code Playgroud) 我正在制作一个实时位置监听应用程序,我需要每 3 秒调用一次位置更新。
我使用融合位置。
我的间隔设置为 3 秒,但是看起来这个间隔的最小值是5 秒。
代码:
public class GpsService extends Service implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener {
private static int UPDATE_INTERVAL = 1000 * 3; // /< location update interval
private static int FASTEST_INTERVAL = 1000 * 3; // /< fastest location update interval
private static int DISPLACE_METERS = 10; // displacement in meters, not used atm
private LocationRequest mLocationRequest;
private LocationWrapper mLastLocation;
...
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
Run Code Online (Sandbox Code Playgroud)
... …
我正在使用 Android 地理围栏 API。添加地理围栏后,当位置打开时,它工作正常。但是如果我关闭位置并再次打开,之后进入或退出时地理围栏不会触发。我已经在使用 Android 5.0.2 设备的 Moto G 上进行了测试。如果位置关闭,地理围栏是否会过期?
我在 Android 文档中看到,
如果用户禁用网络位置提供程序,地理围栏服务将停止更新,所有注册的地理围栏将被删除,并由提供的待处理意图生成意图。在这种情况下,从此意图创建的 GeofencingEvent 表示错误事件,其中 hasError() 返回 true,而 getErrorCode() 返回 GEOFENCE_NOT_AVAILABLE。
我想获取用户的国家/地区代码,而不使用在 android M 下需要主动请求的任何权限。例如,如果用户位于中国,我想取回 CH。我不想为此使用“危险”权限 - 也不想read_phone_state通过TelephonyManager,也不想通过位置来尝试根据他的 GPS 进行猜测。有没有办法做到这一点,如果有的话,怎么做?
我尝试使用以下代码,但如果用户将其语言设置与其所在国家/地区分开(例如印度的用户将其手机设置为 English_US),则很容易失败:
Resources r = paramContext.getResources();
Configuration config = r.getConfiguration();
Locale currentLocale = config.locale;
Log.d("TAG", "Country: " + currentLocale.getCountry());
Run Code Online (Sandbox Code Playgroud) android country-codes android-location android-permissions android-6.0-marshmallow
我有一个代码应该返回用户位置,但经度不断返回null,因此我的应用程序不断崩溃.我该如何防止这种情况?
//get the user longitude and latitude
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
Geocoder geocoder; //return meaningful data from user location
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);
address = addresses.get(0).getAddressLine(0);
city = addresses.get(0).getLocality();
Run Code Online (Sandbox Code Playgroud) 我想通过 GPS 以地址的形式获取我当前的位置。我正在使用安卓工作室。它说我的应用程序停止工作。其中有什么错误?有人可以帮我摆脱这种情况吗?
我在 activity_main.xml 文件中的代码是
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Run Code Online (Sandbox Code Playgroud)
我在 MainActivity.java 中的代码是
package com.example.showlatlong;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import java.util.List;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
Location gps_loc, network_loc, final_loc;
double longitude;
double latitude;
String userCountry, userAddress;
@Override
protected void onCreate(Bundle savedInstanceState) …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我有一个位置监听器,它记录了位置的准确性,并在我所在的地图上放置了一个标记,但是精确度总是在900左右,我的位置在我实际上的0.2到0.5英里之间.如果我加载谷歌地图,它会在几米内获得我的位置.
有什么方法可以确保准确的位置?我在Manifest中拥有这些权限
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Run Code Online (Sandbox Code Playgroud)
这是我的倾听者
LocationListener searchUsingLocation = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
float accuracy = location.getAccuracy();
Log.d("FUApp", "Location changed: "+accuracy);
List<JSONObject> centres = sortVenuesByNearest(location);
try {
populateList(centres);
} catch (JSONException e) {
e.printStackTrace();
}
setRefreshActionButtonState(false, R.id.search_using_location);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
Log.d("FUApp", "Starting request");
locationService.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 50, 5, searchUsingLocation);
Run Code Online (Sandbox Code Playgroud)
编辑:此外我的手机设置,访问位置,GPS卫星通信,Wi-Fi和移动网络位置也在.
我需要你宝贵的帮助;)
我有一个的Android服务即在后台运行该固定位置的装置周期性地(一个位置轮询).当我使用使用Google Play服务的新Android Location API更新代码时.让我们来看看服务:
public class NewLocationPollerService extends Service implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener
{
private LocationRequest mLocationRequest = null;
private LocationClient mLocationClient = null;
...
private class PollerThread extends WakefulThread
{
protected void onPreExecute()
{
if ( GooglePlayServicesUtility.areServicesConnected( NewLocationPollerService.this ) )
mLocationClient.requestLocationUpdates(mLocationRequest, intent);
}
protected void onPostExecute()
{
if ( GooglePlayServicesUtility.areServicesConnected( NewLocationPollerService.this ) )
mLocationClient.removeLocationUpdates(intent);
super.onPostExecute();
}
}
...
}
Run Code Online (Sandbox Code Playgroud)
方法" areServicesConnected() "如下:
public class GooglePlayServicesUtility
{
private static final …Run Code Online (Sandbox Code Playgroud)