高德地图测两点距离android比较精确的

今日长缨在手,何时缚住苍龙。这篇文章主要讲述高德地图测两点距离android比较精确的相关的知识,希望能为你提供帮助。
【高德地图测两点距离android比较精确的】/////参考资料:高德官方:【http://lbs.amap.com/api/android-location-sdk/guide/android-location/getlocation】
主要三个文件,前提配置androidstudio的高德地图使用环境,
给几张图:

高德地图测两点距离android比较精确的

文章图片

2.project展示栏里app目录下的build.grade文件,高德开发手册有提到配置
高德地图测两点距离android比较精确的

文章图片
高德地图测两点距离android比较精确的

文章图片
1 apply plugin: ‘com.android.application‘ 2 3 android { 4compileSdkVersion 26 5buildToolsVersion "26.0.1" 6 //添加map的so文件 7sourceSets{ 8main{ 9jniLibs.srcDirs=[‘libs‘] 10} 11} 12defaultConfig { 13applicationId "com.example.administrator.networkorder" 14minSdkVersion 21 15targetSdkVersion 26 16versionCode 1 17versionName "1.0" 18testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 19ndk { 20//设置支持的SO库架构(开发者可以根据需要,选择一个或多个平台的so)设置地图 21abiFilters "armeabi", "armeabi-v7a", "arm64-v8a", "x86","arm64-v8a","x86_64" 22} 23} 24buildTypes { 25release { 26minifyEnabled false 27proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘ 28} 29} 30 } 31 32 dependencies { 33compile fileTree(include: [‘*.jar‘], dir: ‘libs‘) 34androidTestCompile(‘com.android.support.test.espresso:espresso-core:2.2.2‘, { 35exclude group: ‘com.android.support‘, module: ‘support-annotations‘ 36}) 37compile ‘com.android.support:appcompat-v7:26.+‘ 38//3D地图so及jar 39compile ‘com.amap.api:3dmap:latest.integration‘ 40//定位功能 41compile ‘com.amap.api:location:latest.integration‘ 42//搜索功能 43compile ‘com.amap.api:search:latest.integration‘ 44testCompile ‘junit:junit:4.12‘ 45//compile files(‘libs/AMap3DMap_5.4.0_AMapNavi_5.4.0_AMapSearch_5.3.1_AMapLocation_3.6.0_20170929.jar‘) 46compile files(‘libs/AMap2DMap_5.2.0_AMapLocation_3.6.0_20170918.jar‘) 47 }

View Code3.AndroidManifest.xml文件,配置权限,开发key
高德地图测两点距离android比较精确的

文章图片
高德地图测两点距离android比较精确的

文章图片
1 < !--创建调试版地图key【参照:http://blog.csdn.net/a873228446/article/details/51316979】--> 2 < manifest xmlns:android="http://schemas.android.com/apk/res/android" 3package="com.example.administrator.networkorder"> 4< !--//地图SDK(包含其搜索功能)需要的基础权限,,,,,,高德地图--> 5< !--允许程序打开网络套接字--> 6< uses-permission android:name="android.permission.INTERNET" /> 7< !--允许程序设置内置sd卡的写权限--> 8< uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 9< !--允许程序获取网络状态--> 10< uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 11< !--允许程序访问WiFi网络信息--> 12< uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 13< !--允许程序读写手机状态和身份--> 14< uses-permission android:name="android.permission.READ_PHONE_STATE" /> 15< !--允许程序访问CellID或WiFi热点来获取粗略的位置--> 16< uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 17< !--用于申请调用A-GPS模块--> 18< uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"> < /uses-permission> 19< !--用于申请获取蓝牙信息进行室内定位--> 20< uses-permission android:name="android.permission.BLUETOOTH"> < /uses-permission> 21< uses-permission android:name="android.permission.BLUETOOTH_ADMIN"> < /uses-permission> 22< !--用于进行网络定位--> 23< !--< uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"> < /uses-permission> --> 24< !--用于访问GPS定位--> 25< uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"> < /uses-permission> 26< application 27android:allowBackup="true" 28android:icon="@mipmap/ic_launcher" 29android:label="@string/app_name" 30android:roundIcon="@mipmap/ic_launcher_round" 31android:supportsRtl="true" 32android:theme="@style/AppTheme"> 33< meta-data android:name="com.amap.api.v2.apikey" android:value="https://www.songbingjia.com/android/d4b100ad1856c9109720ea5f855b0638"> 34< !-- //开发者申请的key--> 35< /meta-data> 36< service android:name="com.amap.api.location.APSService"> < /service> 37< activity android:name=".MapTest1"> 38< intent-filter> 39< !--设置启动项--> 40< action android:name="android.intent.action.MAIN"> < /action> 41< category android:name="android.intent.category.LAUNCHER"> < /category> 42< /intent-filter> 43< /activity> 44< /application> 45 46 < /manifest>

View Code4.布局文件,,,就一个页面
高德地图测两点距离android比较精确的

文章图片
高德地图测两点距离android比较精确的

文章图片
1 < ?xml version="1.0" encoding="utf-8"?> 2 < FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 3android:orientation="vertical" android:layout_width="match_parent" 4android:layout_height="match_parent"> 5< com.amap.api.maps.MapView 6android:id="@+id/map1" 7android:layout_width="match_parent" 8android:layout_height="match_parent"> 9< /com.amap.api.maps.MapView> 10< Button 11android:id="@+id/btn_location" 12android:layout_width="match_parent" 13android:layout_height="wrap_content" 14android:text="获取当前位置" 15/> 16< Button 17android:id="@+id/btn_dis" 18android:layout_width="match_parent" 19android:layout_height="wrap_content" 20android:text="计算当前位置距离上个位置距离" 21android:layout_marginTop="60sp" 22/> 23 < /FrameLayout>

View Code5.activity文件,,官网提到三种定位模式,高精度,低功耗,设备定位,本来用的高精度,总感觉就一两米,他就算出20,室内的,改用设备定位就好了,会不会是网络不好,设备定位用的GPS才准点,用的android5,
高德地图测两点距离android比较精确的

文章图片
高德地图测两点距离android比较精确的

文章图片
1 package com.example.administrator.networkorder; 2 3 import android.os.Bundle; 4 import android.support.annotation.Nullable; 5 import android.support.v7.app.AppCompatActivity; 6 import android.util.Log; 7 import android.view.View; 8 import android.widget.Button; 9 import android.widget.Toast; 10 11 import com.amap.api.location.AMapLocation; 12 import com.amap.api.location.AMapLocationClient; 13 import com.amap.api.location.AMapLocationClientOption; 14 import com.amap.api.location.AMapLocationListener; 15 import com.amap.api.location.CoordinateConverter; 16 import com.amap.api.location.DPoint; 17 import com.amap.api.maps.AMap; 18 import com.amap.api.maps.AMapUtils; 19 import com.amap.api.maps.MapView; 20 import com.amap.api.maps.model.LatLng; 21 import com.amap.api.maps.model.MyLocationStyle; 22 23 import java.util.Map; 24 25 /** 26* Created by Administrator on 2017/10/9 0009. 27*/ 28 29 public class MapTest1 extends AppCompatActivity { 30MapView mMapView; 31AMap aMap; 32MyLocationStyle myLocationStyle; 33//---------------------------------------- 34private Button buttonGetLocation; 35AMapLocationClient aMapLocationClient; 36AMapLocationListener aMapLocationListener; 37AMapLocationClientOption aMapLocationClientOption; 38Button buttonCal; 39 40private LatLng latLng_first,latLng_second; 41private boolean changePosition = false; 42private int i1 = 1; 43@Override 44protected void onCreate(@Nullable Bundle savedInstanceState) { 45super.onCreate(savedInstanceState); 46setContentView(R.layout.map); 47mMapView = (MapView) findViewById(R.id.map1); 48mMapView.onCreate(savedInstanceState); 49buttonGetLocation = (Button) findViewById(R.id.btn_location); 50buttonCal = (Button) findViewById(R.id.btn_dis); 51if (aMap == null) 52aMap = mMapView.getMap(); 53 //testLocation(); 54 //showLocation(); 55buttonGetLocation.setOnClickListener(new View.OnClickListener() { 56@Override 57public void onClick(View v) { 58showLocation(); 59} 60}); 61buttonCal.setOnClickListener(new View.OnClickListener() { 62@Override 63public void onClick(View v) { 64String a="定位次数不够"; 65if (i1> 2){ 66a="位置1:"+latLng_first.latitude+","+latLng_first.longitude+"]位置2:[" 67+latLng_second.latitude+","+latLng_second.longitude+"]距离:"+AMapUtils.calculateLineDistance(latLng_first,latLng_second); 68} 69Toast.makeText(MapTest1.this,"s::"+a,Toast.LENGTH_LONG).show(); 70} 71}); 72} 73public float getDistance(LatLng start, LatLng end) {//来自【http://blog.csdn.net/qq_34589749/article/details/53482567】的计算方法 74 75double lon1 = (Math.PI / 180) * start.longitude; 76double lon2 = (Math.PI / 180) * end.longitude; 77double lat1 = (Math.PI / 180) * start.latitude; 78double lat2 = (Math.PI / 180) * end.latitude; 79 80// 地球半径 81double R = 6371; 82 83// 两点间距离 km,如果想要米的话,结果*1000就可以了 84double d = Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)) 85* R; 86float d1 = (float) Math.abs(d*1000); 87d1 = Math.round(d1); 88 //AMapUtils.g 89return d1 ; 90} 91private void showLocation(){ 92aMapLocationClient = new AMapLocationClient(getApplicationContext()); 93aMapLocationListener = new AMapLocationListener() { 94@Override 95public void onLocationChanged(AMapLocation aMapLocation) { 96if (aMapLocation != null) { 97if (aMapLocation.getErrorCode() == 0) { 98 //可在其中解析amapLocation获取相应内容。 99LatLng latLng1 = new LatLng(38.008466,112.446289),latLng2=new LatLng(38.008353,112.446109); //参照:http://blog.csdn.net/u012527802/article/details/42271983 100float distance1=CoordinateConverter.calculateLineDistance(new DPoint(38.008466,112.446289),new DPoint(38.008353,112.446109)); 101if (changePosition){ 102latLng_first = new LatLng(aMapLocation.getLatitude(),aMapLocation.getLongitude()); 103}else 104latLng_second = new LatLng(aMapLocation.getLatitude(),aMapLocation.getLongitude()); 105changePosition = changePosition?false:true; 106i1++; 107Toast.makeText(MapTest1.this,"定位成功:"+aMapLocation.getAddress()+"经纬:["+aMapLocation.getLatitude()+","+aMapLocation.getLongitude()+ 108"],精度:"+aMapLocation.getAccuracy()+"测试两点距离大概三米左右,计算值{不准啊}:"+ 109AMapUtils.calculateLineDistance(latLng1,latLng2)+"官网给的方法结果:"+distance1 110+"博客主1:"+getDistance(latLng1,latLng2),Toast.LENGTH_LONG).show(); 111}else { 112//定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。 113Log.e("AmapError","location Error, ErrCode:" 114+ aMapLocation.getErrorCode() + ", errInfo:" 115+ aMapLocation.getErrorInfo()); 116} 117} 118} 119}; 120aMapLocationClient.setLocationListener(aMapLocationListener); 121aMapLocationClientOption = new AMapLocationClientOption(); 122//使用手机定位的话, 123aMapLocationClientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Device_Sensors); 124aMapLocationClientOption.setOnceLocation(true); 125aMapLocationClientOption.setOnceLocationLatest(true); 126 //aMapLocationClientOption.setInterval(2000); 127aMapLocationClientOption.setNeedAddress(true); 128aMapLocationClientOption.setLocationCacheEnable(false); 129aMapLocationClient.setLocationOption(aMapLocationClientOption); 130aMapLocationClient.startLocation(); 131} 132private void testLocation(){ 133myLocationStyle = new MyLocationStyle(); 134myLocationStyle.interval(1000); 135myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE); 136myLocationStyle.showMyLocation(true); 137aMap.setMyLocationStyle(myLocationStyle); 138aMap.setMyLocationEnabled(true); 139} 140@Override 141protected void onDestroy() { 142super.onDestroy(); 143//在activity执行onDestroy时执行mMapView.onDestroy(),销毁地图 144mMapView.onDestroy(); 145} 146@Override 147protected void onResume() { 148super.onResume(); 149//在activity执行onResume时执行mMapView.onResume (),重新绘制加载地图 150mMapView.onResume(); 151} 152@Override 153protected void onPause() { 154super.onPause(); 155//在activity执行onPause时执行mMapView.onPause (),暂停地图的绘制 156mMapView.onPause(); 157} 158@Override 159protected void onSaveInstanceState(Bundle outState) { 160super.onSaveInstanceState(outState); 161//在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState),保存地图当前的状态 162mMapView.onSaveInstanceState(outState); 163} 164 }

View Code6.试了下,又出问题了,定位慢,还不准了,2,3米测成31米,三个都不准确,可能没用用室内定位的方式》》???,,,

    推荐阅读