天天看點

Android特色開發——基于位置的服務                                  對《第一行代碼---Android》的學習筆記基于位置的服務簡介 使用百度地圖

                                  對《第一行代碼---Android》的學習筆記

基于位置的服務簡介

  基于位置服務簡稱LBS,主要工作原理就是利用無線電通訊網絡GPs等定位方式确定移動位置的所在。

找到自己的位置

  android使用LocationManager類實作

  首先擷取執行個體

LocationManager locationManager=(~)getSystemSeriver(Context.LOACTION_SERVICE);
           

  然後需要選擇一個位置提供器來确定裝置的分目前位置。

  Android提供了三種位置提供器可供選擇:1.GPS——PROVIDER2.NETWORK_PROVIDER 3.PASSIVE_PROVIDER.

  其中前兩種使用的比較多,分别表示GPS定位和使用網絡定位,前者定位的精準度比較高,但是非常耗電,而網絡定位的精準度比較低,但耗電少。

  将選擇好的位置提供器傳入到getLastKnownLocation()方法中,就可以得到一個Location對象

String provider=LocationManager.NETWORK_PROVIDER;

  Location location=locationManager.getLastKnownLocation(provider);
           

這個Location對象中包含了經緯度,海拔等一系列的位置資訊,然後從中提取我們需要的資訊即可

如果有些時候你想讓定位的精度更精确一點,但又不确定GPS定位的功能是否已經啟用,這個時候就可以判斷一下有哪些位置提供器

List<String>providerList=locationManager.getProviders(true);
           

getProvides()方法接受一個布爾型參數,傳入true就表示隻有啟用的位置提供器才會被傳回,之後從providesList中判斷是否包含GPS定位的功能就好。

使用requestLocationUpdates()方法,隻要傳入LocationListener的執行個體,就可以裝置位置發生改變時擷取最新的位置資訊。

locationMabager.requestLocationUpdates(LocationManager.GPS_PROVIDER,5000,10,new LocationListener(){});

參數:1.位置提供器的類型2.監聽位置變化的時間間隔3.監聽位置變化的距離間隔4.LocationListener監聽器

反向地理編碼,看的懂得位置資訊

 使用Geocoding API進行反向地理編碼首先發送一個HTTP請求給谷歌伺服器,然後将傳回的JSON資料進行解析

public class MainActivity extends Activity {

	public static final int SHOW_LOCATION = 0;

	private TextView positionTextView;

	private LocationManager locationManager;

	private String provider;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		positionTextView = (TextView) findViewById(R.id.position_text_view);
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		// 擷取所有可用的位置提供器
		List<String> providerList = locationManager.getProviders(true);
		if (providerList.contains(LocationManager.GPS_PROVIDER)) {
			provider = LocationManager.GPS_PROVIDER;
		} else if (providerList.contains(LocationManager.NETWORK_PROVIDER)) {
			provider = LocationManager.NETWORK_PROVIDER;
		} else {
			// 沒有可用提示使用者
			Toast.makeText(this, "No location provider to use",
					Toast.LENGTH_SHORT).show();
			return;
		}
		Location location = locationManager.getLastKnownLocation(provider);
		if (location != null) {
			// 顯示目前裝置的位置資訊
			showLocation(location);
		}
		locationManager.requestLocationUpdates(provider, 5000, 1,
				locationListener);
	}

	protected void onDestroy() {
		super.onDestroy();
		if (locationManager != null) {
			// 關閉程式時将監聽器移除
			locationManager.removeUpdates(locationListener);
		}
	}

	LocationListener locationListener = new LocationListener() {

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}

		@Override
		public void onProviderEnabled(String provider) {
		}

		@Override
		public void onProviderDisabled(String provider) {
		}

		@Override
		public void onLocationChanged(Location location) {
			// 更新目前裝置資訊
			showLocation(location);
		}
	};

	private void showLocation(final Location location) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					// 組裝反向地理編碼的接口
					StringBuilder url = new StringBuilder();
					url.append("http://maps.googleapis.com/maps/api/geocode/json?latlng=");
					url.append(location.getLatitude()).append(",")
							.append(location.getLongitude());
					url.append("&sensor=false");
					HttpClient httpClient = new DefaultHttpClient();
					HttpGet httpGet = new HttpGet(url.toString());
					// 在請求消息中指定語言,保證伺服器傳回中文資料
					httpGet.addHeader("Accept-Language", "zh-CN");
					HttpResponse httpResponse = httpClient.execute(httpGet);
					if (httpResponse.getStatusLine().getStatusCode() == 200) {
						HttpEntity entity = httpResponse.getEntity();
						String response = EntityUtils.toString(entity, "utf-8");
						JSONObject jsonObject = new JSONObject(response);
						// 擷取results節點下的位置資訊
						JSONArray resultArray = jsonObject.getJSONArray("results");
						if (resultArray.length() > 0) {
							JSONObject subObject = resultArray.getJSONObject(0);
							// 取出格式化的位置資訊
							String address = subObject.getString("formatted_address");
							Message message = new Message();
							message.what = SHOW_LOCATION;
							message.obj = address;
							handler.sendMessage(message);
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}).start();
	}

	private Handler handler = new Handler() {

		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SHOW_LOCATION:
				String currentPosition = (String) msg.obj;
				positionTextView.setText(currentPosition);
				break;
			default:
				break;
			}
		}

	};

}
           

使用百度地圖

首先在布局中方式一個百度提供的MapView自定義組建,是以使用它的時候需要帶上完整的包名。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.baidu.mapapi.map.MapView
        android:id="@+id/map_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true" />

</LinearLayout>
           

還需要設定多個權限

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_GPS" />
           

最終要的需要白申請的APIKey配置進去,也可以在代碼中動态配置

manager = new BMapManager(this);
 
  manager.init("SHVPoTtIpzfonPD3HCkc5sIt", null);
           
public class MainActivity extends Activity {

	private BMapManager manager;

	private MapView mapView;

	private LocationManager locationManager;

	private String provider;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		manager = new BMapManager(this);
		// API Key闇�瑕佹浛鎹㈡垚浣犺嚜宸辯殑
		manager.init("SHVPoTtIpzfonPD3HCkc5sIt", null);
		setContentView(R.layout.activity_main);
		mapView = (MapView) findViewById(R.id.map_view);
		mapView.setBuiltInZoomControls(true);
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		// 擷取位置提供其
		List<String> providerList = locationManager.getProviders(true);
		if (providerList.contains(LocationManager.GPS_PROVIDER)) {
			provider = LocationManager.GPS_PROVIDER;
		} else if (providerList.contains(LocationManager.NETWORK_PROVIDER)) {
			provider = LocationManager.NETWORK_PROVIDER;
		} else {
			
			Toast.makeText(this, "No location provider to use",
					Toast.LENGTH_SHORT).show();
			return;
		}
		Location location = locationManager.getLastKnownLocation(provider);
		if (location != null) {
			navigateTo(location);
		}
	}

	private void navigateTo(Location location) {
		MapController controller = mapView.getController();
		// 璁劇疆缂╂斁绾у埆
		controller.setZoom(16);
		GeoPoint point = new GeoPoint((int) (location.getLatitude() * 1E6),
				(int) (location.getLongitude() * 1E6));
		// 璁劇疆鍦闆浘涓績鐐�
		controller.setCenter(point);
		MyLocationOverlay myLocationOverlay = new MyLocationOverlay(mapView);
		LocationData locationData = new LocationData();
		// 鎸囧畾鎴戠殑浣嶇疆
		locationData.latitude = location.getLatitude();
		locationData.longitude = location.getLongitude();
		myLocationOverlay.setData(locationData);
		mapView.getOverlays().add(myLocationOverlay);
		// 鍒鋒柊浣挎柊澧炶鐩栫墿鐢熸晥
		mapView.refresh();
		PopupOverlay pop = new PopupOverlay(mapView, new PopupClickListener() {
			@Override
			public void onClickedPopup(int index) {
				// 鐩稿簲鍥劇墖鐨勭偣鍑諱簨浠�
				Toast.makeText(MainActivity.this,
						"You clicked button " + index, Toast.LENGTH_SHORT)
						.show();
			}
		});
		// 鍒涘緩涓�涓暱搴︿負3鐨凚itmap鏁扮粍
		Bitmap[] bitmaps = new Bitmap[3];
		try {
			// 灏嗕笁寮犲浘鐗囪鍙栧埌鍐呭瓨涓�
			bitmaps[0] = BitmapFactory.decodeResource(getResources(),
					R.drawable.left);
			bitmaps[1] = BitmapFactory.decodeResource(getResources(),
					R.drawable.middle);
			bitmaps[2] = BitmapFactory.decodeResource(getResources(),
					R.drawable.right);
		} catch (Exception e) {
			e.printStackTrace();
		}
		pop.showPopup(bitmaps, point, 18);
	}

	@Override
	protected void onDestroy() {
		mapView.destroy();
		if (manager != null) {
			manager.destroy();
			manager = null;
		}
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		mapView.onPause();
		if (manager != null) {
			manager.stop();
		}
		super.onPause();
	}

	@Override
	protected void onResume() {
		mapView.onResume();
		if (manager != null) {
			manager.start();
		}
		super.onResume();
	}

}