LocationManagerAndroid上的API似乎对于仅需要偶尔粗略估计用户位置的应用程序来说有点痛苦.
我正在处理的应用程序本身并不是一个位置应用程序,但它确实需要获取用户的位置才能显示附近的商家列表.它不需要担心用户是否在移动或类似的东西.
这是我想做的事情:
ActivityX中需要它时,它将可用.看起来它应该不那么难,但在我看来,我必须启动两个不同的位置提供商(GPS和NETWORK)并管理每个生命周期.不仅如此,我还必须在多个活动中复制相同的代码以满足#2.我曾尝试过使用getBestProvider()过去将解决方案缩减为只使用一个位置提供商,但这似乎只能为您提供最好的"理论"提供商,而不是实际上会给您带来最佳结果的提供商.
有没有更简单的方法来实现这一目标?
LocationClient和之间有什么区别LocationManager.
它们之间的优缺点是什么(如电池,准确度)?
哪个更好用?
我从http://developer.android.com/training/location/retrieve-current.html下载了一个演示项目,我想我没有丢失任何步骤; 但我找不到哪个jar文件包含"com.google.android.gms.location.LocationClient.class"文件
我们发现所有"google-play-services.jar"和"maps.jar"以及"android.jar(所有版本)"都不包含"LocationClient.class"?
我正在努力获得最准确的位置.
到目前为止,我在Google文档中成功使用了LocationClient:http: //developer.android.com/training/location/retrieve-current.html
但现在我发现这个类很老了:http: //developer.android.com/reference/com/google/android/gms/location/package-summary.html
"不推荐使用此类.使用LocationServices".
但是,在LocationServices的示例中,即使在此站点中: Android的LocationClient类也已弃用,但在文档中使用
看来我们应该使用import:com.google.android.gms.location.LocationServices但是我无法导入这个类..
有任何想法吗 ?
编辑:
我在这个页面找到了:h2ttps://github.com/M66B/XPrivacy/issues/1774
看似相似的问题,有人猜测:
"似乎是Play服务5.0的一部分."
我想知道是不是这样 - 有人试过这个吗?
我们在崩溃时遇到了这种崩溃,奇怪的是它在onConnected()请求位置更新时会在回调中发生.
码:
abstract public class MainService_6_LocationClient extends MainService_5_DriverGpsLocationStoring
implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private LocationListener highAccuracyListener;
private GoogleApiClient googleApiClient;
private LocationRequest gpsRequest;
@Override
public void onCreate() {
super.onCreate();
lazyInit();
googleApiClient.connect();
}
@Override public void onConnected(Bundle bundle) {
Log.d(TAG, "onConnected");
lazyInit();
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, gpsRequest,
highAccuracyListener);
}
private void lazyInit() {
if (highAccuracyListener == null) {
highAccuracyListener = new HighAccuracyLocationListener();
}
if (googleApiClient == null) {
googleApiClient = new GoogleApiClient.Builder(this).addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
if (gpsRequest == null) {
gpsRequest = new …Run Code Online (Sandbox Code Playgroud) 我只是想做一个简单的"教程"应用程序来获取我的手机的位置(以便在以后的其他应用程序中学习如何使用它),但我只是没有到达任何地方.
Android Developer的教程:首先,我按照Android开发者网站(developer.android.com/training/location/receive-location-updates.html)中的教程进行操作.
就像那里所示,我使用了Location,LocationClient和LocationRequest,在onCreate中初始化它们(并设置它们).LocationClient在onStart和onStop上连接和断开连接.
我在连接后请求位置更新(在onConnected中).我在进行此调用之前验证了GooglePlayServices是否可用,而且我仍然没有在"onLocationChanged"中获得任何更新.
GoogleApiClient:我注意到不推荐使用LocationClient,而首选的是LocationServices(developer.android.com/reference/com/google/android/gms/location/LocationClient.html).
如下所示:https://developer.android.com/reference/com/google/android/gms/location/FusedLocationProviderApi.html,我使用GoogleApiClient并将其设置为LocationServices(也是/sf/answers/1750969531/).所以我把它全部设置为使用它,但我仍然没有得到任何关于位置的更新.
onLocationChanged如果有响应则应该打印.我还放了一个打印位置或"没有"的按钮.
我检查了使用谷歌地图,看看我是否有一些东西,但它的工作正常.
的build.gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.android.gms:play-services:5.0.77'
compile "com.android.support:support-v4:20.0.+"
}
Run Code Online (Sandbox Code Playgroud)
表现
...
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
...
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
...
Run Code Online (Sandbox Code Playgroud)
码
public class MyActivity extends Activity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
private GoogleApiClient mLocationClient;
private Location mCurrentLocation;
LocationRequest mLocationRequest;
...
/* Constant fields - request interval */
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
mLocationClient …Run Code Online (Sandbox Code Playgroud) android location android-location google-api-client google-play-services
我正在尝试为我的新导航应用程序获取用户位置.我想经常检查用户位置,它必须准确.我使用示例中的以下代码来获取位置.
public class MainActivity extends Activity implements LocationListener {
private LocationManager locationManager;
private String provider;
private TextView a;
private TextView b;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
a = (TextView) findViewById(R.id.a);
b = (TextView) findViewById(R.id.b);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
a.setText("Location not available");
b.setText("Location not available");
}
} …Run Code Online (Sandbox Code Playgroud) 我想在我的Android应用程序中实现类似这样的东西,我正在Xamarin中开发但是找不到它所在的命名空间GoogleApiClient.有人可以帮忙吗?
我格式化了我的计算机并再次导入了我的Android项目,该项目已经使用Google Maps API V2完成并发现了许多错误.我重新链接了google_play_services_lib和android-support-v7-appcompat.I仍然在导入com.google时发现错误. android.gms.location.LocationClient.Only"LocationClient"的调用给出错误,其他导入正常.
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
(ERROR)import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
Run Code Online (Sandbox Code Playgroud)
并且在我使用R.调用的任何地方发现错误.错误:R无法解析为变量
setContentView(R.layout.activity_main);
EditText et = (EditText) findViewById(R.id.editText1);
并在控制台中收到以下错误.
[2014-12-19 23:22:44 - MapsProject] C:\Users\Sudesh\Android_Workspace\android-support-v7-appcompat\res\values-v21\themes_base.xml:121: error: Error: No resource found that matches the given name: attr 'android:colorControlHighlight'.
[2014-12-19 23:22:44 - MapsProject]
[2014-12-19 23:22:44 - MapsProject] C:\Users\Sudesh\Android_Workspace\android-support-v7-appcompat\res\values-v21\themes_base.xml:119: error: Error: No resource found that matches the given …Run Code Online (Sandbox Code Playgroud) android android-appcompat r.java-file google-play-services location-client
android ×10
location ×4
geolocation ×3
geofencing ×1
google-api ×1
google-maps ×1
gps ×1
java ×1
r.java-file ×1
xamarin ×1