編輯:關於Android編程
在上一篇博客中,我們成功把地圖導入了我們的項目。本篇我們准備為地圖添加:第一,定位功能;第二,與方向傳感器結合,通過旋轉手機進行道路的方向確認。有了這兩個功能,地圖已經可以為我服務了~~~~
效果圖:

好了,可以代碼,為了方便,我把所有的按鈕都放到了menu菜單中。<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+MaGis/W0zsb0tq+2qM67PC9wPgo8cD48cHJlIGNsYXNzPQ=="brush:java;">/**
* 定位的客戶端
*/
private LocationClient mLocationClient;
/**
* 定位的監聽器
*/
public MyLocationListener mMyLocationListener;
/**
* 當前定位的模式
*/
private LocationMode mCurrentMode = LocationMode.NORMAL;
/***
* 是否是第一次定位
*/
private volatile boolean isFristLocation = true;
/**
* 初始化定位相關代碼
*/
private void initMyLocation()
{
// 定位初始化
mLocationClient = new LocationClient(this);
mMyLocationListener = new MyLocationListener();
mLocationClient.registerLocationListener(mMyLocationListener);
// 設置定位的相關配置
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);// 打開gps
option.setCoorType("bd09ll"); // 設置坐標類型
option.setScanSpan(1000);
mLocationClient.setLocOption(option);
}
然後是定位的監聽器MyLocationListener:
/**
* 實現實位回調監聽
*/
public class MyLocationListener implements BDLocationListener
{
@Override
public void onReceiveLocation(BDLocation location)
{
// map view 銷毀後不在處理新接收的位置
if (location == null || mMapView == null)
return;
// 構造定位數據
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此處設置開發者獲取到的方向信息,順時針0-360
.direction(mXDirection).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
mCurrentAccracy = location.getRadius();
// 設置定位數據
mBaiduMap.setMyLocationData(locData);
mCurrentLantitude = location.getLatitude();
mCurrentLongitude = location.getLongitude();
// 設置自定義圖標
BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory
.fromResource(R.drawable.navi_map_gps_locked);
MyLocationConfigeration config = new MyLocationConfigeration(
mCurrentMode, true, mCurrentMarker);
mBaiduMap.setMyLocationConfigeration(config);
// 第一次定位時,將地圖位置移動到當前位置
if (isFristLocation)
{
isFristLocation = false;
LatLng ll = new LatLng(location.getLatitude(),
location.getLongitude());
MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
mBaiduMap.animateMapStatus(u);
}
}
}可以看到,我們初始化了定位的參數,設置了定位的監聽器,每隔1s會進行一次定位,應用打開時,第一定位,會把地圖中心設置當前用戶位置。@Override
protected void onStart()
{
// 開啟圖層定位
mBaiduMap.setMyLocationEnabled(true);
if (!mLocationClient.isStarted())
{
mLocationClient.start();
}
// 開啟方向傳感器
myOrientationListener.start();
super.onStart();
}
@Override
protected void onStop()
{
// 關閉圖層定位
mBaiduMap.setMyLocationEnabled(false);
mLocationClient.stop();
// 關閉方向傳感器
myOrientationListener.stop();
super.onStop();
}記得在AndroidManifest.xml配一個service
2、我的位置
點擊我的位置菜單會調用center2myLoc方法。
case R.id.id_menu_map_myLoc: center2myLoc(); break;
/**
* 地圖移動到我的位置,此處可以重新發定位請求,然後定位;
* 直接拿最近一次經緯度,如果長時間沒有定位成功,可能會顯示效果不好
*/
private void center2myLoc()
{
LatLng ll = new LatLng(mCurrentLantitude, mCurrentLongitude);
MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
mBaiduMap.animateMapStatus(u);
}3、集成方向傳感器
首先是封裝的方向傳感器的類MyOrientationListener.java
package com.zhy.zhy_baidu_ditu_demo00;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
public class MyOrientationListener implements SensorEventListener
{
private Context context;
private SensorManager sensorManager;
private Sensor sensor;
private float lastX ;
private OnOrientationListener onOrientationListener ;
public MyOrientationListener(Context context)
{
this.context = context;
}
// 開始
public void start()
{
// 獲得傳感器管理器
sensorManager = (SensorManager) context
.getSystemService(Context.SENSOR_SERVICE);
if (sensorManager != null)
{
// 獲得方向傳感器
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
}
// 注冊
if (sensor != null)
{//SensorManager.SENSOR_DELAY_UI
sensorManager.registerListener(this, sensor,
SensorManager.SENSOR_DELAY_UI);
}
}
// 停止檢測
public void stop()
{
sensorManager.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
}
@Override
public void onSensorChanged(SensorEvent event)
{
// 接受方向感應器的類型
if (event.sensor.getType() == Sensor.TYPE_ORIENTATION)
{
// 這裡我們可以得到數據,然後根據需要來處理
float x = event.values[SensorManager.DATA_X];
if( Math.abs(x- lastX) > 1.0 )
{
onOrientationListener.onOrientationChanged(x);
}
// Log.e("DATA_X", x+"");
lastX = x ;
}
}
public void setOnOrientationListener(OnOrientationListener onOrientationListener)
{
this.onOrientationListener = onOrientationListener ;
}
public interface OnOrientationListener
{
void onOrientationChanged(float x);
}
}
/**
* 初始化方向傳感器
*/
private void initOritationListener()
{
myOrientationListener = new MyOrientationListener(
getApplicationContext());
myOrientationListener
.setOnOrientationListener(new OnOrientationListener()
{
@Override
public void onOrientationChanged(float x)
{
mXDirection = (int) x;
// 構造定位數據
MyLocationData locData = new MyLocationData.Builder()
.accuracy(mCurrentAccracy)
// 此處設置開發者獲取到的方向信息,順時針0-360
.direction(mXDirection)
.latitude(mCurrentLantitude)
.longitude(mCurrentLongitude).build();
// 設置定位數據
mBaiduMap.setMyLocationData(locData);
// 設置自定義圖標
BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory
.fromResource(R.drawable.navi_map_gps_locked);
MyLocationConfigeration config = new MyLocationConfigeration(
mCurrentMode, true, mCurrentMarker);
mBaiduMap.setMyLocationConfigeration(config);
}
});
}對於旋轉手機確定方向,實際上利用了
new MyLocationData.Builder() //此處設置開發者獲取到的方向信息,順時針0-360 .direction(mXDirection)只需要把x方向的角度設置即可~~~是不是很簡單~~~
好了,介紹完畢了,關閉地圖樣式的切換,以及跟隨、羅盤等模式的切換就不介紹了,大家自己看下源碼~~
源碼點擊下載
Android通用索引欄實現代碼
偶爾看到之前寫過的代碼,感覺好多東西幾乎在很多項目中都要用到,雖然每個項目的需求和設計都不同,不過實現的效果都是一樣的,可能只是數據格式和一些顏色等的細微差距.但是有的時
Android圖像處理技術(實現Android中的PS)
今天我們接著上次講的內容,介紹另一種改變圖片色彩的方法:像素;今天通過一個例子來熟悉像素操作:實現圖片的 底片,懷舊,雕塑,這三種比較有意思的效果。首先,上圖,勾引一下你
android 圖片閱讀 之 穹の思念
資源是好不容易下載到的,關於代碼,沒什麼好說的。 說點這期間遇到的問題。 漫畫 的每一話大概有20幾個頁面,實際都是jpg圖片,
Android編程之TabWidget選項卡用法實例分析
本文實例講述了Android編程之TabWidget選項卡用法。分享給大家供大家參考,具體如下:1 概覽TabWidget與TabHost。tab組件一般包括TabHos