使用哪个 API 访问 Android 设备上的 GPS 位置? Android API 或 Google 位置服务 API
Posted
技术标签:
【中文标题】使用哪个 API 访问 Android 设备上的 GPS 位置? Android API 或 Google 位置服务 API【英文标题】:Which API to use to access GPS location on the Android device ? Android API or The Google Location Services API 【发布时间】:2016-01-18 13:48:02 【问题描述】:我是 android 开发的初学者。我必须使用分布式位置估计算法开发一个用于 GPS 位置估计的 Android 应用程序。它使用相邻节点的位置信息并计算当前的 GPS 位置。我通过 android 位置文档http://developer.android.com/reference/android/location/package-summary.html 了解到推荐使用 Google 位置服务 API。我在这里浏览了很多帖子,但仍然处于阴霾中。欢迎任何意见。
MainActivity.java
package com.example.sxn8837.locationapi;
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import static com.example.sxn8837.locationapi.R.*;
import static com.example.sxn8837.locationapi.R.layout.activity_main;
import static com.google.android.gms.location.LocationServices.*;
public class MainActivity extends Activity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,LocationListener
private static final String TAG = MainActivity.class.getSimpleName();
private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 1000;
private Location mLastLocation;
private GoogleApiClient mGoogleApiClient;
private boolean mRequestLocationUpdates = false ;
private LocationRequest mLocationRequest;
private static int UPDATE_INTERVAL = 10000;
private static int FASTEST_INTERVAL = 5000;
private static int DISPLACEMENT = 10;
private TextView lbLocation;
private Button btnStartLocationUpdates = (Button) findViewById(id.buttonLocationUpdates);
private Button btnShowLocation = (Button) findViewById(id.buttonShowLocation);
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(activity_main);
lbLocation = (TextView) findViewById(id.lblLocation);
if(checkPlayServices())
buildGoogleApiClient();
createLocationRequest();
btnShowLocation.setOnClickListener(new View.OnClickListener ()
public void onClick(View v)
displayLocation();
);
btnStartLocationUpdates.setOnClickListener(new View.OnClickListener()
public void onClick( View v)
togglePeriodLocationUpdates();
);
protected void onStart()
super.onStart();
if(mGoogleApiClient != null)
mGoogleApiClient.connect();
protected void onResume()
super.onResume();
checkPlayServices();
if(mGoogleApiClient.isConnected() && mRequestLocationUpdates )
startLocationUpdates();
protected void onStop()
super.onStop();
if(mGoogleApiClient.isConnected() )
mGoogleApiClient.disconnect();
protected void onPause()
super.onPause();
stopLocationUpdates();
private void displayLocation()
mLastLocation = FusedLocationApi.getLastLocation(mGoogleApiClient);
if(mLastLocation != null)
double latitude = mLastLocation.getLatitude();
double longitude = mLastLocation.getLongitude();
lbLocation.setText(latitude + "," +longitude);
else
lbLocation.setText("Couldn't get Location Updates");
private void togglePeriodLocationUpdates()
if(!mRequestLocationUpdates)
btnStartLocationUpdates.setText(getString(string.btn_stop_location_update));
mRequestLocationUpdates = true;
startLocationUpdates();
else
btnStartLocationUpdates.setText(getString(string.btn_stop_location_update));
mRequestLocationUpdates = false ;
stopLocationUpdates();
protected synchronized void buildGoogleApiClient()
mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this)
.addApi(API).build();
protected void createLocationRequest()
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
private boolean checkPlayServices()
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if(resultCode != ConnectionResult.SUCCESS)
if(GooglePlayServicesUtil.isUserRecoverableError(resultCode))
GooglePlayServicesUtil.getErrorDialog(resultCode,this,PLAY_SERVICES_RESOLUTION_REQUEST).show();
else
Toast.makeText(getApplicationContext(),"This Device is not supported", Toast.LENGTH_LONG).show();
finish();
return false ;
return true;
protected void startLocationUpdates()
FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest, (com.google.android.gms.location.LocationListener) this);
protected void stopLocationUpdates ()
final PendingResult<Status> statusPendingResult = FusedLocationApi.removeLocationUpdates(mGoogleApiClient, (com.google.android.gms.location.LocationListener) this);
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
@Override
public void onConnected(Bundle bundle)
displayLocation();
if(mRequestLocationUpdates)
startLocationUpdates();
@Override
public void onConnectionSuspended(int i)
mGoogleApiClient.connect();
@Override
public void onLocationChanged(Location location)
mLastLocation = location ;
Toast.makeText(getApplicationContext(),"Location Changed",Toast.LENGTH_SHORT).show();
displayLocation();
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
@Override
public void onProviderEnabled(String provider)
@Override
public void onProviderDisabled(String provider)
@Override
public void onConnectionFailed(ConnectionResult connectionResult)
Log.i(TAG, "Connection Failed:" + connectionResult.getErrorCode());
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_
android:layout_ android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main" tools:context=".MainActivity">
<TextView android:text="@string/lbl_you_are_at"
android:layout_
android:layout_
android:textSize = "25sp"
android:textStyle = "bold"/>
<TextView
android:id="@+id/lblLocation"
android:padding="15dp"
android:layout_
android:layout_
android:textSize = "16sp"
android:textStyle = "bold"/>
<Button
android:id="@+id/buttonShowLocation"
android:padding="20dp"
android:layout_
android:layout_
android:layout_marginTop="40dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text = "@string/btn_get_location"
android:textSize = "16sp"
android:textStyle = "bold"/>
>
<Button
android:id="@+id/buttonLocationUpdates"
android:padding="20dp"
android:layout_
android:layout_
android:layout_marginTop="60dp"
android:background="@color/common_signin_btn_default_background"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text = "@string/btn_start_location_update"
android:textSize = "16sp"
android:textStyle = "bold"/>
>
</LinearLayout>
Androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sxn8837.locationapi" >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" >
<meta-data
android:name = "com.google.android.gms.version"
android:value = "@integer/google_play_services_version"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
日志猫
11-09 20:18:13.352 3645-3645/? D/dalvikvm: Not late-enabling CheckJNI (already on)
11-09 20:18:15.012 3645-3645/com.example.sxn8837.locationapi I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
11-09 20:18:15.022 3645-3645/com.example.sxn8837.locationapi W/dalvikvm: VFY: unable to resolve virtual method 511: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
11-09 20:18:15.022 3645-3645/com.example.sxn8837.locationapi D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
11-09 20:18:15.032 3645-3645/com.example.sxn8837.locationapi I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
11-09 20:18:15.032 3645-3645/com.example.sxn8837.locationapi W/dalvikvm: VFY: unable to resolve virtual method 533: Landroid/content/res/TypedArray;.getType (I)I
11-09 20:18:15.032 3645-3645/com.example.sxn8837.locationapi D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
11-09 20:18:15.312 3645-3645/com.example.sxn8837.locationapi I/dalvikvm: Could not find method android.content.pm.PackageManager.getPackageInstaller, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zzh
11-09 20:18:15.312 3645-3645/com.example.sxn8837.locationapi W/dalvikvm: VFY: unable to resolve virtual method 438: Landroid/content/pm/PackageManager;.getPackageInstaller ()Landroid/content/pm/PackageInstaller;
11-09 20:18:15.312 3645-3645/com.example.sxn8837.locationapi D/dalvikvm: VFY: replacing opcode 0x6e at 0x000b
11-09 20:18:15.332 3645-3645/com.example.sxn8837.locationapi W/GooglePlayServicesUtil: Google Play services is missing.
11-09 20:18:15.502 3645-3645/com.example.sxn8837.locationapi D/dalvikvm: GC_FOR_ALLOC freed 158K, 11% free 3301K/3692K, paused 83ms, total 86ms
11-09 20:18:16.002 3645-3645/com.example.sxn8837.locationapi D/dalvikvm: GC_FOR_ALLOC freed 3K, 11% free 3451K/3848K, paused 23ms, total 23ms
11-09 20:18:16.022 3645-3645/com.example.sxn8837.locationapi I/dalvikvm-heap: Grow heap (frag case) to 5.278MB for 1764376-byte allocation
11-09 20:18:16.052 3645-3654/com.example.sxn8837.locationapi D/dalvikvm: GC_FOR_ALLOC freed <1K, 8% free 5174K/5572K, paused 29ms, total 29ms
11-09 20:18:16.282 3645-3645/com.example.sxn8837.locationapi W/GooglePlayServicesUtil: Google Play services is missing.
11-09 20:18:16.332 3645-3645/com.example.sxn8837.locationapi D/AndroidRuntime: Shutting down VM
11-09 20:18:16.332 3645-3645/com.example.sxn8837.locationapi W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb1adbba8)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: FATAL EXCEPTION: main
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: Process: com.example.sxn8837.locationapi, PID: 3645
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: java.lang.RuntimeException: Unable to resume activity com.example.sxn8837.locationapi/com.example.sxn8837.locationapi.MainActivity: java.lang.NullPointerException
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2774)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2803)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2238)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5001)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: Caused by: java.lang.NullPointerException
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: at com.example.sxn8837.locationapi.MainActivity.onResume(MainActivity.java:86)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: at android.app.Activity.performResume(Activity.java:5310)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2764)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2803)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2238)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5001)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
【问题讨论】:
【参考方案1】:您可以使用Google’s fused location API for Android。
Fused Location Provider 智能管理底层定位技术,并根据我们的需求为我们提供最佳位置。
为什么使用:
我们可以选择其中一个位置提供商(网络或 GPS)并请求位置更新或设置邻近警报。但是这种方法有两个主要问题:
-
如果我们需要定义精确的位置,我们必须在网络和 GPS 位置提供程序之间切换(因为 GPS 不能在室内工作)。
接近警报用于通知用户接近某个位置,这会影响电池寿命。
使用此 API 的优势
-
简单的 API:让我们指定“高精度”或“低功耗”等高级需求,而不必担心位置提供者。
立即可用:让我们的应用可以立即访问最近的最佳位置。
电源效率:最大限度地减少应用程序的电源使用。根据所有传入的位置请求和可用传感器,融合位置提供者选择最有效的方式来满足这些需求。
多功能性:满足广泛的需求,从需要高精度定位的前台使用到需要定期更新位置且功耗影响可忽略不计的后台使用。
【讨论】:
感谢您的建议!会试试看! @ Er.Rohit Sharma 我将在周末工作,使用 API 检索 GPS 坐标我刚刚有另一个查询我不确定我是否必须为此创建另一个线程。我需要一个数据库来存储通过 API 检索到的 GPS 坐标。我确实经历了各种线程,一些建议使用 SQLite、带有空间扩展/插件的 mysql 和一些 PostGIS。我需要开发一个简单的应用程序,这样一个轻量级数据库就可以了。欢迎任何输入? 如果您想将设备上的位置(本地)存储在某个数据库中,那么您可以使用 SQLite 。但是,如果您只想在数据库中存储位置而不存储其他数据,那么使用数据库连接不是最佳选择。然后你应该使用“共享首选项”。将这种小数据存储在设备中非常容易实现并且非常可靠。与 SQLite 相比,从共享首选项存储和检索数据也更快。 非常感谢。将在接下来的几天内尝试所有这些。并发布下周的情况。我会检查这个答案是否已解决。你很棒帮助。 我通过参考一些教程在线视频等尝试了以下程序代码运行没有任何错误,并且在模拟器上不幸显示 locationApi 已停止。LocationApi 是我的 android 项目的名称。我在这里附加 MainActivity.java 代码 – Shravya 54 分钟前【参考方案2】:Google 位置服务 API 是 Google Play 服务的一部分,它提供了一个更强大的高级框架,可以自动处理位置提供者、用户移动和位置准确性。它还根据您提供的功耗参数处理位置更新调度。在大多数情况下,通过使用定位服务 API,您可以获得更好的电池性能和更合适的准确度。
http://developer.android.com/guide/topics/location/strategies.html
【讨论】:
以上是关于使用哪个 API 访问 Android 设备上的 GPS 位置? Android API 或 Google 位置服务 API的主要内容,如果未能解决你的问题,请参考以下文章
Android - 用于检查哪个设备(有线耳机或 USB 耳机)正在播放音频的 API(如果两者都已连接)
应用程序如何在没有 root 的情况下访问 Android 6.0(API 级别 23)中 USB OTG 存储上的文件?
有没有办法通过官方 API 提高 Android 设备上的背光?