Java tutorial
/* * Copyright 2016 Alex_ZHOU * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.alex.vmandroid.display.voice.db; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.util.Log; import com.alex.vmandroid.R; import com.alex.vmandroid.base.BaseActivity; import com.amap.api.location.AMapLocation; import com.amap.api.location.AMapLocationClient; import com.amap.api.location.AMapLocationClientOption; import com.amap.api.location.AMapLocationListener; import com.amap.api.maps.AMap; import com.amap.api.maps.LocationSource; import com.amap.api.maps.MapView; public class RecordDBActivity extends BaseActivity implements LocationSource, AMap.OnMapLoadedListener, AMapLocationListener { public static final String TAG = RecordDBActivity.class.getName(); private AMapLocationClient mLocationClient; private OnLocationChangedListener mListener; private MapView mMapView; @Override protected void onDestroy() { mMapView.onDestroy(); super.onDestroy(); } @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.i(TAG, "onCreate: "); setContentView(R.layout.activity_voice_db_record_db); mMapView = (MapView) findViewById(R.id.record_db_map_view); mMapView.onCreate(savedInstanceState);// ? AMap mAMap = mMapView.getMap(); mAMap.setLocationSource(this);// ?? mAMap.setOnMapLoadedListener(this); //mAMap.getUiSettings().setMyLocationButtonEnabled(true);// ?? mAMap.setMyLocationEnabled(true);// true????false???????false RecordDBFragment fragment = RecordDBFragment.newInstance(); FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.replace(R.id.record_db_fl, fragment); transaction.commit(); } @Override public void activate(OnLocationChangedListener onLocationChangedListener) { mListener = onLocationChangedListener; if (mLocationClient == null) { mLocationClient = new AMapLocationClient(getApplicationContext()); AMapLocationClientOption mLocationOption = new AMapLocationClientOption(); //?? mLocationClient.setLocationListener(this); //?? mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); //?? mLocationClient.setLocationOption(mLocationOption); // ??????? // ?????2000ms?stopLocation()??? // ????onDestroy() // ?????stopLocation()?sdk mLocationClient.startLocation(); } } @Override public void deactivate() { mListener = null; if (mLocationClient != null) { mLocationClient.stopLocation(); mLocationClient.onDestroy(); } mLocationClient = null; } @Override public void onMapLoaded() { } @Override public void onLocationChanged(AMapLocation amapLocation) { if (mListener != null && amapLocation != null) { if (amapLocation.getErrorCode() == 0) { // mCity = amapLocation.getCity(); // mCityTextView.setText(mCity); // // mPresenter.searchWeatherRecord(getContext(), amapLocation.getCity()); mListener.onLocationChanged(amapLocation);// ?? } else { String errText = "?," + amapLocation.getErrorCode() + ": " + amapLocation.getErrorInfo(); Log.e("AmapErr", errText); //mLocationErrText.setVisibility(View.VISIBLE); //mLocationErrText.setText(errText); } } } }