码迷,mamicode.com
首页 > 移动开发 > 详细

android 百度地图 定位功能

时间:2015-07-27 18:57:50      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:android

废话不多说 直接新建一个新android项目:location,然后花一分钟申请一个key,然后就是把百度定位demo抄一下就行

1:首先在AndroidManifest.xml中添加权限

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
    </uses-permission>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >
    </uses-permission>
    <uses-permission android:name="android.permission.READ_PHONE_STATE" >
    </uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
    </uses-permission>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" >
    </uses-permission>
    <uses-permission android:name="android.permission.READ_LOGS" >
    </uses-permission>
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />

2:在AndroidManifest.xml文件中使用自己写的Application 在application节点中加上一行代码

android:name="com.baidu.baidulocationdemo.LocationApplication" 

3:在application节点上 复制如下内容

<service
            android:name="com.baidu.location.f"
            android:enabled="true"
            android:process=":remote" >
            <intent-filter>
                <action android:name="com.baidu.location.service_v2.2" >
                </action>
            </intent-filter>
        </service>
        <!-- meta-data需要写在application中 -->
        <meta-data
            android:name="com.baidu.lbsapi.API_KEY"
            android:value="xAxmR5WgrssC9ATV8gZOvq96" />

在这注意需要把换成自己申请的key

4:配置就算ok了,那么就写代码么,谁叫我们是码农呢? 首先看下自己定义的类MyApplication.java

package com.example.location;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.Poi;
import android.app.Application;
import android.app.Service;
import android.os.Vibrator;
import android.util.Log;
import android.widget.TextView;
import java.util.List;

public class MyApplication extends Application {
    public LocationClient mLocationClient;
    public MyLocationListener mMyLocationListener;
    public TextView mLocationResult,logMsg;
    public TextView trigger,exit;
    public Vibrator mVibrator;
		@Override
		public void onCreate() {
			super.onCreate();
			 mLocationClient = new LocationClient(this.getApplicationContext());
	        mMyLocationListener = new MyLocationListener();
	        mLocationClient.registerLocationListener(mMyLocationListener);
	        mVibrator =(Vibrator)getApplicationContext().getSystemService(Service.VIBRATOR_SERVICE);
		}
		
		 /**
	     * 实现实时位置回调监听
	     */
	    public class MyLocationListener implements BDLocationListener {
	        @Override
	        public void onReceiveLocation(BDLocation location) {
	            StringBuffer sb = new StringBuffer(256);
	            sb.append("time : ");
	            sb.append(location.getTime());
	            sb.append("\nerror code : ");
	            sb.append(location.getLocType());
	            sb.append("\nlatitude : ");
	            sb.append(location.getLatitude());
	            sb.append("\nlontitude : ");
	            sb.append(location.getLongitude());
	            sb.append("\nradius : ");
	            sb.append(location.getRadius());
	            if (location.getLocType() == BDLocation.TypeGpsLocation){// GPS定位结果
	                sb.append("\nspeed : ");
	                sb.append(location.getSpeed());// 单位:公里每小时
	                sb.append("\nsatellite : ");
	                sb.append(location.getSatelliteNumber());
	                sb.append("\nheight : ");
	                sb.append(location.getAltitude());// 单位:米
	                sb.append("\ndirection : ");
	                sb.append(location.getDirection());
	                sb.append("\naddr : ");
	                sb.append(location.getAddrStr());
	                sb.append("\ndescribe : ");
	                sb.append("gps定位成功");

	            } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){// 网络定位结果
	                sb.append("\naddr : ");
	                sb.append(location.getAddrStr());
	                //运营商信息
	                sb.append("\noperationers : ");
	                sb.append(location.getOperators());
	                sb.append("\ndescribe : ");
	                sb.append("网络定位成功");
	            } else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 离线定位结果
	                sb.append("\ndescribe : ");
	                sb.append("离线定位成功,离线定位结果也是有效的");
	            } else if (location.getLocType() == BDLocation.TypeServerError) {
	                sb.append("\ndescribe : ");
	                sb.append("服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因");
	            } else if (location.getLocType() == BDLocation.TypeNetWorkException) {
	                sb.append("\ndescribe : ");
	                sb.append("网络不同导致定位失败,请检查网络是否通畅");
	            } else if (location.getLocType() == BDLocation.TypeCriteriaException) {
	                sb.append("\ndescribe : ");
	                sb.append("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机");
	            }
	            sb.append("\nlocationdescribe : ");// 位置语义化信息
	            sb.append(location.getLocationDescribe());
	            List<Poi> list = location.getPoiList();// POI信息
	            if (list != null) {
	                sb.append("\npoilist size = : ");
	                sb.append(list.size());
	                for (Poi p : list) {
	                    sb.append("\npoi= : ");
	                    sb.append(p.getId() + " " + p.getName() + " " + p.getRank());
	                }
	            }
	            logMsg(sb.toString());
	            Log.i("BaiduLocationApiDem", sb.toString());
	        }
	    }
	    /**
	     * 显示请求字符串
	     * @param str
	     */
	    public void logMsg(String str) {
	        try {
	            if (mLocationResult != null)
	                mLocationResult.setText(str);
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	    }
}

这里是直接从百度定位sdk demo中复制过来的,这个不需要去敲,因为它没啥技术和逻辑可言.

5:MainActivity.java

package com.example.location;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {
	private TextView tv_locaiton_result;
	private LocationClient mLocationClient;
	private LocationMode tempMode = LocationMode.Hight_Accuracy;
	private String tempcoor="gcj02";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mLocationClient = ((MyApplication)getApplication()).mLocationClient;
		tv_locaiton_result = (TextView) findViewById(R.id.tv_locaiton_result);
		((MyApplication)getApplication()).mLocationResult = tv_locaiton_result;
		initLocation();
	}
	 private void initLocation(){
	        LocationClientOption option = new LocationClientOption();
	        option.setLocationMode(tempMode);//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
	        option.setCoorType(tempcoor);//可选,默认gcj02,设置返回的定位结果坐标系,
	        int span=5000;
	        option.setScanSpan(span);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
	        option.setOpenGps(true);//可选,默认false,设置是否使用gps
	        option.setLocationNotify(true);//可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果
	        option.setIgnoreKillProcess(true);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死
	        option.setEnableSimulateGps(false);//可选,默认false,设置是否需要过滤gps仿真结果,默认需要
	        option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”
	        option.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到
	        mLocationClient.setLocOption(option);
	        mLocationClient.start();
	    }
}

ok,定位就好了 不过这是定位获取到地址,地图上显示  下次弄  



版权声明:本文为博主原创文章,未经博主允许不得转载。

android 百度地图 定位功能

标签:android

原文地址:http://blog.csdn.net/coderinchina/article/details/47086387

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!