标签:
个人习惯,上图
关于高德地图自动定位返回地址详细信息一直没写,一方面太忙了(也可以说太懒了),另一方面这个地方的内容太少,因为项目后面会用到快速搜索提示,往地图中添加marker.以及导航以及语音提示等等 本来想等项目上线在好好总结一下,算了不想拖了,
上代码:
package com.example.mydemo;
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;
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 {
// 定位
private LocationManagerProxy mLocationManger;
private TextView mTxt;
private GeocodeSearch geocoderSearch;
//声明mLocationOption对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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;
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,导航等问题后面待续,
标签:
原文地址:http://www.cnblogs.com/yizuochengchi2012/p/5043706.html