android显示地图代码,Android Studio之高德地图实现定位和3D地图显示(示例代码)

/****

* 讲诉了高德地图定位和3D地图显示

*

* 打包和未打包的情况是不一样的,高德配置是可以配置调试版和发布版

**/

public class MainActivity extends AppCompatActivity implementsAMapLocationListener,GeocodeSearch.OnGeocodeSearchListener {private AMapLocationClient locationClient = null;private AMapLocationClientOption locationOption = null;privateTextView textView;privateString[] strMsg;privatecom.amap.api.maps.AMap aMap;privateMapView mapView;privateGeocodeSearch geocoderSearch;privateMarker geoMarker;private staticLatLonPoint latLonPoint;

@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

textView=(TextView) findViewById(R.id.text_map);

mapView=(MapView) findViewById(R.id.map);

mapView.onCreate(savedInstanceState);//此方法必须重写

Location();

}private voidinitMap(){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);

}

@Overridepublic voidonLocationChanged(AMapLocation loc) {if (null !=loc) {

Message msg=mHandler.obtainMessage();

msg.obj=loc;

msg.what=Utils.MSG_LOCATION_FINISH;

mHandler.sendMessage(msg);

}

}

Handler mHandler= newHandler() {public voiddispatchMessage(android.os.Message msg) {switch(msg.what) {//定位完成

caseUtils.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]);

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 voidLocation() {//TODO Auto-generated method stub

try{

locationClient= new AMapLocationClient(this);

locationOption= newAMapLocationClientOption();//设置定位模式为低功耗模式

locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);//设置定位监听

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).show();

}

}/*** 响应逆地理编码*/

public void getAddress(finalLatLonPoint latLonPoint) {

RegeocodeQuery query= new RegeocodeQuery(latLonPoint, 200,

GeocodeSearch.AMAP);//第一个参数表示一个Latlng,第二参数表示范围多少米,第三个参数表示是火系坐标系还是GPS原生坐标系

geocoderSearch.getFromLocationAsyn(query);//设置同步逆地理编码请求

}/*** 地理编码查询回调*/@Overridepublic void onGeocodeSearched(GeocodeResult result, intrCode) {

}/*** 逆地理编码回调*/@Overridepublic void onRegeocodeSearched(RegeocodeResult result, intrCode) {if (rCode == 1000) {if (result != null && result.getRegeocodeAddress() != null

&& result.getRegeocodeAddress().getFormatAddress() != null) {

Toast.makeText(MainActivity.this,result.getRegeocodeAddress().getFormatAddress()+ "附近",Toast.LENGTH_LONG).show();

aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(

AMapUtil.convertToLatLng(latLonPoint),15));

geoMarker.setPosition(AMapUtil.convertToLatLng(latLonPoint));

}else{

}

}else{

}

}/*** 方法必须重写*/@Overridepublic voidonResume() {super.onResume();

mapView.onResume();

}/*** 方法必须重写*/@Overridepublic voidonPause() {super.onPause();

mapView.onPause();

}/*** 方法必须重写*/@Overridepublic voidonSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);

mapView.onSaveInstanceState(outState);

}

@Overridepublic voidonDestroy() {super.onDestroy();

mapView.onDestroy();

}

}

你可能感兴趣的:(android显示地图代码)