百度定位---定位提醒

定位提醒

package com.qu.weibao.demo;
import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.BDNotifyListener;
import com.baidu.location.LocationClient;
import com.qu.waibao.R;

import android.app.Activity;
import android.app.Service;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

/***
 * 实例演示位置提醒功能,由于定位本身具有空间性,所以设置当前点为提醒点,用户可以通过
 * 坐标拾取系统来自定义提醒点 http://api.map.baidu.com/lbsapi/getpoint/index.html
 * @author baidu
 *
 */
public class NotifyActivity extends Activity{

    private Button startNotify;
    private Vibrator mVibrator;
    private LocationClient mLocationClient;
    /**
     * 定位监听
     */
    private NotiftLocationListener listener;
    /**
     * 设置定位到当前位置的经纬度
     */
    private double longtitude,latitude;
    /**
     * 提醒监听
     */
    private NotifyLister mNotifyLister;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notify);
        startNotify = (Button)findViewById(R.id.notifystart);

        //初始化定位的配置信息
        listener = new NotiftLocationListener();
        mVibrator =(Vibrator)getApplicationContext().getSystemService(Service.VIBRATOR_SERVICE);
        mLocationClient  = new LocationClient(this);
        mLocationClient.registerLocationListener(listener);
        mNotifyLister = new NotifyLister();
        mLocationClient.registerNotify(mNotifyLister);
        startNotify.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if(startNotify.getText().toString().equals("开启位置提醒")){
                    mLocationClient.start();
                }else{
                    if(mNotifyLister!=null){
                        mLocationClient.removeNotifyEvent(mNotifyLister);
                    }
                }
            }
        });
    }

    //移除监听的回调
    @Override
    protected void onStop() {
        super.onStop();
        mLocationClient.removeNotifyEvent(mNotifyLister);
        mLocationClient.unRegisterLocationListener(listener);
        mLocationClient.stop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLocationClient.removeNotifyEvent(mNotifyLister);
        mLocationClient.unRegisterLocationListener(listener);
        mLocationClient.stop();
    }


    //设置提醒
    private Handler notifyHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //TODO 这里也是可以根据自己的业务来做逻辑
            //4个参数代表要位置提醒的点的坐标,具体含义依次为:纬度,经度,距离范围,坐标系类型(gcj02,gps,bd09,bd09ll)
            mNotifyLister.SetNotifyLocation(latitude,longtitude, 3000,mLocationClient.getLocOption().getCoorType());
        }

    };

    //定位监听的回调
    public class NotiftLocationListener extends BDAbstractLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            longtitude = location.getLongitude();
            latitude = location.getLatitude();
            //TODO  这里可以根据自己的业务来做操作
            notifyHandler.sendEmptyMessage(0);
        }

    }

    //提醒监听的回调
    public class NotifyLister extends BDNotifyListener{
        public void onNotify(BDLocation mlocation, float distance){
            mVibrator.vibrate(1000);//振动提醒已到设定位置附近
        }
    }
}

 

转载于:https://my.oschina.net/quguangle/blog/1863455

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值