快捷搜索:  as  test  1111  test aNd 8=8  test++aNd+8=8  as++aNd+8=8  as aNd 8=8

伟德官伟德官网手机版_酒文化网进入



实践需求:经由过程GPS或者3G/2G收集(基站)定位,并将定位的结果(我当前的位置)标注在百度舆图上。

当用户点击舆图上代表“我确当前位置”的点时,显示当前我所在位置的具体信息。

一、经由过程GPS或者3G/2G收集(基站)定位。

基于 Android百度舆图之定位SDK(v3.1)应用示例(一) 和 Android百度舆图之定位SDK(v3.1)应用示例(二) 这两篇的积淀,导入百度供给的so和jar文件及在AndroidManifest.xml文件添加响应的设置设置设备摆设摆设等等就不罗嗦了,不懂得的请查看前面的。

留意:因为要应用百度的供给的定位办事,以是在AndroidManifest.xml文件中必然要记得添加Service。(若没添加,运行不报错,也没有任何效果。)

设置定位参数,注册定位结果接管器。代码如下:

mLocationClient = new LocationClient(this.getApplicationContext());

mLocationListener = new MyLocationListener();mLocationClient.registerLocationListener(mLocationListener);

LocationClientOption locationOption = new LocationClientOption();

locationOption.setOpenGps(true);locationOption.setCoorType("bd09ll");

locationOption.setPriority(LocationClientOption.GpsFirst);locationOption.setAddrType("all");

locationOption.setProdName("经由过程GPS定位");mLocationClient.setLocOption(locationOption);

Log.i(TAG, "BaiduMapMyLocationActivity 开启定位");

mLocationClient.start();

二、将定位的结果(我当前的位置)标注在百度舆图上。

1、在前面为 mLocationClient工具注册了BDLocationListener,自定义类实现BDLocationListener接口,处置惩罚定位获得的结果,详细代码如下:

class MyLocationListener implements BDLocationListener {

@Overridepublic void onReceiveLocation(BDLocation location) {

if (location == null) {return;

}

Log.i(TAG, "BaiduMapMyLocationActivity onReceiveLocation()");

// TODO 为方便测试查看testLog(location);

// 在舆图上标注定位获得我当前的位置

markLocation(location);

mBDLocation = location;}

@Override

public void onReceivePoi(BDLocation arg0) {

}}

2、构建一个认真在舆图上显示用户当前位置的Overlay工具及为该Overlay工具设置数据。

mLocationOverlay = new LocationOverlay(mMapView);

// 百度官方API文档解释:打开指南针,然则我试验感觉默认指南针便是打开的// mLocationOverlay.enableCompass();

locData = new LocationData();mLocationOverlay.setData(locData);

3、将认真在舆图上显示用户当前位置的Overlay工具添加到覆盖物列表中,并刷新舆图视图view。

mMapView.getOverlays().add(mLocationOverlay);

mMapView.refresh();

4、将我确当前位置移动到舆图的中间点

mMapController.animateTo(new GeoPoint(

(int) (locData.latitude * 1e6),(int) (locData.longitude * 1e6)));

三、当用户点击舆图上代表“我确当前位置”的点时,显示当前我所在位置的具体信息。自定义类承袭MyLocationOverlay,重写 protected boolean dispatchTap() {}措施,在措施体中添加响应的处置惩罚即可。

class LocationOverlay extends MyLocationOverlay {

public LocationOverlay(MapView mapView) {

super(mapView);}

// 处置惩罚在“我的位置”坐标上的点击事故。

@Overrideprotected boolean dispatchTap() {

Log.i(TAG, "BaiduMapMyLocationActivity 处置惩罚在“我的位置”坐标上的点击事故 dispatchTap()");

if (mBDLocation != null) {showLocation(mBDLocation);

}

return super.dispatchTap();}

}

四、运行效果图

点击舆图上的蓝色指南针图标,效果图如下:

点击上图玄色区域,该区域消掉;点击“从新定位”按钮,从新获取我当前的位置。

注:我在屋子采纳的是Wifi收集定位,以是地址信息不为null。

五、我发明存在的问题:

1、百度最新API与API文档中描述个别不符

a. 这行mLocationOverlay.enableCompass();代码,百度官方API文档解释:打开指南针,然则我颠末测试,感觉默认指南针便是打开的。

b. 舆图引擎治理类BMapManager,API文档中描述:boolean start() // 开启百度舆图API;boolean stop() // 终止百度舆图API,调用此函数后,不会再发生回调。实际上应用最新的SDK中的BMapManager类创建的工具已经没有什么start、stop措施。

2、当调用 mLocationClient.start(); (LocationClient类实例化后,调用它的start措施。)后,会从新履行一次BaiduMapApplication(# class BaiduMapApplication extends Application{})的onCreate()回调措施。今朝道没发明影相应用,然则我小我感觉:若之前已履行过,也便是说初始化过舆图引擎类,就不应该再次初始化。不知道这个算是个问题不?官方给的demo里也存在这个问题,怎么办理?(资本挥霍应用)

六、完备代码:

AndroidManifest.xml文件设置设置设备摆设摆设:

xml version="1.0" encoding="utf-8"?>

manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.easi.baidu.map"

android:versionCode="1"android:versionName="1.0" >

uses-sdk android:minSdkVersion="8" />

application

android:icon="@drawable/ic_launcher"android:label="@string/app_name"

android:name=".BaiduMapApplication" >activity

android:configChanges="keyboardHidden|orientation"android:label="@string/app_name"

android:name=".BaiduMapMyLocationActivity"android:screenOrientation="sensor" >

intent-filter >action android:name="android.intent.action.MAIN" />

category android:name="android.intent.category.LAUNCHER" />

intent-filter>activity>

service

android:enabled="true"android:name="com.baidu.location.f"

android:process=":remote" />application>

uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >

uses-permission>uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >

uses-permission>uses-permission android:name="android.permission.INTERNET" >

uses-permission>uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >

uses-permission>uses-permission android:name="android.permission.ACCESS_WIFI_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.CALL_PHONE" >

uses-permission>

permission android:name="android.permission.BAIDU_LOCATION_SERVICE" >permission>

uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE" >

uses-permission>uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >

uses-permission>uses-permission android伟德官伟德官网手机版:name="android.permission.ACCES_MOCK_LOCATION" >

uses-permission>uses-permission android:name="android.permission.ACCESS_GPS" />

supports-screens

android:anyDensity="true"android:largeScreens="true"

android:normalScreens="false"android:resizeable="true"

android:smallScreens="true" />

manifest>

初始化舆图引擎工具,寄放共享资本,承袭自Application。

package com.easi.baidu.map;

import android.app.Application;

import android.content.Context;import android.util.Log;

import android.widget.Toast;

import com.baidu.mapapi.BMapManager;import com.baidu.mapapi.MKGeneralListener;

import com.baidu.mapapi.map.MKEvent;

/奸淫 初始化舆图引擎工具,寄放共享资本。

* 必要在AndroidMinifest.xml文件中的application标签添加name属性。* {@link #}

* @author android_ls*/

public class BaiduMapApplication extends Application {

/奸淫 申请的百度舆图API Key密钥

*/private static final String BAIDU_MAP_KEY = "8BB7F0E5C9C77BD6B9B655DB928B74B6E2D838FD";

/**

* Log打印标签*/

private static final String TAG = "BaiduMapApplication";

/* package */BMapManager mMapManager;

/* package */Context mContext;

@Overridepublic void onCreate() {

super.onCreate();Log.i(TAG, "BaiduMapApplication onCreate()");

mContext = getApplicationContext();

mMapManager = new BMapManager(getApplicationContext());

initMapManager();}

/**

* 初始化BMapManager工具*

* @return boolean*/

public void initMapManager() {boolean result = mMapManager.init(BAIDU_MAP_KEY, new MKGeneralListener() {

@Override

public void onGetNetworkState(int error) {if (error == MKEvent.ERROR_NETWORK_CONNECT) {

Toast.makeText(mContext, "您的收集掉足啦!", Toast.LENGTH_LONG).show();} else if (error == MKEvent.ERROR_NETWORK_DATA) {

Toast.makeText(mContext, "输入精确的检索前提!", Toast.LENGTH_LONG).show();}

}

@Overridepublic void onGetPermissionState(int error) {

if (error == MKEvent.ERROR_PERMISSION_DENIED) {// 授权Key差错:

Toast.makeText(mContext, "您的授权Key掉足!", Toast.LENGTH_LONG).show();}

}

});

if (!result) {Toast.makeText(mContext, "初始化舆图引擎掉败!", Toast.LENGTH_LONG).show();

} else {Log.i(TAG, "舆图引擎初始化成功!");

}}

@Override

public void onTerminate() {Log.i(TAG, "BaiduMapApplication onTerminate()");

if (mMapManager != null) {

mMapManager.destroy();mMapManager = null;

}

super.onTerminate();}

}

必要用到百度舆图的Activity基类,承袭自Activity

package com.easi.baidu.map;

import android.app.Activity;

import android.content.res.Configuration;import android.os.Bundle;

import android.util.Log;

import com.baidu.mapapi.BMapManager;import com.baidu.mapapi.map.MapController;

import com.baidu.mapapi.map.MapView;

/奸淫 必要用到百度舆图的Activity基类

* #public class BaiduMapMyLocationActivity extends BaiduMapBaseActivity{}* @author android_ls

*/ public abstract class BaiduMapBaseActivity extends Activity {

/**

* Application工具的引用*/

protected BaiduMapApplication mApplication;

/奸淫 显示舆图的View组件

*/protected MapView mMapView;

/**

* MapView节制器工具的引用*/

protected MapController mMapController;

@Overridepublic void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

mApplication = (BaiduMapApplication) this.getApplication();

if (mApplication.mMapManager == null) {Log.e("BaiduMapApplication", "BaiduMapBaseActivity mApplication.mMapManager is NULL!");

mApplication.mMapManager = new BMapManager(mApplication.mContext);

mApplication.initMapManager();}

setContentView(getLayoutId());

mMapView = (MapView) this.findViewById(getMapViewId());

mMapController = mMapView.getController();}

/**

* 获取代表结构文件的ID* #setContentView(getLayoutId());

* @return int*/

public abstract int getLayoutId();

/奸淫 获取代表MapView工具的组件ID

* #(MapView) this.findViewById(getMapViewId());* @return int

*/public abstract int getMapViewId();

@Override

protected void onPause() {mMapView.onPause();

super.onPause();}

@Override

protected void onResume() {mMapView.onResume();

super.onResume();}

@Override

protected void onDestroy() {mMapView.destroy();

super.onDestroy();}

@Override

protected void onSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);

mMapView.onSaveInstanceState(outState);

}

@Overrideprotected void onRestoreInstanceState(Bundle savedInstanceState) {

super.onRestoreInstanceState(savedInstanceState);mMapView.onRestoreInstanceState(savedInstanceState);

}

@Overridepublic void onConfigurationChanged(Configuration newConfig) {

super.onConfigurationChanged(newConfig);}

}

定位功能详细实现类,承袭自BaiduMapBaseActivity

package com.easi.baidu.map;

import android.os.Bundle;

import android.util.Log;import android.view.View;

import android.widget.TextView;

import com.baidu.location.BDLocation;import com.baidu.location.BDLocationListener;

import com.baidu.location.LocationClient;import com.baidu.location.LocationClientOption;

import com.baidu.mapapi.map.LocationData;import com.baidu.mapapi.map.MapView;

import com.baidu.mapapi.map.MyLocationOverlay;import com.baidu.platform.comapi.basestruct.GeoPoint;

/**

* 供给经由过程GPS或者3G/2G收集(基站)定位,并将定位的结果(我当前的位置)标注在百度舆图上。* 当用户点击舆图上代表“我确当前位置”的点时,显示当前我所在位置的具体信息。

** 注:当gps可用,而且获取了定位结果时,不再提议收集哀求,直接返回给用户坐标。

* 假如gps弗成用,再提议收集哀求,进行定位。* @author android_ls

**/

public class BaiduMapMyLocationActivity extends BaiduMapBaseActivity {

/奸淫 Log打印标签

*/private static final String TAG = "BaiduMapMyLocationActivity";

/**

* 定位SDK的核心类*/

private LocationClient mLocationClient;

/奸淫 显示定位结果具体信息的View

*/private TextView tvLocationResult;

/**

* 标注我的位置的覆盖物* # class LocationOverlay extends MyLocationOverlay{}

*/private MyLocationOverlay mLocationOverlay;

/**

* 我的位置信息数据*/

private LocationData locData;

/奸淫 定位结果处置惩罚器

* # class MyLocationListener implements BDLocationListener{}*/

private MyLocationListener mLocationListener;

/奸淫 暂时缓存我确当前位置数据

*/private BDLocation mBDLocation;

@Override

public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);

Log.i(TAG, "BaiduMapMyLocationActivity onCreate()");

mMapController.setZoom(15);mMapController.enableClick(true);

tvLocationResult = (TextView) this.findViewById(R.id.tv_location_result);

mLocationClient = new LocationClient(this.getApplicationContext());

mLocationListener = new MyLocationListener();mLocationClient.registerLocationListener(mLocationListener);

LocationClientOption locationOption = new LocationClientOption();

locationOption.setOpenGps(true);locationOption.setCoorType("bd09ll");

locationOption.setPriority(LocationClientO伟德官伟德官网手机版ption.GpsFirst);locationOption.setAddrType("all");

locationOption.setProdName("经由过程GPS定位");mLocationClient.setLocOption(locationOption);

Log.i(TAG, "BaiduMapMyLocationActivity 开启定位");

mLocationClient.start();

// 从新定位findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if (mLocationClient != null && mLocationClient.isStarted())mLocationClient.requestLocation();

}});

mLocationOverlay = new LocationOverlay(mMapView);

// 百度官方API文档解释:打开指南针,然则我试验感觉默认指南针便是打开的// mLocationOverlay.enableCompass();

locData = new LocationData();

tvLocationResult.setOnClickListener(new View.OnClickListener() {

@Overridepublic void onClick(View v) {

tvLocationResult.setVisibility(View.GONE);}

});

}

class LocationOverlay extends MyLocationOverlay {

public LocationOverlay(伟德官伟德官网手机版MapView mapView) {super(mapView);

}

// 处置惩罚在“我的位置”坐标上的点击事故。@Override

protected boolean dispatchTap() {Log.i(TAG, "BaiduMapMyLocationActivity 处置惩罚在“我的位置”坐标上的点击事故 dispatchTap()");

if (mBDLocation != null) {

showLocation(mBDLocation);}

return super.dispatchTap();

}

}

/奸淫 显示我当前位置的具体信息

* @param location*/

private void showLocation(BDLocation location) {StringBuffer sb = new StringBuffer(256);

sb.append(" time : ");sb.append(location.getTime());

sb.append("\n error code : ");sb.append(location.getLocType());

sb.append("\n latitude : ");sb.append(location.getLatitude());

sb.append("\n lontitude : ");sb.append(location.getLongitude());

// 判断是否有定位精度半径

if (location.hasRadius()) {// 获取定位精度半径,单位是米

float accuracy = location.getRadius();Log.i(TAG, "accuracy = " + accuracy);

sb.append("\n radius : ");

sb.append(location.getRadius());}

if (location.getLocType() == BDLocation.TypeGpsLocation) {

if (location.hasSpeed()) {sb.append("\n speed : ");

sb.append(location.getSpeed());}

if (location.hasSateNumber()) {

sb.append("\n satellite : ");sb.append(location.getSatelliteNumber());

}} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {

// 获取反地舆编码。 只有应用收集定位的环境下,才能获取当前位置的反地舆编码描述。if (location.hasAddr()) {

String address = location.getAddrStr();Log.i(TAG, "address = " + address);

sb.append("\n addr : ");

sb.append(address);}

}

tvLocationResult.setVisibility(View.VISIBLE);tvLocationResult.setText(sb);

}

/奸淫 在舆图上标注定位获得我当前的位置

* @param location*/

private void markLocation(BDLocation location) {locData.latitude = location.getLatitude();

locData.longitude = location.getLongitude();locData.direction = location.getDerect();

// 判断是否有定位精度半径

if (location.hasRadius()) {// 获取定位精度半径,单位是米

locData.accuracy = location.getRadius();}

mLocationOverlay.setData(locData);

mMapView.getOverlays().add(mLocationOverlay);mMapView.refresh();

// 将我确当前位置移动到舆图的中间点

mMapController.animateTo(new GeoPoint((int) (locData.latitude * 1e6),

(int) (locData.longitude * 1e6)));

}

class MyLocationListener implements BDLocationListener {@Override

public void onReceiveLocation(BDLocation location) {if (location == null) {

return;}

Log.i(TAG, "BaiduMapMyLocationActivity onReceiveLocation()");

// TODO 为方便测试查看

testLog(location);

// 在舆图上标注定位获得我当前的位置markLocation(location);

m伟德官伟德官网手机版BDLocation = location;

}

@Overridepublic void onReceivePoi(BDLocation arg0) {

}

}

/奸淫 打印测试LOG信息

* @param location*/

private void testLog(BDLocation location) {String province = location.getProvince(); // 获取省份信息

String city = location.getCity(); // 获取城市信息String district = location.getDistrict(); // 获取区县信息

Log.i(TAG, "province = " + province);

Log.i(TAG, "city = " + city);Log.i(TAG, "district = " + district);

int type = location.getLocType();

Log.i(TAG, "当前定位采纳的类型是:type = " + type);

String coorType = location.getCoorType();Log.i(TAG, "坐标系类型:coorType = " + coorType);

float derect = location.getDerect();

Log.i(TAG, "derect = " + derect);}

@Override

public int getLayoutId() {return R.layout.main;

}

@Overridepublic int getMapViewId() {

return R.id.bmapView;}

@Overr伟德官伟德官网手机版ide

protected void onDestroy() {Log.i(TAG, "BaiduMapMyLocationActivity onDestroy() start");

mLocationClient.stop();

mLocationClient.unRegisterLocationListener(mLocationListener);this.mApplication.onTerminate();

super.onDestroy();

Log.i(TAG, "BaiduMapMyLocationActivity onDestroy() end");

}

}

转自:http://blog.csdn.net/android_ls/article/details/8586229

免责声明:以上内容源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。

您可能还会对下面的文章感兴趣: