百度地图API的什么的,也就不多说了。官网上介绍的也比较详细,还都是中文的,仔细阅读下就行,对了,我用的是v4-2的版本。感觉只有晚上11点以后的工作效率才会翻倍啊,大家有没有这个体会啊,最近想做一款跑步的软件,当然是很简单的那种,做完了会贴上代码和大家来讨论的。
官网链接:http://developer.baidu.com/map/index.php?title=android-locsdk/guide/v4-2
package com.nsz.baidulocationtest; /** * 定位服务接口 * * @author zhou.ni * */ public interface LocationServiceInterface { /** * 初始化 */ void init(); /** * 开始定位 */ void startLocation(); /** * 停止定位 */ void stop(); }
package com.nsz.baidulocationtest; import android.content.Context; import android.location.Location; import android.util.Log; import com.baidu.location.BDLocation; import com.baidu.location.BDLocationListener; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import com.baidu.location.LocationClientOption.LocationMode; /** * 提供定位服务的实现 * * @author zhou.ni * */ public class BaiduLocationSercice implements LocationServiceInterface { private static final String TAG = "LocationUtilBaidu"; // private static final int mLocationUpdateMinTime = 20000; //定位请求间隔 private LocationClient mLocationClient = null; private BDLocationListener mBDLocationListener; public Location location; private String tempcoor = "gcj02"; // 坐标系 private LocationMode tempMode = LocationMode.Hight_Accuracy; // 高精度定位模式 Context mContext; public BaiduLocationSercice(Context mContext) { super(); this.mContext = mContext; init(); } public void init() { mLocationClient = new LocationClient(mContext); setOption(); } /** * 设置定位完成后的监听器 * @param listener */ public void registerLocationListenner(BDLocationListener listener) { this.mBDLocationListener = listener; mLocationClient.registerLocationListener(mBDLocationListener); } /** * 获得最后一次定位结果 * * @return */ public BDLocation getLastLocation() { return mLocationClient.getLastKnownLocation(); } /** * 配置定位参数 */ private void setOption() { LocationClientOption option = new LocationClientOption(); option.setLocationMode(tempMode); // 设置定位模式 option.setCoorType(tempcoor); // 返回的定位结果是百度经纬度,默认值gcj02 // option.setScanSpan(mLocationUpdateMinTime);//设置发起定位请求的间隔时间.不设置就只定位一次 option.setIsNeedAddress(true); //返回的定位结果包含地址信息 option.setNeedDeviceDirect(true);//返回的定位结果包含手机机头的方向 mLocationClient.setLocOption(option); } @Override public void startLocation() { if (mLocationClient != null) { mLocationClient.start(); mLocationClient.requestLocation(); } } @Override public void stop() { if (mLocationClient != null && mLocationClient.isStarted()) { mLocationClient.stop(); } } // private BDLocation mLocation; // /** // * 实现实位回调监听 // */ // public class MyLocationListenner implements BDLocationListener { // // @Override // public void onReceiveLocation(BDLocation location) { // if (location == null) // return ; // // Receive 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) { // sb.append("\nspeed : "); // sb.append(location.getSpeed()); // sb.append("\nsatellite : "); // sb.append(location.getSatelliteNumber()); // sb.append("\ndirection : "); // sb.append("\naddr : "); // sb.append(location.getAddrStr()); // sb.append(location.getDirection()); // } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) { // sb.append("\naddr : "); // sb.append(location.getAddrStr()); // // 运营商信息 // sb.append("\noperationers : "); // sb.append(location.getOperators()); // } // Log.i(TAG, sb.toString()); // if (location != null) { // mLocation = location; // } // // } // // } }
package com.nsz.baidulocationtest; import java.util.Timer; import java.util.TimerTask; import com.baidu.location.BDLocation; import com.baidu.location.BDLocationListener; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class AddrLocationActivity extends Activity { private BaiduLocationSercice mBaiduLocationSercice; private MyLocitionListener locationListener; TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_addr_search); initView(); } private void initView() { tv = (TextView) this.findViewById(R.id.tv); mBaiduLocationSercice = new BaiduLocationSercice(getApplicationContext()); getLocation(); } private void getLocation() { try { if( locationListener == null ){ locationListener = new MyLocitionListener(); mBaiduLocationSercice.registerLocationListenner(locationListener); } mBaiduLocationSercice.startLocation();// 开始定位 timeOutTimer.schedule(new locationTimeOutTask(), 15000);//15秒超时 } catch (Exception e) { if(e!=null) e.printStackTrace(); } } class MyLocitionListener implements BDLocationListener{ @Override public void onReceiveLocation(BDLocation locition) { if( locition == null ){ return; } mBaiduLocationSercice.stop(); timeOutTimer.cancel(); isLocationReturn = true; MyAddress addr = transform(locition); tv.setText(addr.getAddrName()); } } /** * 对象转换 * @param location * @return */ private MyAddress transform(BDLocation location) { MyAddress addr = new MyAddress(); addr.setAddrName(location.getAddrStr()); addr.setCityName(location.getCity()); addr.setLat(location.getLatitude()+""); addr.setLng(location.getLongitude()+""); return addr; } protected boolean isLocationReturn = false; private Timer timeOutTimer = new Timer(); class locationTimeOutTask extends TimerTask{ @Override public void run() { if(!isLocationReturn){ if( mBaiduLocationSercice != null ){ mBaiduLocationSercice.stop(); } } } } }
package com.nsz.baidulocationtest; import java.io.Serializable; /** * 地址模型 * * @author zhou.ni * */ public class MyAddress implements Serializable { private static final long serialVersionUID = 1L; /** 详细地址 **/ private String addrName; /** 地址所在城市名称 **/ private String cityName; /** 地址所在纬度 **/ private String lat; /** 地址所在经度 **/ private String lng; public String getAddrName() { return addrName; } public void setAddrName(String addrName) { this.addrName = addrName; } public String getCityName() { return cityName; } public void setCityName(String cityName) { this.cityName = cityName; } public String getLat() { return lat; } public void setLat(String lat) { this.lat = lat; } public String getLng() { return lng; } public void setLng(String lng) { this.lng = lng; } }
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.nsz.baidulocationtest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="20" /> <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" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <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="DUpR6inXeQAgj9Aln2QGjxy7" /> <activity android:name="com.nsz.baidulocationtest.AddrLocationActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
布局文件就不贴代码了,就是一个TextView。对了,不知道为什么,我有一款华为的Z87,是没有SM卡的,但是连接的wifi,却不能定位成功。但是另一款锤子去可以,如果大家知道的话,请告知。
效果图: