我的習慣,上圖android
關於高德地圖自動定位返回地址詳細信息一直沒寫,一方面太忙了(也能夠說太懶了),另外一方面這個地方的內容太少,由於項目後面會用到快速搜索提示,往地圖中添加marker.以及導航以及語音提示等等 原本想等項目上線在好好總結一下,算了不想拖了,git
上代碼:api
package com.example.mydemo;app
import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;ide
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.GeocodeSearch.OnGeocodeSearchListener;
import com.amap.api.services.geocoder.RegeocodeResult;工具
public class MainActivity extends Activity implements
OnGeocodeSearchListener, AMapLocationListener {post
// 定位
private LocationManagerProxy mLocationManger;
private TextView mTxt;
private GeocodeSearch geocoderSearch;
//聲明mLocationOption對象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);this
mTxt = (TextView) findViewById(R.id.tv_txt1);
initLocation();
}
/**
* 初始化定位
*/
private void initLocation() {
// TODO Auto-generated method stub
mLocationManger = LocationManagerProxy.getInstance(getApplicationContext());
mLocationManger.requestLocationData(LocationProviderProxy.AMapNetwork,
60 * 1000, 15, this);
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
stopLocation();
}
}, 15000);// 設置超過15秒尚未定位到就中止定位
geocoderSearch=new GeocodeSearch(getApplicationContext());
geocoderSearch.setOnGeocodeSearchListener(this);
}
/**
* 重寫方法
*/
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(AMapLocation amapLocation) {
// TODO Auto-generated method stub
if (amapLocation != null) {
Double Latitude = amapLocation.getLatitude();
Double Longitude = amapLocation.getLongitude();
Bundle bundle = amapLocation.getExtras();
String desc = "";
String cityCode = "";
if(null!=bundle){
desc = bundle.getString("desc");
}
String str = ("定位成功:(" + Longitude + "," + Latitude + ")"
+ "\n精 度 :" + amapLocation.getAccuracy() + "米"
+ "\n定位方式:" + amapLocation.getProvider() + "\n定位時間:"
+ AMapUtil.convertToTime(amapLocation.getTime()) + "\n城市編碼:"
+ cityCode + "\n位置描述:" + desc + "\n省:"
+ amapLocation.getProvince() + "\n市:" + amapLocation.getCity()
+ "\n區(縣):" + amapLocation.getDistrict() + "\n區域編碼:" + amapLocation
.getAdCode());
String address="位置描述:" + desc + "省:"
+ amapLocation.getProvince() + "市:" + amapLocation.getCity()
+ "區(縣):" + amapLocation.getDistrict() + "區域編碼:" + amapLocation
.getAdCode();
String wk= amapLocation.getDistrict();
Log.i("打印的數據是是是是", address);
Log.i("打印的數據是是是是", address);
Log.i("打印的數據是是是是", address);
Log.i("打印的數據是是是是", address);
mTxt.setText(wk);
}
}
@Override
public void onGeocodeSearched(GeocodeResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onRegeocodeSearched(RegeocodeResult arg0, int arg1) {
// TODO Auto-generated method stub
}編碼
private Handler mHandler=new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {
case 0:
break;.net
default:
break;
}
}
};
/**
* 銷燬定位
*/
private void stopLocation() {
if (mLocationManger != null) {
mLocationManger.removeUpdates(this);
mLocationManger.destory();
}
mLocationManger = null;
}
}
用到了工具類,就不貼了,在demo裏面都有:
http://download.csdn.net/detail/onebelowzero2012/9352489
注意textview顯示文字較長的話寬度設置爲match...
注意清單文件的配置權限等問題。
直接用的話 改一下注冊的key就能夠啦!
另外注意LocationManagerProxy用到Android_Location_V1.3.3.jar及其以上的包纔有,關於高德地圖官網上自動定位的例子表示很不爽,可能我沒找到地方吧,並且高德地圖決定關閉論壇,感受高德地圖論壇開着也沒啥用,根本幫不上新手忙的,關於高德地圖marker,導航等問題後面待續,