package com.njnu.kai.practice.location;

import android.os.Bundle;
import android.view.View;

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.njnu.kai.support.BaseTestListFragment;
import com.njnu.kai.support.TestFunction;

/**
 * Created by kai
 * since 16/11/7
 */
public class AMapLocationTestFragment extends BaseTestListFragment {

    private AMapLocationClient mLocationClient;
    private AMapLocationClientOption mLocationOption = new AMapLocationClientOption();

    private boolean mLocationStarted;

    @TestFunction("start or stop location")
    public void onStartLocation() {
        if (mLocationClient.isStarted()) {
            mLocationClient.stopLocation();
        } else {
            mLocationClient.startLocation();
        }

//        if (mLocationStarted) {
//            mLocationClient.stopLocation();
//        } else {
//            mLocationClient.startLocation();
//        }
//        mLocationStarted = !mLocationStarted;
    }


    AMapLocationListener mLocationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation loc) {
            if (null != loc) {
                //解析定位结果
                setResult(LocationUtils.getLocationStr(loc));
            } else {
                setResult("定位失败，loc is null");
            }
        }
    };


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initLocation();
    }

    @Override
    public void onDestroy() {
        if (mLocationClient != null) {
            mLocationClient.onDestroy();
        }
        super.onDestroy();
    }

    private void initLocation(){
        makeDefaultOption();
        //初始化client
        mLocationClient = new AMapLocationClient(getContext());
        //设置定位参数
//        mLocationOption.setOnceLocationLatest(true);
        mLocationClient.setLocationOption(mLocationOption);
        // 设置定位监听
        mLocationClient.setLocationListener(mLocationListener);
    }

    private AMapLocationClientOption makeDefaultOption(){
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);//可选，设置定位模式，可选的模式有高精度、仅设备、仅网络。默认为高精度模式
        mLocationOption.setGpsFirst(false);//可选，设置是否gps优先，只在高精度模式下有效。默认关闭
        mLocationOption.setHttpTimeOut(30000);//可选，设置网络请求超时时间。默认为30秒。在仅设备模式下无效
        mLocationOption.setInterval(5000);//可选，设置定位间隔。默认为2秒
        mLocationOption.setNeedAddress(true);//可选，设置是否返回逆地理地址信息。默认是true
        mLocationOption.setOnceLocation(false);//可选，设置是否单次定位。默认是false
        mLocationOption.setOnceLocationLatest(false);//可选，设置是否等待wifi刷新，默认为false.如果设置为true,会自动变为单次定位，持续定位时不要使用
        AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);//可选， 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP
        mLocationOption.setSensorEnable(false);//可选，设置是否使用传感器。默认是false
        return mLocationOption;
    }
}

@@ -886,32 +886,306 @@
 startLocation();
+   //addGeoFenceAlert radius-%E5%9C%B0%E7%90%86%E5%9B%B4%E6%A0%8F%E5%8C%BA%E5%9F%9F%E7%9A%84%E5%8D%8A%E5%BE%84%EF%BC%8C%E5%8D%95%E4%BD%8D%E4%B8%BA%E7%B1%B3%EF%BC%8C%E5%9B%B4%E6%A0%8F%E5%8D%8A%E5%BE%84%E5%9C%A8100%E7%B1%B3%EF%BC%8D1000%E7%B1%B3%E4%B9%8B%E9%97%B4%0A            //PendingIntent %E7%9A%84 Bundle %E5%86%85%E5%AE%B9%E4%B8%AD%E5%8C%85%E6%8B%AC status %E5%AD%97%E6%AE%B5%EF%BC%8C%E5%BD%93 status %E4%B8%BA0%E6%97%B6%E8%A1%A8%E7%A4%BA%E8%AE%BE%E5%A4%87%E4%BB%8E%E5%8C%BA%E5%9F%9F%E4%B8%AD%E7%A6%BB%E5%BC%80%EF%BC%8C%E4%B8%BA1%E6%97%B6%E8%A1%A8%E7%A4%BA%E8%AE%BE%E5%A4%87%E8%BF%9B%E5%85%A5%E8%AF%A5%E5%8C%BA%E5%9F%9F%E3%80%82%0A            //%E6%96%87%E6%A1%A3%E4%B8%AD%E8%AF%B4%E6%98%AFstatus%E5%AD%97%E6%AE%B5,%E4%BD%86%E5%AE%98%E6%96%B9demo%E4%B8%AD%E6%98%AFint status = bundle.getInt(%22event%22); String geoFenceId = bundle.getString(%22fenceid%22);
 %0A        %7D%0A%0A//  

add comment