写点什么

android 定位到输入的地址

用户头像
Android架构
关注
发布于: 2021 年 11 月 07 日

import com.baidu.location.LocationClientOption;


import com.baidu.mapapi.map.BaiduMap;


import com.baidu.mapapi.map.BitmapDescriptor;


import com.baidu.mapapi.map.BitmapDescriptorFactory;


import com.baidu.mapapi.map.MapStatusUpdate;


import com.baidu.mapapi.map.MapStatusUpdateFactory;


import com.baidu.mapapi.map.MapView;


import com.baidu.mapapi.map.MarkerOptions;


import com.baidu.mapapi.map.MyLocationData;


import com.baidu.mapapi.model.LatLng;


import com.baidu.mapapi.search.core.SearchResult;


import com.baidu.mapapi.search.geocode.GeoCodeOption;


import com.baidu.mapapi.search.geocode.GeoCodeResult;


import com.baidu.mapapi.search.geocode.GeoCoder;


import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;


import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;


public class MainActivity extends Activity implements


OnGetGeoCoderResultListener {


GeoCoder mSearch = null; // 搜索模块,也可去掉地图模块独立使用


// 定位相关


LocationClient mLocClien


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


t;


public MyLocationListenner myListener = new MyLocationListenner();


BitmapDescriptor mCurrentMarker;


MapView mMapView;


BaiduMap mBaiduMap;


// UI 相关


OnCheckedChangeListener radioButtonListener;


Button requestLocButton;


boolean isFirstLoc = true;// 是否首次定位


@Override


protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);


setContentView(R.layout.activity_main);


// 地图初始化


mMapView = (MapView) findViewById(R.id.bmapView);


mBaiduMap = mMapView.getMap();


// 开启定位图层


mBaiduMap.setMyLocationEnabled(true);


// 定位初始化


mLocClient = new LocationClient(this);


mLocClient.registerLocationListener(myListener);


LocationClientOption option = new LocationClientOption();


option.setOpenGps(true);// 打开 gps


option.setCoorType("bd09ll"); // 设置坐标类型


option.setScanSpan(1000);


mLocClient.setLocOption(option);


mLocClient.start();


// 初始化搜索模块,注册事件监听


mSearch = GeoCoder.newInstance();


mSearch.setOnGetGeoCodeResultListener(this);


Button btn_search = (Button) findViewById(R.id.btn_search);


btn_search.setOnClickListener(new OnClickListener() {


@Override


public void onClick(View arg0) {


SearchButtonProcess();


}


});


}


/**


  • 发起地址搜索

  • @param v


*/


public void SearchButtonProcess() {


EditText editCity = (EditText) findViewById(R.id.et_city);


EditText editGeoCodeKey = (EditText) findViewById(R.id.et_addr);


// Geo 搜索


mSearch.geocode(new GeoCodeOption().city(


editCity.getText().toString()).address(


editGeoCodeKey.getText().toString()));


}


@Override


public boolean onCreateOptionsMenu(Menu menu) {


// Inflate the menu; this adds items to the action bar if it is present.


getMenuInflater().inflate(R.menu.main, menu);


return true;


}


/**


  • 定位 SDK 监听函数


*/


public class MyLocationListenner implements BDLocationListener {


@Override


public void onReceiveLocation(BDLocation location) {


// map view 销毁后不在处理新接收的位置


if (location == null || mMapView == null)


return;


MyLocationData locData = new MyLocationData.Builder()


.accuracy(location.getRadius())


// 此处设置开发者获取到的方向信息,顺时针 0-360


.direction(100).latitude(location.getLatitude())


.longitude(location.getLongitude()).build();


mBaiduMap.setMyLocationData(locData);


if (isFirstLoc) {


isFirstLoc = false;


LatLng ll = new LatLng(location.getLatitude(),


location.getLongitude());


MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);


mBaiduMap.animateMapStatus(u);


}


}


public void onReceivePoi(BDLocation poiLocation) {


}


}


@Override


protected void onPause() {


mMapView.onPause();


super.onPause();


}


@Override


protected void onResume() {


mMapView.onResume();


super.onResume();


}


@Override


protected void onDestroy() {


// 退出时销毁定位


mLocClient.stop();


mSearch.destroy();


// 关闭定位图层


mBaiduMap.setMyLocationEnabled(false);


mMapView.onDestroy();


mMapView = null;


super.onDestroy();


}


/**


  • 地址搜索返回结果


*/


@Override


public void onGetGeoCodeResult(GeoCodeResult result) {


if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {


Toast.makeText(MainActivity.this, "抱歉,未能找到结果", Toast.LENGTH_LONG)


.show();


return;


}


mBaiduMap.clear();


mBaiduMap.addOverlay(new MarkerOptions().position(result.getLocation())


.icon(BitmapDescriptorFactory


.fromResource(R.drawable.icon_marka)));


mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(result


.getLocation()));


String strInfo = String.format("纬度:%f 经度:%f",


result.getLocation().latitude, result.getLocation().longitude);

用户头像

Android架构

关注

还未添加个人签名 2021.10.31 加入

还未添加个人简介

评论

发布
暂无评论
android 定位到输入的地址