Guest User

Untitled

a guest
Jan 21st, 2016
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.68 KB | None | 0 0
  1. package com.lucas_chen.locationautogooglemapv2;
  2.  
  3. import android.content.Intent;
  4. import android.location.Location;
  5. import android.os.Bundle;
  6. import android.support.v4.app.FragmentActivity;
  7. import android.view.View;
  8. import android.widget.TextView;
  9. import android.widget.Toast;
  10. import com.google.android.gms.common.ConnectionResult;
  11. import com.google.android.gms.common.api.GoogleApiClient;
  12. import com.google.android.gms.location.LocationListener;
  13. import com.google.android.gms.location.LocationRequest;
  14. import com.google.android.gms.location.LocationServices;
  15. import com.google.android.gms.maps.CameraUpdateFactory;
  16. import com.google.android.gms.maps.GoogleMap;
  17. import com.google.android.gms.maps.SupportMapFragment;
  18. import com.google.android.gms.maps.model.BitmapDescriptor;
  19. import com.google.android.gms.maps.model.BitmapDescriptorFactory;
  20. import com.google.android.gms.maps.model.CameraPosition;
  21. import com.google.android.gms.maps.model.LatLng;
  22. import com.google.android.gms.maps.model.Marker;
  23. import com.google.android.gms.maps.model.MarkerOptions;
  24.  
  25. public class MapsActivity extends FragmentActivity implements GoogleApiClient.ConnectionCallbacks
  26. ,GoogleApiClient.OnConnectionFailedListener,LocationListener{
  27.  
  28.  
  29. private GoogleMap mMap;
  30. // Google API用戶端物件
  31. private GoogleApiClient googleApiClient;
  32. // Location請求物件
  33. private LocationRequest locationRequest;
  34. // 記錄目前最新的位置
  35. private Location currentLocation;
  36. // 顯示目前與儲存位置的標記物件
  37. private Marker currentMarker;
  38.  
  39. @Override
  40. protected void onCreate(Bundle savedInstanceState) {
  41. super.onCreate(savedInstanceState);
  42. setContentView(R.layout.activity_maps);
  43.  
  44.  
  45. setUpMapIfNeeded();
  46. // 建立Google API用戶端物件
  47. configGoogleApiClient();
  48. // 建立Location請求物件
  49. configLocationRequest();
  50. // 讀取記事儲存的座標
  51. Intent intent = getIntent();
  52. double lat = intent.getDoubleExtra("lat", 0.0);
  53. double lng = intent.getDoubleExtra("lng",0.0);
  54. // 如果記事已經儲存座標
  55. if(lat != 0.0 & lng != 0.0) {
  56. // 建立座標物件
  57. LatLng itemPlace = new LatLng(lat, lng);
  58. // 加入地圖標記
  59. addMarker(itemPlace, intent.getStringExtra("title"),
  60. intent.getStringExtra("datetime"));
  61. // 移動地圖
  62. moveMap(itemPlace);
  63. }
  64. else {
  65. // 連線到Google API用戶端
  66. if(!googleApiClient.isConnected()) {
  67. googleApiClient.connect();
  68. }
  69. }
  70. }
  71.  
  72. @Override
  73. protected void onResume() {
  74. super.onResume();
  75. setUpMapIfNeeded();
  76. // 連線到Google API用戶端
  77. if(!googleApiClient.isConnected() && currentMarker != null ){
  78. googleApiClient.connect();
  79. }
  80. }
  81. // 建立Google API用戶端物件
  82. private synchronized void configGoogleApiClient() {
  83. googleApiClient = new GoogleApiClient.Builder(this)
  84. .addConnectionCallbacks(this)
  85. .addOnConnectionFailedListener(this)
  86. .addApi(LocationServices.API)
  87. .build();
  88. }
  89. // 建立Location請求物件
  90. private void configLocationRequest() {
  91. locationRequest = new LocationRequest();
  92. // 設定讀取位置資訊的間隔時間為一秒(1000ms)
  93. locationRequest.setInterval(1000);
  94. // 設定讀取位置資訊最快的間隔時間為一秒(1000ms)
  95. locationRequest.setFastestInterval(1000);
  96. // 設定優先讀取高精確度的位置資訊(GPS)
  97. locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
  98. }
  99. // 移動地圖到參數指定的位置
  100. private void moveMap(LatLng place){
  101. // 建立地圖攝影機的位置物件
  102. CameraPosition cameraPosition = new CameraPosition.Builder()
  103. .target(place)
  104. .zoom(17)
  105. .build();
  106.  
  107. // 使用動畫的效果移動地圖
  108. mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
  109. }
  110. // 在地圖加入指定位置與標題的標記
  111. private void addMarker(LatLng place ,String title,String snippet){
  112. BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher);
  113.  
  114. MarkerOptions markerOptions = new MarkerOptions();
  115. markerOptions.position(place)
  116. .title(title)
  117. .snippet(snippet)
  118. .icon(icon);
  119.  
  120. mMap.addMarker(markerOptions);
  121. }
  122. // ConnectionCallbacks
  123. @Override
  124. public void onConnected(Bundle bundle) {
  125. // 已經連線到Google Services
  126. // 啟動位置更新服務
  127. // 位置資訊更新的時候,應用程式會自動呼叫LocationListener.onLocationChanged
  128. LocationServices.FusedLocationApi.requestLocationUpdates(
  129. googleApiClient, locationRequest, MapsActivity.this);
  130. }
  131. // ConnectionCallbacks
  132. @Override
  133. public void onConnectionSuspended(int i) {
  134. // Google Services連線中斷
  135. // int參數是連線中斷的代號
  136. }
  137. // OnConnectionFailedListener
  138. @Override
  139. public void onConnectionFailed(ConnectionResult connectionResult) {
  140. // Google Services連線失敗
  141. // ConnectionResult參數是連線失敗的資訊
  142. int errorCode = connectionResult.getErrorCode();
  143.  
  144. // 裝置沒有安裝Google Play服務
  145. if(errorCode == connectionResult.SERVICE_MISSING){
  146. Toast.makeText(this, R.string.google_play_service_missing,
  147. Toast.LENGTH_LONG).show();
  148. }
  149. }
  150. // LocationListener
  151. @Override
  152. public void onLocationChanged(Location location) {
  153. // 位置改變
  154. // Location參數是目前的位置
  155. currentLocation = location;
  156. LatLng latLng = new LatLng(currentLocation.getLatitude(),currentLocation.getLongitude());
  157.  
  158. // 設定目前位置的標記
  159. if (currentMarker == null ){
  160. currentMarker = mMap.addMarker(new MarkerOptions().position(latLng));
  161. }
  162.  
  163. else {
  164. currentMarker.setPosition(latLng);
  165. }
  166. // 移動地圖到目前的位置
  167. moveMap(latLng);
  168. }
  169. //提升效能
  170. @Override
  171. protected void onPause() {
  172. super.onPause();
  173.  
  174. // 移除位置請求服務
  175. if (googleApiClient.isConnected()){
  176. LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
  177. }
  178. }
  179. @Override
  180. protected void onStop() {
  181. super.onStop();
  182.  
  183. // 移除Google API用戶端連線
  184. if(googleApiClient.isConnected()){
  185. googleApiClient.disconnect();
  186. }
  187. }
  188. //這是原本的
  189. private void setUpMapIfNeeded() {
  190. // Do a null check to confirm that we have not already instantiated the map.
  191. if (mMap == null) {
  192. // Try to obtain the map from the SupportMapFragment.
  193. mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
  194. .getMap();
  195. // Check if we were successful in obtaining the map.
  196. if (mMap != null) {
  197. setUpMap();
  198. }
  199. }
  200. }
  201. private void setUpMap() {
  202. //原本的--
  203. //mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
  204. // 建立位置的座標物件
  205. LatLng place = new LatLng(25.033408, 121.564099);
  206. moveMap(place);
  207. }
  208.  
  209.  
  210. }
Advertisement
Add Comment
Please, Sign In to add comment