Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.lucas_chen.locationautogooglemapv2;
- import android.content.Intent;
- import android.location.Location;
- import android.os.Bundle;
- import android.support.v4.app.FragmentActivity;
- import android.view.View;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.google.android.gms.common.ConnectionResult;
- import com.google.android.gms.common.api.GoogleApiClient;
- import com.google.android.gms.location.LocationListener;
- import com.google.android.gms.location.LocationRequest;
- import com.google.android.gms.location.LocationServices;
- import com.google.android.gms.maps.CameraUpdateFactory;
- import com.google.android.gms.maps.GoogleMap;
- import com.google.android.gms.maps.SupportMapFragment;
- import com.google.android.gms.maps.model.BitmapDescriptor;
- 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;
- public class MapsActivity extends FragmentActivity implements GoogleApiClient.ConnectionCallbacks
- ,GoogleApiClient.OnConnectionFailedListener,LocationListener{
- private GoogleMap mMap;
- // Google API用戶端物件
- private GoogleApiClient googleApiClient;
- // Location請求物件
- private LocationRequest locationRequest;
- // 記錄目前最新的位置
- private Location currentLocation;
- // 顯示目前與儲存位置的標記物件
- private Marker currentMarker;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_maps);
- setUpMapIfNeeded();
- // 建立Google API用戶端物件
- configGoogleApiClient();
- // 建立Location請求物件
- configLocationRequest();
- // 讀取記事儲存的座標
- Intent intent = getIntent();
- double lat = intent.getDoubleExtra("lat", 0.0);
- double lng = intent.getDoubleExtra("lng",0.0);
- // 如果記事已經儲存座標
- if(lat != 0.0 & lng != 0.0) {
- // 建立座標物件
- LatLng itemPlace = new LatLng(lat, lng);
- // 加入地圖標記
- addMarker(itemPlace, intent.getStringExtra("title"),
- intent.getStringExtra("datetime"));
- // 移動地圖
- moveMap(itemPlace);
- }
- else {
- // 連線到Google API用戶端
- if(!googleApiClient.isConnected()) {
- googleApiClient.connect();
- }
- }
- }
- @Override
- protected void onResume() {
- super.onResume();
- setUpMapIfNeeded();
- // 連線到Google API用戶端
- if(!googleApiClient.isConnected() && currentMarker != null ){
- googleApiClient.connect();
- }
- }
- // 建立Google API用戶端物件
- private synchronized void configGoogleApiClient() {
- googleApiClient = new GoogleApiClient.Builder(this)
- .addConnectionCallbacks(this)
- .addOnConnectionFailedListener(this)
- .addApi(LocationServices.API)
- .build();
- }
- // 建立Location請求物件
- private void configLocationRequest() {
- locationRequest = new LocationRequest();
- // 設定讀取位置資訊的間隔時間為一秒(1000ms)
- locationRequest.setInterval(1000);
- // 設定讀取位置資訊最快的間隔時間為一秒(1000ms)
- locationRequest.setFastestInterval(1000);
- // 設定優先讀取高精確度的位置資訊(GPS)
- locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
- }
- // 移動地圖到參數指定的位置
- private void moveMap(LatLng place){
- // 建立地圖攝影機的位置物件
- CameraPosition cameraPosition = new CameraPosition.Builder()
- .target(place)
- .zoom(17)
- .build();
- // 使用動畫的效果移動地圖
- mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
- }
- // 在地圖加入指定位置與標題的標記
- private void addMarker(LatLng place ,String title,String snippet){
- BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher);
- MarkerOptions markerOptions = new MarkerOptions();
- markerOptions.position(place)
- .title(title)
- .snippet(snippet)
- .icon(icon);
- mMap.addMarker(markerOptions);
- }
- // ConnectionCallbacks
- @Override
- public void onConnected(Bundle bundle) {
- // 已經連線到Google Services
- // 啟動位置更新服務
- // 位置資訊更新的時候,應用程式會自動呼叫LocationListener.onLocationChanged
- LocationServices.FusedLocationApi.requestLocationUpdates(
- googleApiClient, locationRequest, MapsActivity.this);
- }
- // ConnectionCallbacks
- @Override
- public void onConnectionSuspended(int i) {
- // Google Services連線中斷
- // int參數是連線中斷的代號
- }
- // OnConnectionFailedListener
- @Override
- public void onConnectionFailed(ConnectionResult connectionResult) {
- // Google Services連線失敗
- // ConnectionResult參數是連線失敗的資訊
- int errorCode = connectionResult.getErrorCode();
- // 裝置沒有安裝Google Play服務
- if(errorCode == connectionResult.SERVICE_MISSING){
- Toast.makeText(this, R.string.google_play_service_missing,
- Toast.LENGTH_LONG).show();
- }
- }
- // LocationListener
- @Override
- public void onLocationChanged(Location location) {
- // 位置改變
- // Location參數是目前的位置
- currentLocation = location;
- LatLng latLng = new LatLng(currentLocation.getLatitude(),currentLocation.getLongitude());
- // 設定目前位置的標記
- if (currentMarker == null ){
- currentMarker = mMap.addMarker(new MarkerOptions().position(latLng));
- }
- else {
- currentMarker.setPosition(latLng);
- }
- // 移動地圖到目前的位置
- moveMap(latLng);
- }
- //提升效能
- @Override
- protected void onPause() {
- super.onPause();
- // 移除位置請求服務
- if (googleApiClient.isConnected()){
- LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
- }
- }
- @Override
- protected void onStop() {
- super.onStop();
- // 移除Google API用戶端連線
- if(googleApiClient.isConnected()){
- googleApiClient.disconnect();
- }
- }
- //這是原本的
- private void setUpMapIfNeeded() {
- // Do a null check to confirm that we have not already instantiated the map.
- if (mMap == null) {
- // Try to obtain the map from the SupportMapFragment.
- mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
- .getMap();
- // Check if we were successful in obtaining the map.
- if (mMap != null) {
- setUpMap();
- }
- }
- }
- private void setUpMap() {
- //原本的--
- //mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
- // 建立位置的座標物件
- LatLng place = new LatLng(25.033408, 121.564099);
- moveMap(place);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment