現在FusedLocationApiで、アプリを作ったのですが、エミュレーターで手動で位置を操作すると、onLocationChanged()が呼び出されますが、実際に1kmほど歩いても、
実験した大学のある一点をさしたまま、ピンが移動されることはありませんでした。
基地局がそこにしかないからそのような表示になるのでしょうか?
どうすればいいでしょうか?
java
1 2 3public class LocationActivity extends FragmentActivity implements 4 GoogleApiClient.ConnectionCallbacks, 5 GoogleApiClient.OnConnectionFailedListener, 6 LocationListener { 7 8 9 10 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_main); 15 16 17 18 // LocationRequest を生成して精度、インターバルを設定 19 locationRequest = LocationRequest.create(); 20 21 locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 22 locationRequest.setInterval(50000); 23 locationRequest.setFastestInterval(1000); 24 locationRequest.setSmallestDisplacement(1); 25 26 // locationRequest.setInterval(5000).setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 27 //locationRequest.setExpirationDuration();このリクエストの期間をミリ秒単位 28 //locationRequest.setExpirationTime();起動後の要求の有効期限 29 //locationRequest.setMaxWaitTime();複数の場所を一度に配信できます。 30 //locationRequest.setNumUpdates();新しい場所が1つだけ必要な場合 31 //locationRequest.setPriority(1);//要求の優先順位を設定 32 33 34 fusedLocationProviderApi = LocationServices.FusedLocationApi; 35 36 mGoogleApiClient = new GoogleApiClient.Builder(this) 37 .addApi(LocationServices.API) 38 .addConnectionCallbacks(this) 39 .addOnConnectionFailedListener(this) 40 .build(); 41 42 // 測位開始 43 Button buttonStart = (Button)findViewById(R.id.button_start); 44 buttonStart.setOnClickListener(new View.OnClickListener() { 45 @Override 46 public void onClick(View v) { 47 startFusedLocation(); 48 } 49 }); 50 51 // 測位終了 52 Button buttonStop = (Button)findViewById(R.id.button_stop); 53 buttonStop.setOnClickListener(new View.OnClickListener() { 54 @Override 55 public void onClick(View v) { 56 stopFusedLocation(); 57 } 58 }); 59 60 } 61 62 63 private void startFusedLocation(){ 64 if (!mResolvingError) { 65 mGoogleApiClient.connect(); 66 67 } else { 68 } 69 70 } 71 72 private void stopFusedLocation(){ 73 // Disconnecting the client invalidates it. 74 mGoogleApiClient.disconnect(); 75 } 76 77 @Override 78 protected void onStart() { 79 super.onStart(); 80 81 } 82 83 @Override 84 protected void onStop() { 85 super.onStop(); 86 stopFusedLocation(); 87 } 88 89 90 @Override 91 public void onConnected(Bundle bundle) { 92 93 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 94 return; 95 } 96 Location currentLocation = fusedLocationProviderApi.getLastLocation(mGoogleApiClient); 97 98 if (currentLocation != null && currentLocation.getTime() > 20000) { 99 location = currentLocation; 100 101 textLog += "---------- onConnected \n"; 102 textLog += "Latitude=" + String.valueOf(location.getLatitude()) + "\n"; 103 textLog += "Longitude=" + String.valueOf(location.getLongitude()) + "\n"; 104 textLog += "Accuracy=" + String.valueOf(location.getAccuracy()) + "\n"; 105 textLog += "Altitude=" + String.valueOf(location.getAltitude()) + "\n"; 106 textLog += "Time=" + String.valueOf(location.getTime()) + "\n"; 107 textLog += "Speed=" + String.valueOf(location.getSpeed()) + "\n"; 108 textLog += "Bearing=" + String.valueOf(location.getBearing()) + "\n"; 109 textView.setText(textLog); 110 111 112 } else { 113 // バックグラウンドから戻ってしまうと例外が発生する場合がある 114 try { 115 116 fusedLocationProviderApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this); 117 Executors.newScheduledThreadPool(1).schedule(new Runnable() { 118 @Override 119 public void run() { 120 fusedLocationProviderApi.removeLocationUpdates(mGoogleApiClient, LocationActivity.this); 121 } 122 }, 60000, TimeUnit.MILLISECONDS); 123 124 textLog += "onConnected(), requestLocationUpdates \n"; 125 textView.setText(textLog); 126 127 } catch (Exception e) { 128 e.printStackTrace(); 129 finish(); 130 } 131 } 132 } 133 134 135 @Override 136 public void onLocationChanged(Location location) { 137 lastLocationTime = location.getTime() - lastLocationTime; 138 139 textLog += "2---------- onLocationChanged \n"; 140 textLog += "2Latitude=" + String.valueOf(location.getLatitude()) + "\n"; 141 textLog += "2Longitude=" + String.valueOf(location.getLongitude()) + "\n"; 142 textLog += "2Accuracy=" + String.valueOf(location.getAccuracy()) + "\n"; 143 textLog += "2Altitude=" + String.valueOf(location.getAltitude()) + "\n"; 144 textLog += "2Time=" + String.valueOf(location.getTime()) + "\n"; 145 textLog += "2Speed=" + String.valueOf(location.getSpeed()) + "\n"; 146 textLog += "2Bearing=" + String.valueOf(location.getBearing()) + "\n"; 147 textLog += "2time= " + String.valueOf(lastLocationTime) + " msec \n"; 148 textView.setText(textLog); 149 } 150 151 @Override 152 public void onConnectionSuspended(int i) { 153 } 154 155 @Override 156 public void onConnectionFailed(ConnectionResult connectionResult) { 157 158 if (mResolvingError) { 159 160 return; 161 } else if (connectionResult.hasResolution()) { 162 163 } else { 164 mResolvingError = true; 165 } 166 }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/07/28 22:46