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

/////参考资料:高德官方:【http://lbs.amap.com/api/android-location-sdk/guide/android-location/getlocation】

主要三个文件,前提配置androidstudio的高德地图使用环境,

给几张图:高德地图测两点距离android比较精确的

2.project展示栏里app目录下的build.grade文件,高德开发手册有提到配置

 apply plugin: 'com.android.application'

 android {
     compileSdkVersion 26
     buildToolsVersion "26.0.1"
 //    添加map的so文件
     sourceSets{
         main{
             jniLibs.srcDirs=['libs']
         }
     }
     defaultConfig {
         applicationId "com.example.administrator.networkorder"
         minSdkVersion 21
         targetSdkVersion 26
         versionCode 1
         versionName "1.0"
         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
         ndk {
             //设置支持的SO库架构(开发者可以根据需要,选择一个或多个平台的so)设置地图
             abiFilters "armeabi", "armeabi-v7a", "arm64-v8a", "x86","arm64-v8a","x86_64"
         }
     }
     buildTypes {
         release {
             minifyEnabled false
             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
         }
     }
 }

 dependencies {
     compile fileTree(include: ['*.jar'], dir: 'libs')
     androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
         exclude group: 'com.android.support', module: 'support-annotations'
     })
     compile 'com.android.support:appcompat-v7:26.+'
     //3D地图so及jar
     compile 'com.amap.api:3dmap:latest.integration'
     //定位功能
     compile 'com.amap.api:location:latest.integration'
     //搜索功能
     compile 'com.amap.api:search:latest.integration'
     testCompile 'junit:junit:4.12'
     //    compile files('libs/AMap3DMap_5.4.0_AMapNavi_5.4.0_AMapSearch_5.3.1_AMapLocation_3.6.0_20170929.jar')
     compile files('libs/AMap2DMap_5.2.0_AMapLocation_3.6.0_20170918.jar')
 }

3.AndroidManifest.xml文件,配置权限,开发key

 <!--创建调试版地图key【参照:http://blog.csdn.net/a873228446/article/details/51316979】-->
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.example.administrator.networkorder">
     <!--//地图SDK(包含其搜索功能)需要的基础权限,,,,,,高德地图-->
     <!--允许程序打开网络套接字-->
     <uses-permission android:name="android.permission.INTERNET" />
     <!--允许程序设置内置sd卡的写权限-->
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
     <!--允许程序获取网络状态-->
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
     <!--允许程序访问WiFi网络信息-->
     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
     <!--允许程序读写手机状态和身份-->
     <uses-permission android:name="android.permission.READ_PHONE_STATE" />
     <!--允许程序访问CellID或WiFi热点来获取粗略的位置-->
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
     <!--用于申请调用A-GPS模块-->
     <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission>
     <!--用于申请获取蓝牙信息进行室内定位-->
     <uses-permission android:name="android.permission.BLUETOOTH"></uses-permission>
     <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"></uses-permission>
     <!--用于进行网络定位-->
     <!--<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>-->
     <!--用于访问GPS定位-->
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
     <application
         android:allowBackup="true"
         android:icon="@mipmap/ic_launcher"
         android:label="@string/app_name"
         android:roundIcon="@mipmap/ic_launcher_round"
         android:supportsRtl="true"
         android:theme="@style/AppTheme">
         <meta-data android:name="com.amap.api.v2.apikey" android:value="d4b100ad1856c9109720ea5f855b0638">
            <!-- //开发者申请的key-->
         </meta-data>
         <service android:name="com.amap.api.location.APSService"></service>
         <activity android:name=".MapTest1">
             <intent-filter>
                 <!--设置启动项-->
                 <action android:name="android.intent.action.MAIN"></action>
                 <category android:name="android.intent.category.LAUNCHER"></category>
             </intent-filter>
         </activity>
     </application>

 </manifest>

4.布局文件,,,就一个页面

 <?xml version="1.0" encoding="utf-8"?>
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical" android:layout_width="match_parent"
     android:layout_height="match_parent">
     <com.amap.api.maps.MapView
         android:id="@+id/map1"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
     </com.amap.api.maps.MapView>
     <Button
         android:id="@+id/btn_location"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="获取当前位置"
         />
     <Button
         android:id="@+id/btn_dis"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="计算当前位置距离上个位置距离"
         android:layout_marginTop="60sp"
         />
 </FrameLayout>

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

 package com.example.administrator.networkorder;

 import android.os.Bundle;
 import android.support.annotation.Nullable;
 import android.support.v7.app.AppCompatActivity;
 import android.util.Log;
 import android.view.View;
 import android.widget.Button;
 import android.widget.Toast;

 import com.amap.api.location.AMapLocation;
 import com.amap.api.location.AMapLocationClient;
 import com.amap.api.location.AMapLocationClientOption;
 import com.amap.api.location.AMapLocationListener;
 import com.amap.api.location.CoordinateConverter;
 import com.amap.api.location.DPoint;
 import com.amap.api.maps.AMap;
 import com.amap.api.maps.AMapUtils;
 import com.amap.api.maps.MapView;
 import com.amap.api.maps.model.LatLng;
 import com.amap.api.maps.model.MyLocationStyle;

 import java.util.Map;

 /**
  * Created by Administrator on 2017/10/9 0009.
  */

 public class MapTest1 extends AppCompatActivity {
     MapView mMapView;
     AMap aMap;
     MyLocationStyle myLocationStyle;
     //----------------------------------------
     private Button buttonGetLocation;
     AMapLocationClient aMapLocationClient;
     AMapLocationListener aMapLocationListener;
     AMapLocationClientOption aMapLocationClientOption;
     Button buttonCal;

     private LatLng latLng_first,latLng_second;
     private boolean changePosition = false;
     private int i1 = 1;
     @Override
     protected void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.map);
         mMapView = (MapView) findViewById(R.id.map1);
         mMapView.onCreate(savedInstanceState);
         buttonGetLocation = (Button) findViewById(R.id.btn_location);
         buttonCal = (Button) findViewById(R.id.btn_dis);
         if (aMap == null)
             aMap = mMapView.getMap();
 //        testLocation();
 //        showLocation();
         buttonGetLocation.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 showLocation();
             }
         });
         buttonCal.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 String a="定位次数不够";
                 if (i1>2){
                     a="位置1:"+latLng_first.latitude+","+latLng_first.longitude+"]位置2:["
                             +latLng_second.latitude+","+latLng_second.longitude+"]距离:"+AMapUtils.calculateLineDistance(latLng_first,latLng_second);
                 }
                 Toast.makeText(MapTest1.this,"s::"+a,Toast.LENGTH_LONG).show();
             }
         });
     }
     public float getDistance(LatLng start, LatLng end) {//来自【http://blog.csdn.net/qq_34589749/article/details/53482567】的计算方法

         double lon1 = (Math.PI / 180) * start.longitude;
         double lon2 = (Math.PI / 180) * end.longitude;
         double lat1 = (Math.PI / 180) * start.latitude;
         double lat2 = (Math.PI / 180) * end.latitude;

         // 地球半径
         double R = 6371;

         // 两点间距离 km,如果想要米的话,结果*1000就可以了
         double d = Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1))
                 * R;
         float d1 = (float) Math.abs(d*1000);
         d1 = Math.round(d1);
 //        AMapUtils.g
         return d1 ;
     }
     private void showLocation(){
         aMapLocationClient = new AMapLocationClient(getApplicationContext());
         aMapLocationListener = new AMapLocationListener() {
             @Override
             public void onLocationChanged(AMapLocation aMapLocation) {
                 if (aMapLocation != null) {
                     if (aMapLocation.getErrorCode() == 0) {
 //可在其中解析amapLocation获取相应内容。
                         LatLng latLng1 = new LatLng(38.008466,112.446289),latLng2=new LatLng(38.008353,112.446109);//参照:http://blog.csdn.net/u012527802/article/details/42271983
                         float distance1=CoordinateConverter.calculateLineDistance(new DPoint(38.008466,112.446289),new DPoint(38.008353,112.446109));
                         if (changePosition){
                             latLng_first = new LatLng(aMapLocation.getLatitude(),aMapLocation.getLongitude());
                         }else
                             latLng_second = new LatLng(aMapLocation.getLatitude(),aMapLocation.getLongitude());
                         changePosition = changePosition?false:true;
                         i1++;
                         Toast.makeText(MapTest1.this,"定位成功:"+aMapLocation.getAddress()+"经纬:["+aMapLocation.getLatitude()+","+aMapLocation.getLongitude()+
                                 "],精度:"+aMapLocation.getAccuracy()+"测试两点距离大概三米左右,计算值{不准啊}:"+
                                 AMapUtils.calculateLineDistance(latLng1,latLng2)+"官网给的方法结果:"+distance1
                                 +"博客主1:"+getDistance(latLng1,latLng2),Toast.LENGTH_LONG).show();
                     }else {
                         //定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。
                         Log.e("AmapError","location Error, ErrCode:"
                                 + aMapLocation.getErrorCode() + ", errInfo:"
                                 + aMapLocation.getErrorInfo());
                     }
                 }
             }
         };
         aMapLocationClient.setLocationListener(aMapLocationListener);
         aMapLocationClientOption = new AMapLocationClientOption();
         //使用手机定位的话,
         aMapLocationClientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Device_Sensors);
         aMapLocationClientOption.setOnceLocation(true);
         aMapLocationClientOption.setOnceLocationLatest(true);
 //        aMapLocationClientOption.setInterval(2000);
         aMapLocationClientOption.setNeedAddress(true);
         aMapLocationClientOption.setLocationCacheEnable(false);
         aMapLocationClient.setLocationOption(aMapLocationClientOption);
         aMapLocationClient.startLocation();
     }
     private void testLocation(){
         myLocationStyle = new MyLocationStyle();
         myLocationStyle.interval(1000);
         myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);
         myLocationStyle.showMyLocation(true);
         aMap.setMyLocationStyle(myLocationStyle);
         aMap.setMyLocationEnabled(true);
     }
     @Override
     protected void onDestroy() {
         super.onDestroy();
         //在activity执行onDestroy时执行mMapView.onDestroy(),销毁地图
         mMapView.onDestroy();
     }
     @Override
     protected void onResume() {
         super.onResume();
         //在activity执行onResume时执行mMapView.onResume (),重新绘制加载地图
         mMapView.onResume();
     }
     @Override
     protected void onPause() {
         super.onPause();
         //在activity执行onPause时执行mMapView.onPause (),暂停地图的绘制
         mMapView.onPause();
     }
     @Override
     protected void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
         //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState),保存地图当前的状态
         mMapView.onSaveInstanceState(outState);
     }
 }

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

上一篇:第一章-第二题Unity3D游戏引擎相关--By林培文


下一篇:Android开发 Unity3D基础 Android Development