高德地图坐标距离计算中的平均速度怎么计算的

android(45)
最近比较闲,所以就顺便研究高德地图,原因是之前基本上都用的百度地图,但是百度地图的代码太多了,两字,很烦。
先来个效果图:
蓝色的marker就是点击的,蓝色的圆圈是我当前位置。
apk下载地址:
一些基本操作这里就不多说了,自己去看官方文档,我们这里说一下周边搜索和POI计算距离。
首先定位一下自己当前位置,因为我要拿到当前的位置的经纬度,后面有用:
* 设置一些amap的属性
private void setUpLocation() {
mAMap.setLocationSource(this);
mAMap.getUiSettings().setMyLocationButtonEnabled(true);
mAMap.setMyLocationEnabled(true);
mAMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
* 定位成功后回调函数
public void onLocationChanged(AMapLocation amapLocation) {
if (mListener != null && amapLocation != null) {
if (amapLocation != null
&& amapLocation.getErrorCode() == 0) {
amapLocation.getLocationType();
myLat = amapLocation.getLatitude();
myLongt = amapLocation.getLongitude();
amapLocation.getAccuracy();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(amapLocation.getTime());
df.format(date);
amapLocation.getAddress();
amapLocation.getCountry();
amapLocation.getProvince();
amapLocation.getCity();
amapLocation.getDistrict();
amapLocation.getStreet();
amapLocation.getStreetNum();
amapLocation.getCityCode();
amapLocation.getAdCode();
TextView textView = (TextView) findViewById(R.id.city_detail);
textView.setText(amapLocation.getCity() + "=" + amapLocation.getDistrict() + "==" + amapLocation.getStreetNum());
mListener.onLocationChanged(amapLocation);
String errText = "定位失败," + amapLocation.getErrorCode() + ": " + amapLocation.getErrorInfo();
Log.e("AmapErr", errText);
* 激活定位
public void activate(OnLocationChangedListener listener) {
mListener =
if (mlocationClient == null) {
mlocationClient = new AMapLocationClient(this);
mLocationOption = new AMapLocationClientOption();
mlocationClient.setLocationListener(this);
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
mlocationClient.setLocationOption(mLocationOption);
mlocationClient.startLocation();
* 停止定位
public void deactivate() {
mListener = null;
if (mlocationClient != null) {
mlocationClient.stopLocation();
mlocationClient.onDestroy();
mlocationClient = null;
可以看到,定位成功之后我们可以拿到任何想要的数据。
一步步来,先初始化一下数据,
* 初始化AMap对象
private void init() {
if (mAMap == null) {
mAMap = mapview.getMap();
mAMap.setOnMapClickListener(this);
mAMap.setOnMarkerClickListener(this);
mAMap.setOnInfoWindowClickListener(this);
mAMap.setInfoWindowAdapter(this);
TextView searchButton = (TextView) findViewById(R.id.btn_search);
searchButton.setOnClickListener(this);
setUpLocation();
locationMarker = mAMap.addMarker(new MarkerOptions()
.anchor(0.5f, 0.5f)
.icon(BitmapDescriptorFactory
.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.point4)))
.position(new LatLng(myLat, myLongt)));
locationMarker.showInfoWindow();
mAMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(myLat, myLongt), 14));
tvDistance = (TextView) findViewById(R.id.poi_info);
我们可以根据自己想要的去搜索
* 开始进行poi搜索
protected void doSearchQuery() {
keyWord = mSearchText.getText().toString().trim();
currentPage = 0;
query = new PoiSearch.Query(keyWord, "", "");
query.setPageSize(20);
query.setPageNum(currentPage);
LatLonPoint lp = new LatLonPoint(myLat, myLongt);
if (lp != null) {
poiSearch = new PoiSearch(this, query);
poiSearch.setOnPoiSearchListener(this);
poiSearch.setBound(new SearchBound(lp, 10000, true));
poiSearch.searchPOIAsyn();
搜索的结果会在
public void onPoiSearched(PoiResult result, int rcode) 里面来做,如果rcode==1000那就是搜索成功了
public void onPoiSearched(PoiResult result, int rcode) {
if (rcode == AMapException.CODE_AMAP_SUCCESS) {
if (result != null && result.getQuery() != null) {// 搜索poi的结果
if (result.getQuery().equals(query)) {// 是否是同一条
poiResult =
poiItems = poiResult.getPois();// 取得第一页的poiitem数据,页数从数字0开始
List&SuggestionCity& suggestionCities = poiResult
.getSearchSuggestionCitys();// 当搜索不到poiitem数据时,会返回含有搜索关键字的城市信息
if (poiItems != null && poiItems.size() & 0) {
//清除POI信息显示
whetherToShowDetailInfo(false);
//并还原点击marker样式
if (mlastMarker != null) {
resetlastmarker();
//清理之前搜索结果的marker
if (poiOverlay != null) {
poiOverlay.removeFromMap();
mAMap.clear();
poiOverlay = new myPoiOverlay(mAMap, poiItems);
poiOverlay.addToMap();
poiOverlay.zoomToSpan();
mAMap.addMarker(new MarkerOptions()
.anchor(0.5f, 0.5f)
.icon(BitmapDescriptorFactory
.fromBitmap(BitmapFactory.decodeResource(
getResources(), R.mipmap.point4)))
.position(new LatLng(myLat, myLongt)));
mAMap.addCircle(new CircleOptions()
.center(new LatLng(myLat, myLongt)).radius(5000)
.strokeColor(Color.BLUE)
.fillColor(Color.argb(50, 1, 1, 1))
.strokeWidth(2));
} else if (suggestionCities != null
&& suggestionCities.size() & 0) {
showSuggestCity(suggestionCities);
ToastUtil.show(PoiAroundSearchActivity.this,
R.string.no_result);
.show(PoiAroundSearchActivity.this, R.string.no_result);
ToastUtil.showerror(this.getApplicationContext(), rcode);
接下来要做的就是根据搜索到的marker,当用户点击这个marker的时候,就计算出用户当前点击的marker到用户之间的距离。
public boolean onMarkerClick(Marker marker) {
if (marker.getObject() != null) {
whetherToShowDetailInfo(true);
PoiItem mCurrentPoi = (PoiItem) marker.getObject();
if (mlastMarker == null) {
mlastMarker =
// 将之前被点击的marker置为原来的状态
resetlastmarker();
mlastMarker =
detailMarker =
detailMarker.setIcon(BitmapDescriptorFactory
.fromBitmap(BitmapFactory.decodeResource(
getResources(),
R.mipmap.poi_marker_pressed)));
setPoiItemDisplayContent(mCurrentPoi);
} catch (Exception e) {
// TODO: handle exception
whetherToShowDetailInfo(false);
resetlastmarker();
//我的当前位置与点击的marker的距离
LatLng latlngA = new LatLng(myLat, myLongt);
distance = AMapUtils.calculateLineDistance(latlngA, marker.getPosition());
tvDistance.setText("两点间距离为:" + distance + "m");
这里我只是显示十个marker。
怎么把marker添加到地图里面呢,我们自己定义一个类吧,
* 自定义PoiOverlay
private class myPoiOverlay {
private AM
private List&PoiItem& mP
private ArrayList&Marker& mPoiMarks = new ArrayList&Marker&();
public myPoiOverlay(AMap amap, List&PoiItem& pois) {
* 添加Marker到地图中。
public void addToMap() {
for (int i = 0; i & mPois.size(); i++) {
Marker marker = mamap.addMarker(getMarkerOptions(i));
PoiItem item = mPois.get(i);
marker.setObject(item);
mPoiMarks.add(marker);
* 去掉PoiOverlay上所有的Marker。
public void removeFromMap() {
for (Marker mark : mPoiMarks) {
mark.remove();
* 移动镜头到当前的视角。
public void zoomToSpan() {
if (mPois != null && mPois.size() & 0) {
if (mamap == null)
LatLngBounds bounds = getLatLngBounds();
mamap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100));
private LatLngBounds getLatLngBounds() {
LatLngBounds.Builder b = LatLngBounds.builder();
for (int i = 0; i & mPois.size(); i++) {
b.include(new LatLng(mPois.get(i).getLatLonPoint().getLatitude(),
mPois.get(i).getLatLonPoint().getLongitude()));
return b.build();
private MarkerOptions getMarkerOptions(int index) {
return new MarkerOptions()
.position(
new LatLng(mPois.get(index).getLatLonPoint()
.getLatitude(), mPois.get(index)
.getLatLonPoint().getLongitude()))
.title(getTitle(index)).snippet(getSnippet(index))
.icon(getBitmapDescriptor(index));
protected String getTitle(int index) {
return mPois.get(index).getTitle();
protected String getSnippet(int index) {
return mPois.get(index).getSnippet();
* 从marker中得到poi在list的位置。
* marker 一个标记的对象。
* 返回该marker对应的poi在list的位置。
public int getPoiIndex(Marker marker) {
for (int i = 0; i & mPoiMarks.size(); i++) {
if (mPoiMarks.get(i).equals(marker)) {
return -1;
* 返回第index的poi的信息。
* index 第几个poi。
* poi的信息。poi对象详见搜索服务模块的基础核心包(com.amap.api.services.core)中的类 &strong&&a href="../../../../../../Search/com/amap/api/services/core/PoiItem.html" title="com.amap.api.services.core中的类"&PoiItem&/a&&/strong&。
public PoiItem getPoiItem(int index) {
if (index & 0 || index &= mPois.size()) {
return null;
return mPois.get(index);
protected BitmapDescriptor getBitmapDescriptor(int arg0) {
if (arg0 & 10) {
BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(
BitmapFactory.decodeResource(getResources(), markers[arg0]));
BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(
BitmapFactory.decodeResource(getResources(), R.mipmap.marker_other_highlight));
代码解释很全面,应该看懂没问题了。主要代码也就这么多。
贴一个全面的类给大家参考:
import android.graphics.BitmapF
import android.os.B
import android.support.annotation.N
import android.support.v7.app.AppCompatA
import android.util.L
import android.view.V
import android.view.View.OnClickL
import android.widget.EditT
import android.widget.RelativeL
import android.widget.TextV
import com.amap.api.location.AMapL
import com.amap.api.location.AMapLocationC
import com.amap.api.location.AMapLocationClientO
import com.amap.api.location.AMapLocationL
import com.amap.api.maps.AM
import com.amap.api.WindowA
import com.amap.api.maps.AMap.OnInfoWindowClickL
import com.amap.api.maps.AMap.OnMapClickL
import com.amap.api.maps.AMap.OnMarkerClickL
import com.amap.api.maps.AMapU
import com.amap.api.maps.CameraUpdateF
import com.amap.api.maps.LocationS
import com.amap.api.maps.MapV
import com.amap.api.maps.model.BitmapD
import com.amap.api.maps.model.BitmapDescriptorF
import com.amap.api.maps.model.LatL
import com.amap.api.maps.model.LatLngB
import com.amap.api.maps.model.M
import com.amap.api.maps.model.MarkerO
import com.amap.api.services.core.AMapE
import com.amap.api.services.core.LatLonP
import com.amap.api.services.core.PoiI
import com.amap.api.services.core.SuggestionC
import com.amap.api.services.poisearch.PoiR
import com.amap.api.services.poisearch.PoiS
import com.amap.api.services.poisearch.PoiSearch.OnPoiSearchL
import com.amap.api.services.poisearch.PoiSearch.SearchB
import com.example.donghe.maptest.utils.ToastU
import java.text.SimpleDateF
import java.util.ArrayL
import java.util.D
import java.util.L
* 周边搜索
public class PoiAroundSearchActivity extends AppCompatActivity implements OnClickListener,
OnMapClickListener, OnInfoWindowClickListener, InfoWindowAdapter, OnMarkerClickListener,
OnPoiSearchListener, LocationSource,
AMapLocationListener {
private MapV
private AMap mAM
private PoiResult poiR // poi返回的结果
private int currentPage = 0;// 当前页面,从0开始计数
private PoiSearch.Q// Poi查询条件类
private Marker locationM // 选择的点
private Marker detailM
private Marker mlastM
private PoiSearch poiS
private myPoiOverlay poiO// poi图层
private List&PoiItem& poiI// poi数据
private RelativeLayout mPoiD
private TextView mPoiName, mPoiA
private String keyWord = "";
private EditText mSearchT
private TextView tvD
private Double myLat = 0.0, myLongt = 0.0;//我的当前位置的经纬度
private OnLocationChangedListener mL
private AMapLocationClient mlocationC
private AMapLocationClientOption mLocationO
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.poiaroundsearch_activity);
mapview = (MapView) findViewById(R.id.mapView);
mapview.onCreate(savedInstanceState);
* 初始化AMap对象
private void init() {
if (mAMap == null) {
mAMap = mapview.getMap();
mAMap.setOnMapClickListener(this);
mAMap.setOnMarkerClickListener(this);
mAMap.setOnInfoWindowClickListener(this);
mAMap.setInfoWindowAdapter(this);
TextView searchButton = (TextView) findViewById(R.id.btn_search);
searchButton.setOnClickListener(this);
setUpLocation();
locationMarker = mAMap.addMarker(new MarkerOptions()
.anchor(0.5f, 0.5f)
.icon(BitmapDescriptorFactory
.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.point4)))
.position(new LatLng(myLat, myLongt)));
locationMarker.showInfoWindow();
mAMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(myLat, myLongt), 14));
tvDistance = (TextView) findViewById(R.id.poi_info);
private void setup() {
mPoiDetail = (RelativeLayout) findViewById(R.id.poi_detail);
mPoiDetail.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(PoiSearchActivity.this,
SearchDetailActivity.class);
intent.putExtra(“poiitem”, mPoi);
startActivity(intent);
mPoiName = (TextView) findViewById(R.id.poi_name);
mPoiAddress = (TextView) findViewById(R.id.poi_address);
mSearchText = (EditText) findViewById(R.id.input_edittext);
* 开始进行poi搜索
protected void doSearchQuery() {
keyWord = mSearchText.getText().toString().trim();
currentPage = 0;
query = new PoiSearch.Query(keyWord, "", "");// 第一个参数表示搜索字符串,第二个参数表示poi搜索类型,第三个参数表示poi搜索区域(空字符串代表全国)
query.setPageSize(20);// 设置每页最多返回多少条poiitem
query.setPageNum(currentPage);// 设置查第一页
LatLonPoint lp = new LatLonPoint(myLat, myLongt);
if (lp != null) {
poiSearch = new PoiSearch(this, query);
poiSearch.setOnPoiSearchListener(this);
poiSearch.setBound(new SearchBound(lp, 10000, true));//
// 设置搜索区域为以lp点为圆心,其周围10000米范围
poiSearch.searchPOIAsyn();// 异步搜索
* 方法必须重写
protected void onResume() {
super.onResume();
mapview.onResume();
whetherToShowDetailInfo(false);
* 方法必须重写
protected void onPause() {
super.onPause();
mapview.onPause();
* 方法必须重写
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapview.onSaveInstanceState(outState);
* 方法必须重写
protected void onDestroy() {
super.onDestroy();
mapview.onDestroy();
public void onPoiItemSearched(PoiItem arg0, int arg1) {
// TODO Auto-generated method stub
public void onPoiSearched(PoiResult result, int rcode) {
if (rcode == AMapException.CODE_AMAP_SUCCESS) {
if (result != null && result.getQuery() != null) {// 搜索poi的结果
if (result.getQuery().equals(query)) {// 是否是同一条
poiResult =
poiItems = poiResult.getPois();// 取得第一页的poiitem数据,页数从数字0开始
List&SuggestionCity& suggestionCities = poiResult
.getSearchSuggestionCitys();// 当搜索不到poiitem数据时,会返回含有搜索关键字的城市信息
if (poiItems != null && poiItems.size() & 0) {
//清除POI信息显示
whetherToShowDetailInfo(false);
//并还原点击marker样式
if (mlastMarker != null) {
resetlastmarker();
//清理之前搜索结果的marker
if (poiOverlay != null) {
poiOverlay.removeFromMap();
mAMap.clear();
poiOverlay = new myPoiOverlay(mAMap, poiItems);
poiOverlay.addToMap();
poiOverlay.zoomToSpan();
mAMap.addMarker(new MarkerOptions()
.anchor(0.5f, 0.5f)
.icon(BitmapDescriptorFactory
.fromBitmap(BitmapFactory.decodeResource(
getResources(), R.mipmap.point4)))
.position(new LatLng(myLat, myLongt)));
mAMap.addCircle(new CircleOptions()
.center(new LatLng(myLat, myLongt)).radius(5000)
.strokeColor(Color.BLUE)
.fillColor(Color.argb(50, 1, 1, 1))
.strokeWidth(2));
} else if (suggestionCities != null
&& suggestionCities.size() & 0) {
showSuggestCity(suggestionCities);
ToastUtil.show(PoiAroundSearchActivity.this,
R.string.no_result);
.show(PoiAroundSearchActivity.this, R.string.no_result);
ToastUtil.showerror(this.getApplicationContext(), rcode);
public boolean onMarkerClick(Marker marker) {
if (marker.getObject() != null) {
whetherToShowDetailInfo(true);
PoiItem mCurrentPoi = (PoiItem) marker.getObject();
if (mlastMarker == null) {
mlastMarker =
// 将之前被点击的marker置为原来的状态
resetlastmarker();
mlastMarker =
detailMarker =
detailMarker.setIcon(BitmapDescriptorFactory
.fromBitmap(BitmapFactory.decodeResource(
getResources(),
R.mipmap.poi_marker_pressed)));
setPoiItemDisplayContent(mCurrentPoi);
} catch (Exception e) {
// TODO: handle exception
whetherToShowDetailInfo(false);
resetlastmarker();
//我的当前位置与点击的marker的距离
LatLng latlngA = new LatLng(myLat, myLongt);
distance = AMapUtils.calculateLineDistance(latlngA, marker.getPosition());
tvDistance.setText("两点间距离为:" + distance + "m");
// 将之前被点击的marker置为原来的状态
private void resetlastmarker() {
int index = poiOverlay.getPoiIndex(mlastMarker);
if (index & 10) {
mlastMarker.setIcon(BitmapDescriptorFactory
.fromBitmap(BitmapFactory.decodeResource(
getResources(),
markers[index])));
mlastMarker.setIcon(BitmapDescriptorFactory.fromBitmap(
BitmapFactory.decodeResource(getResources(), R.mipmap.marker_other_highlight)));
mlastMarker =
private void setPoiItemDisplayContent(final PoiItem mCurrentPoi) {
mPoiName.setText(mCurrentPoi.getTitle());
mPoiAddress.setText(mCurrentPoi.getSnippet() + mCurrentPoi.getDistance());
public View getInfoContents(Marker arg0) {
// TODO Auto-generated method stub
public View getInfoWindow(Marker arg0) {
// TODO Auto-generated method stub
public void onInfoWindowClick(Marker arg0) {
// TODO Auto-generated method stub
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_search:
doSearchQuery();
private int[] markers = {R.mipmap.poi_marker_1,
R.mipmap.poi_marker_2,
R.mipmap.poi_marker_3,
R.mipmap.poi_marker_4,
R.mipmap.poi_marker_5,
R.mipmap.poi_marker_6,
R.mipmap.poi_marker_7,
R.mipmap.poi_marker_8,
R.mipmap.poi_marker_9,
R.mipmap.poi_marker_10
private void whetherToShowDetailInfo(boolean isToShow) {
if (isToShow) {
mPoiDetail.setVisibility(View.VISIBLE);
mPoiDetail.setVisibility(View.GONE);
public void onMapClick(LatLng arg0) {
whetherToShowDetailInfo(false);
if (mlastMarker != null) {
resetlastmarker();
* poi没有搜索到数据,返回一些推荐城市的信息
private void showSuggestCity(List&SuggestionCity& cities) {
String infomation = "推荐城市\n";
for (int i = 0; i & cities.size(); i++) {
infomation += "城市名称:" + cities.get(i).getCityName() + "城市区号:"
+ cities.get(i).getCityCode() + "城市编码:"
+ cities.get(i).getAdCode() + "\n";
ToastUtil.show(this, infomation);
* 自定义PoiOverlay
private class myPoiOverlay {
private AM
private List&PoiItem& mP
private ArrayList&Marker& mPoiMarks = new ArrayList&Marker&();
public myPoiOverlay(AMap amap, List&PoiItem& pois) {
* 添加Marker到地图中。
* @since V2.1.0
public void addToMap() {
for (int i = 0; i & mPois.size(); i++) {
Marker marker = mamap.addMarker(getMarkerOptions(i));
PoiItem item = mPois.get(i);
marker.setObject(item);
mPoiMarks.add(marker);
* 去掉PoiOverlay上所有的Marker。
* @since V2.1.0
public void removeFromMap() {
for (Marker mark : mPoiMarks) {
mark.remove();
* 移动镜头到当前的视角。
* @since V2.1.0
public void zoomToSpan() {
if (mPois != null && mPois.size() & 0) {
if (mamap == null)
LatLngBounds bounds = getLatLngBounds();
mamap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100));
private LatLngBounds getLatLngBounds() {
LatLngBounds.Builder b = LatLngBounds.builder();
for (int i = 0; i & mPois.size(); i++) {
b.include(new LatLng(mPois.get(i).getLatLonPoint().getLatitude(),
mPois.get(i).getLatLonPoint().getLongitude()));
return b.build();
private MarkerOptions getMarkerOptions(int index) {
return new MarkerOptions()
.position(
new LatLng(mPois.get(index).getLatLonPoint()
.getLatitude(), mPois.get(index)
.getLatLonPoint().getLongitude()))
.title(getTitle(index)).snippet(getSnippet(index))
.icon(getBitmapDescriptor(index));
protected String getTitle(int index) {
return mPois.get(index).getTitle();
protected String getSnippet(int index) {
return mPois.get(index).getSnippet();
* 从marker中得到poi在list的位置。
* @param marker 一个标记的对象。
* @return 返回该marker对应的poi在list的位置。
* @since V2.1.0
public int getPoiIndex(Marker marker) {
for (int i = 0; i & mPoiMarks.size(); i++) {
if (mPoiMarks.get(i).equals(marker)) {
return -1;
* 返回第index的poi的信息。
* @param index 第几个poi。
* @return poi的信息。poi对象详见搜索服务模块的基础核心包(com.amap.api.services.core)中的类 &strong&&a href="../../../../../../Search/com/amap/api/services/core/PoiItem.html" title="com.amap.api.services.core中的类"&PoiItem&/a&&/strong&。
* @since V2.1.0
public PoiItem getPoiItem(int index) {
if (index & 0 || index &= mPois.size()) {
return mPois.get(index);
protected BitmapDescriptor getBitmapDescriptor(int arg0) {
if (arg0 & 10) {
BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(
BitmapFactory.decodeResource(getResources(), markers[arg0]));
BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(
BitmapFactory.decodeResource(getResources(), R.mipmap.marker_other_highlight));
* 设置一些amap的属性
private void setUpLocation() {
mAMap.setLocationSource(this);// 设置定位监听
mAMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示
mAMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
// 设置定位的类型为定位模式 ,可以由定位、跟随或地图根据面向方向旋转几种
mAMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
* 定位成功后回调函数
public void onLocationChanged(AMapLocation amapLocation) {
if (mListener != null && amapLocation != null) {
if (amapLocation != null
&& amapLocation.getErrorCode() == 0) {
//定位成功回调信息,设置相关消息
amapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表
myLat = amapLocation.getLatitude();//获取纬度
myLongt = amapLocation.getLongitude();//获取经度
amapLocation.getAccuracy();//获取精度信息
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(amapLocation.getTime());
df.format(date);//定位时间
amapLocation.getAddress();//地址,如果option中设置isNeedAddress为false,则没有此结果,网络定位结果中会有地址信息,GPS定位不返回地址信息。
amapLocation.getCountry();//国家信息
amapLocation.getProvince();//省信息
amapLocation.getCity();//城市信息
amapLocation.getDistrict();//城区信息
amapLocation.getStreet();//街道信息
amapLocation.getStreetNum();//街道门牌号信息
amapLocation.getCityCode();//城市编码
amapLocation.getAdCode();//地区编码
TextView textView = (TextView) findViewById(R.id.city_detail);
textView.setText(amapLocation.getCity() + "=" + amapLocation.getDistrict() + "==" + amapLocation.getStreetNum());
mListener.onLocationChanged(amapLocation);// 显示系统小蓝点
String errText = "定位失败," + amapLocation.getErrorCode() + ": " + amapLocation.getErrorInfo();
Log.e("AmapErr", errText);
* 激活定位
public void activate(OnLocationChangedListener listener) {
mListener =
if (mlocationClient == null) {
mlocationClient = new AMapLocationClient(this);
mLocationOption = new AMapLocationClientOption();
//设置定位监听
mlocationClient.setLocationListener(this);
//设置为高精度定位模式
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//设置定位参数
mlocationClient.setLocationOption(mLocationOption);
// 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
// 注意设置合适的定位时间的间隔(最小间隔支持为2000ms),并且在合适时间调用stopLocation()方法来取消定位请求
// 在定位结束后,在合适的生命周期调用onDestroy()方法
// 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除
mlocationClient.startLocation();
* 停止定位
public void deactivate() {
mListener =
if (mlocationClient != null) {
mlocationClient.stopLocation();
mlocationClient.onDestroy();
mlocationClient =
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:9190次
排名:千里之外
原创:41篇
转载:15篇
评论:19条}

我要回帖

更多关于 高德地图坐标距离计算 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信