=++++++++++++++++++++MaIN
TiaOzhUan
++++++++++++++++++++++MaP
package com.example.map;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMap;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.MapView;
public class MapActivity extends Activity implements LocationSource,
AMapLocationListener, OnCheckedChangeListener {
private AMap aMap;
private MapView mapView;
private OnLocationChangedListener mListener;
private AMapLocationClient mlocationClient;
private AMapLocationClientOption mLocationOption;
private RadioGroup mGPSModeGroup;
private TextView mLocationErrText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);// 不显示程序的标题栏
setContentView(R.layout.activity_map);
// 获取地图控件引用
mapView = (MapView) findViewById(R.id.map);
// 在activity执行onCreate时执行mMapView.onCreate(savedInstanceState),实现地图生命周期管理
mapView.onCreate(savedInstanceState);
init();
}
/**
* 初始化
*/
private void init() {
if (aMap == null) {
aMap = mapView.getMap();
setUpMap();
}
mGPSModeGroup = (RadioGroup) findViewById(R.id.gps_radio_group);
mGPSModeGroup.setOnCheckedChangeListener(this);
mLocationErrText = (TextView) findViewById(R.id.location_errInfo_text);
mLocationErrText.setVisibility(View.GONE);
}
/**
* 设置一些amap的属性
*/
private void setUpMap() {
aMap.setLocationSource(this);// 设置定位监听
aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示
aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
// 设置定位的类型为定位模式 ,可以由定位、跟随或地图根据面向方向旋转几种
aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
}
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.gps_locate_button:
// 设置定位的类型为定位模式
aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
break;
case R.id.gps_follow_button:
// 设置定位的类型为 跟随模式
aMap.setMyLocationType(AMap.LOCATION_TYPE_MAP_FOLLOW);
break;
case R.id.gps_rotate_button:
// 设置定位的类型为根据地图面向方向旋转
aMap.setMyLocationType(AMap.LOCATION_TYPE_MAP_ROTATE);
break;
}
}
/**
* 方法必须重写
*/
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
/**
* 方法必须重写
*/
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
deactivate();
}
/**
* 方法必须重写
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
/**
* 方法必须重写
*/
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
if (null != mlocationClient) {
mlocationClient.onDestroy();
}
}
/**
* 定位成功后回调函数
*/
@Override
public void onLocationChanged(AMapLocation amapLocation) {
if (mListener != null && amapLocation != null) {
if (amapLocation != null && amapLocation.getErrorCode() == 0) {
mLocationErrText.setVisibility(View.GONE);
mListener.onLocationChanged(amapLocation);// 显示系统小蓝点
} else {
String errText = "定位失败," + amapLocation.getErrorCode() + ": "
+ amapLocation.getErrorInfo();
Log.e("AmapErr", errText);
mLocationErrText.setVisibility(View.VISIBLE);
mLocationErrText.setText(errText);
}
}
}
/**
* 激活定位
*/
@Override
public void activate(OnLocationChangedListener listener) {
mListener = listener;
if (mlocationClient == null) {
mlocationClient = new AMapLocationClient(this);
mLocationOption = new AMapLocationClientOption();
// 设置定位监听
mlocationClient.setLocationListener(this);
// 设置为高精度定位模式
mLocationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
// 设置定位参数
mlocationClient.setLocationOption(mLocationOption);
// 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
// 注意设置合适的定位时间的间隔(最小间隔支持为2000ms),并且在合适时间调用stopLocation()方法来取消定位请求
// 在定位结束后,在合适的生命周期调用onDestroy()方法
// 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除
mlocationClient.startLocation();
}
}
/**
* 停止定位
*/
public void deactivate() {
mListener = null;
if (mlocationClient != null) {
mlocationClient.stopLocation();
mlocationClient.onDestroy();
}
mlocationClient = null;
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.amap.api.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.amap.api.maps.MapView>
<TextView
android:id="@+id/location_errInfo_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|left"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:text="TextView"
android:visibility="gone" />
<RadioGroup
android:id="@+id/gps_radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|left"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/gps_locate_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:textColor="@android:color/black" />
<RadioButton
android:id="@+id/gps_follow_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="11"
android:textColor="@android:color/black" />
<RadioButton
android:id="@+id/gps_rotate_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="22"
android:textColor="@android:color/black" />
</RadioGroup>
</RelativeLayout>
+++++++++++++++++++++++++++qUAnxian
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.map"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="4"
android:targetSdkVersion="8" />
//地图包、搜索包需要的基础权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
//定位包、导航包需要的额外权限(注:基础权限也需要)
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/>
<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" >
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="6ada4bb659004f72e1abd084e503507f"/>
<service android:name="com.amap.api.location.APSService"/>
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MapActivity"
android:label="@string/title_activity_map" >
</activity>
</application>
</manifest>