私はAndroid開発の初心者です。分散位置推定アルゴリズムを使用して GPS 位置推定用の Android アプリを開発する必要があります。近隣ノードの位置情報を使用して、現在の GPS 位置を計算します。Google Location Services API が推奨されていることを、Android の位置に関するドキュメントhttp://developer.android.com/reference/android/location/package-summary.htmlで知りました。私はここでかなりの数の投稿を行ってきましたが、まだ霧の中にあります. 任意の入力を歓迎します。
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_width="match_parent"
android:layout_height="match_parent" 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_width="wrap_content"
android:layout_height="wrap_content"
android:textSize = "25sp"
android:textStyle = "bold"/>
<TextView
android:id="@+id/lblLocation"
android:padding="15dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize = "16sp"
android:textStyle = "bold"/>
<Button
android:id="@+id/buttonShowLocation"
android:padding="20dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
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_height="wrap_content"
android:layout_width="wrap_content"
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)