private AMapLocationClient locationClient = null;
private AMapLocationClientOption locationOption = null;
private TextView textView;
private String[] strMsg;
private com.amap.api.maps.AMap aMap;
private MapView mapView;
private GeocodeSearch geocoderSearch;
private Marker geoMarker;
private static LatLonPoint latLonPoint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.text1);
textView = (TextView)findViewById(R.id.textView2);
button=(Button)findViewById(R.id.button);
mapView = (MapView) findViewById(R.id.map);
mapView.onCreate(savedInstanceState);// 此方法必须重写
Location();
final Intent intent=new Intent();
intent.setAction("Utils");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle data =new Bundle();
data.putString("msg","签到成功");
intent.putExtra("data",data);
sendBroadcast(intent);
}
});
}
private void initMap(){
if (aMap == null) {
aMap = mapView.getMap();
//用高德默认图标
geoMarker= aMap.addMarker(new MarkerOptions().anchor(0.5f, 0.5f).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
//自定义图标
//geoMarker = aMap.addMarker(new MarkerOptions().anchor(0.5f, 0.5f)
// .icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.punch_dw))));
}
geocoderSearch = new GeocodeSearch(this);
geocoderSearch.setOnGeocodeSearchListener(this);
getAddress(latLonPoint);
}
@Override
public void onLocationChanged(AMapLocation loc) {
if (null != loc) {
Message msg = mHandler.obtainMessage();
msg.obj = loc;
msg.what = Utils.MSG_LOCATION_FINISH;
mHandler.sendMessage(msg);
}
}
Handler mHandler = new Handler() {
public void dispatchMessage(android.os.Message msg) {
switch (msg.what) {
//定位完成
case Utils.MSG_LOCATION_FINISH:
String result = "";
try {
AMapLocation loc = (AMapLocation) msg.obj;
result = Utils.getLocationStr(loc, 1);
strMsg = result.split(",");
Toast.makeText(MainActivity.this, "定位成功", Toast.LENGTH_LONG).show();
textView.setText("地址:" + strMsg[0] + "\n" + "经 度:" + strMsg[1] + "\n" + "纬 度:" + strMsg[2]+"\n");
latLonPoint= new LatLonPoint(Double.valueOf(strMsg[2]), Double.valueOf(strMsg[1]));
initMap();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "定位失败", Toast.LENGTH_LONG).show();
}
break;
default:
break;
}
};
};
public void Location() {
// TODO Auto-generated method stub
try {
locationClient = new AMapLocationClient(this);
locationOption = new AMapLocationClientOption();
// 设置定位模式为低功耗模式
locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
// 设置定位监听
locationClient.setLocationListener(this);
locationOption.setOnceLocation(true);//设置为单次定位
locationClient.setLocationOption(locationOption);// 设置定位参数
// 启动定位
locationClient.startLocation();
mHandler.sendEmptyMessage(Utils.MSG_LOCATION_START);
} catch (Exception e) {
Toast.makeText(MainActivity.this, "定位失败", Toast.LENGTH_LONG) |