标签:java files parent extern line encoding point context pos
/////参考资料:高德官方:【http://lbs.amap.com/api/android-location-sdk/guide/android-location/getlocation】
主要三个文件,前提配置androidstudio的高德地图使用环境,
给几张图:
2.project展示栏里app目录下的build.grade文件,高德开发手册有提到配置
1 apply plugin: ‘com.android.application‘ 2 3 android { 4 compileSdkVersion 26 5 buildToolsVersion "26.0.1" 6 // 添加map的so文件 7 sourceSets{ 8 main{ 9 jniLibs.srcDirs=[‘libs‘] 10 } 11 } 12 defaultConfig { 13 applicationId "com.example.administrator.networkorder" 14 minSdkVersion 21 15 targetSdkVersion 26 16 versionCode 1 17 versionName "1.0" 18 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 19 ndk { 20 //设置支持的SO库架构(开发者可以根据需要,选择一个或多个平台的so)设置地图 21 abiFilters "armeabi", "armeabi-v7a", "arm64-v8a", "x86","arm64-v8a","x86_64" 22 } 23 } 24 buildTypes { 25 release { 26 minifyEnabled false 27 proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘ 28 } 29 } 30 } 31 32 dependencies { 33 compile fileTree(include: [‘*.jar‘], dir: ‘libs‘) 34 androidTestCompile(‘com.android.support.test.espresso:espresso-core:2.2.2‘, { 35 exclude group: ‘com.android.support‘, module: ‘support-annotations‘ 36 }) 37 compile ‘com.android.support:appcompat-v7:26.+‘ 38 //3D地图so及jar 39 compile ‘com.amap.api:3dmap:latest.integration‘ 40 //定位功能 41 compile ‘com.amap.api:location:latest.integration‘ 42 //搜索功能 43 compile ‘com.amap.api:search:latest.integration‘ 44 testCompile ‘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‘) 46 compile files(‘libs/AMap2DMap_5.2.0_AMapLocation_3.6.0_20170918.jar‘) 47 }
3.AndroidManifest.xml文件,配置权限,开发key
1 <!--创建调试版地图key【参照:http://blog.csdn.net/a873228446/article/details/51316979】--> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="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 27 android:allowBackup="true" 28 android:icon="@mipmap/ic_launcher" 29 android:label="@string/app_name" 30 android:roundIcon="@mipmap/ic_launcher_round" 31 android:supportsRtl="true" 32 android:theme="@style/AppTheme"> 33 <meta-data android:name="com.amap.api.v2.apikey" android:value="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>
4.布局文件,,,就一个页面
1 <?xml version="1.0" encoding="utf-8"?> 2 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 <com.amap.api.maps.MapView 6 android:id="@+id/map1" 7 android:layout_width="match_parent" 8 android:layout_height="match_parent"> 9 </com.amap.api.maps.MapView> 10 <Button 11 android:id="@+id/btn_location" 12 android:layout_width="match_parent" 13 android:layout_height="wrap_content" 14 android:text="获取当前位置" 15 /> 16 <Button 17 android:id="@+id/btn_dis" 18 android:layout_width="match_parent" 19 android:layout_height="wrap_content" 20 android:text="计算当前位置距离上个位置距离" 21 android:layout_marginTop="60sp" 22 /> 23 </FrameLayout>
5.activity文件,,官网提到三种定位模式,高精度,低功耗,设备定位,本来用的高精度,总感觉就一两米,他就算出20,室内的,改用设备定位就好了,会不会是网络不好,设备定位用的GPS才准点,用的android5,
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 { 30 MapView mMapView; 31 AMap aMap; 32 MyLocationStyle myLocationStyle; 33 //---------------------------------------- 34 private Button buttonGetLocation; 35 AMapLocationClient aMapLocationClient; 36 AMapLocationListener aMapLocationListener; 37 AMapLocationClientOption aMapLocationClientOption; 38 Button buttonCal; 39 40 private LatLng latLng_first,latLng_second; 41 private boolean changePosition = false; 42 private int i1 = 1; 43 @Override 44 protected void onCreate(@Nullable Bundle savedInstanceState) { 45 super.onCreate(savedInstanceState); 46 setContentView(R.layout.map); 47 mMapView = (MapView) findViewById(R.id.map1); 48 mMapView.onCreate(savedInstanceState); 49 buttonGetLocation = (Button) findViewById(R.id.btn_location); 50 buttonCal = (Button) findViewById(R.id.btn_dis); 51 if (aMap == null) 52 aMap = mMapView.getMap(); 53 // testLocation(); 54 // showLocation(); 55 buttonGetLocation.setOnClickListener(new View.OnClickListener() { 56 @Override 57 public void onClick(View v) { 58 showLocation(); 59 } 60 }); 61 buttonCal.setOnClickListener(new View.OnClickListener() { 62 @Override 63 public void onClick(View v) { 64 String a="定位次数不够"; 65 if (i1>2){ 66 a="位置1:"+latLng_first.latitude+","+latLng_first.longitude+"]位置2:[" 67 +latLng_second.latitude+","+latLng_second.longitude+"]距离:"+AMapUtils.calculateLineDistance(latLng_first,latLng_second); 68 } 69 Toast.makeText(MapTest1.this,"s::"+a,Toast.LENGTH_LONG).show(); 70 } 71 }); 72 } 73 public float getDistance(LatLng start, LatLng end) {//来自【http://blog.csdn.net/qq_34589749/article/details/53482567】的计算方法 74 75 double lon1 = (Math.PI / 180) * start.longitude; 76 double lon2 = (Math.PI / 180) * end.longitude; 77 double lat1 = (Math.PI / 180) * start.latitude; 78 double lat2 = (Math.PI / 180) * end.latitude; 79 80 // 地球半径 81 double R = 6371; 82 83 // 两点间距离 km,如果想要米的话,结果*1000就可以了 84 double d = Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)) 85 * R; 86 float d1 = (float) Math.abs(d*1000); 87 d1 = Math.round(d1); 88 // AMapUtils.g 89 return d1 ; 90 } 91 private void showLocation(){ 92 aMapLocationClient = new AMapLocationClient(getApplicationContext()); 93 aMapLocationListener = new AMapLocationListener() { 94 @Override 95 public void onLocationChanged(AMapLocation aMapLocation) { 96 if (aMapLocation != null) { 97 if (aMapLocation.getErrorCode() == 0) { 98 //可在其中解析amapLocation获取相应内容。 99 LatLng latLng1 = new LatLng(38.008466,112.446289),latLng2=new LatLng(38.008353,112.446109);//参照:http://blog.csdn.net/u012527802/article/details/42271983 100 float distance1=CoordinateConverter.calculateLineDistance(new DPoint(38.008466,112.446289),new DPoint(38.008353,112.446109)); 101 if (changePosition){ 102 latLng_first = new LatLng(aMapLocation.getLatitude(),aMapLocation.getLongitude()); 103 }else 104 latLng_second = new LatLng(aMapLocation.getLatitude(),aMapLocation.getLongitude()); 105 changePosition = changePosition?false:true; 106 i1++; 107 Toast.makeText(MapTest1.this,"定位成功:"+aMapLocation.getAddress()+"经纬:["+aMapLocation.getLatitude()+","+aMapLocation.getLongitude()+ 108 "],精度:"+aMapLocation.getAccuracy()+"测试两点距离大概三米左右,计算值{不准啊}:"+ 109 AMapUtils.calculateLineDistance(latLng1,latLng2)+"官网给的方法结果:"+distance1 110 +"博客主1:"+getDistance(latLng1,latLng2),Toast.LENGTH_LONG).show(); 111 }else { 112 //定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。 113 Log.e("AmapError","location Error, ErrCode:" 114 + aMapLocation.getErrorCode() + ", errInfo:" 115 + aMapLocation.getErrorInfo()); 116 } 117 } 118 } 119 }; 120 aMapLocationClient.setLocationListener(aMapLocationListener); 121 aMapLocationClientOption = new AMapLocationClientOption(); 122 //使用手机定位的话, 123 aMapLocationClientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Device_Sensors); 124 aMapLocationClientOption.setOnceLocation(true); 125 aMapLocationClientOption.setOnceLocationLatest(true); 126 // aMapLocationClientOption.setInterval(2000); 127 aMapLocationClientOption.setNeedAddress(true); 128 aMapLocationClientOption.setLocationCacheEnable(false); 129 aMapLocationClient.setLocationOption(aMapLocationClientOption); 130 aMapLocationClient.startLocation(); 131 } 132 private void testLocation(){ 133 myLocationStyle = new MyLocationStyle(); 134 myLocationStyle.interval(1000); 135 myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE); 136 myLocationStyle.showMyLocation(true); 137 aMap.setMyLocationStyle(myLocationStyle); 138 aMap.setMyLocationEnabled(true); 139 } 140 @Override 141 protected void onDestroy() { 142 super.onDestroy(); 143 //在activity执行onDestroy时执行mMapView.onDestroy(),销毁地图 144 mMapView.onDestroy(); 145 } 146 @Override 147 protected void onResume() { 148 super.onResume(); 149 //在activity执行onResume时执行mMapView.onResume (),重新绘制加载地图 150 mMapView.onResume(); 151 } 152 @Override 153 protected void onPause() { 154 super.onPause(); 155 //在activity执行onPause时执行mMapView.onPause (),暂停地图的绘制 156 mMapView.onPause(); 157 } 158 @Override 159 protected void onSaveInstanceState(Bundle outState) { 160 super.onSaveInstanceState(outState); 161 //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState),保存地图当前的状态 162 mMapView.onSaveInstanceState(outState); 163 } 164 }
6.试了下,又出问题了,定位慢,还不准了,2,3米测成31米,三个都不准确,可能没用用室内定位的方式》》???,,,
标签:java files parent extern line encoding point context pos
原文地址:http://www.cnblogs.com/CannotInputChinese/p/7643376.html