要取得定位,必須用到 Google Play Services,所以先檢查 SDK Manager,是否已安裝 Google Play Services。
build.gradle,加入
compile 'com.google.android.gms:play-services-location:11.6.2'
您的版本可能會跟我不同,因為會一直改版。
注意:
儘量不要加入所有的 Google Play Services API,也就是說,不要寫下面這種形式 —-
compile 'com.google.android.gms:play-services:11.6.2'
這樣很容易超出 65K 限制,在您按下 Sync Project with Gradle Files 時會回應錯誤,建議只加入您所需要的 Google Play Services。
開放權限
<manifest xmlns:android="http://schemas.android.com/apk/res/android" ... > ... ... <!-- 精確定位,就是 GPS --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <!-- 約略定位,就是 WI-FI --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> ... ...
activity_main.xml 版面
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <!-- 經度 --> <TextView android:id="@+id/longitude_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginStart="10dp" android:textIsSelectable="true" /> <!-- 緯度 --> <TextView android:id="@+id/latitude_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginStart="10dp" android:textIsSelectable="true" /> </LinearLayout>
GoogleApiClient 提供了 Builder 和 ConnectionCallbacks、OnConnectionFailedListener 兩個 Interface,所以我們要以 Builder 建立 GoogleApiClient 這個 instance 和 實做 ConnectionCallbacks、OnConnectionFailedListener。
範列程式:
// 記得 import 下面這幾個
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationServices;
public class LocationMainActivity extends AppCompatActivity implements ConnectionCallbacks, OnConnectionFailedListener
{
protected static final String TAG = "MainActivity";
protected GoogleApiClient mGoogleApiClient;
protected Location mLastLocation;
protected String mLatitudeLabel;
protected String mLongitudeLabel;
protected TextView mLatitudeText;
protected TextView mLongitudeText;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location_main);
mLatitudeLabel = "緯度";
mLongitudeLabel = "經度";
mLatitudeText = (TextView) findViewById((R.id.latitude_text));
mLongitudeText = (TextView) findViewById((R.id.longitude_text));
buildGoogleApiClient();
}
protected synchronized void buildGoogleApiClient()
{
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
protected void onStart()
{
super.onStart();
mGoogleApiClient.connect();
}
@Override
protected void onStop()
{
super.onStop();
if (mGoogleApiClient.isConnected())
{
mGoogleApiClient.disconnect();
}
}
// 當 GoogleApiClient 連上 Google Play Service 後要執行的動作
@Override
public void onConnected(Bundle connectionHint)
{
// 這行指令在 IDE 會出現紅線,不過仍可正常執行,可不予理會
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null)
{
mLatitudeText.setText(String.format("%s: %f", mLatitudeLabel, mLastLocation.getLatitude()));
mLongitudeText.setText(String.format("%s: %f", mLongitudeLabel, mLastLocation.getLongitude()));
}
else
{
Toast.makeText(this, "偵測不到定位,請確認定位功能已開啟。", Toast.LENGTH_LONG).show();
}
}
@Override
public void onConnectionFailed(ConnectionResult result)
{
Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode());
}
@Override
public void onConnectionSuspended(int cause)
{
Log.i(TAG, "Connection suspended");
mGoogleApiClient.connect();
}
}
資料來源:
http://oldgrayduck.blogspot.tw/2016/06/android-studio-android.html