iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 27
0
Software Development

Java 與 Kotlin 入門系列 第 27

[DAY27]GoogleMap連接方法

  • 分享至 

  • xImage
  •  

// 標記點擊事件

    private void setUpMapIfNeeded() {
        if (mMap == null) {
            mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);

            if (mMap != null) {
                //setUpMap();
                processController();
            }
        }
    }
  • // 移除地圖設定
    // ConnectionCallbacks
    @Override
    public void onConnected(Bundle bundle) {
    
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        LocationServices.FusedLocationApi.requestLocationUpdates(
                googleApiClient, locationRequest, (com.google.android.gms.location.LocationListener) MapsActivity.this);
    }
  • // 已經連線到Google Services
    
  • // 啟動位置更新服務
    
  • // 位置資訊更新的時候,應用程式會自動呼叫LocationListener.onLocationChanged
    
    // ConnectionCallbacks
    @Override
    public void onConnectionSuspended(int i) {
      
    }

    // OnConnectionFailedListener
    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
       
        int errorCode = connectionResult.getErrorCode();

        if (errorCode == ConnectionResult.SERVICE_MISSING) {
            Toast.makeText(this, R.string.google_play_service_missing,
                    Toast.LENGTH_LONG).show();
        }
    }
  • // Google Services連線中斷
  • // int參數是連線中斷的代號
  • // Google Services連線失敗
  • // ConnectionResult參數是連線失敗的資訊
  • // 裝置沒有安裝Google Play服務

上一篇
[DAY26]GoogleMap-02
下一篇
[DAY28]GoogleMap(在連接與結束)
系列文
Java 與 Kotlin 入門30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言