├── .gitignore ├── LICENSE ├── README.md ├── android_studio ├── AMapLocationDemo │ ├── .gitignore │ ├── .idea │ │ ├── .name │ │ ├── compiler.xml │ │ ├── copyright │ │ │ └── profiles_settings.xml │ │ ├── dictionaries │ │ │ └── hongming_wang.xml │ │ ├── encodings.xml │ │ ├── gradle.xml │ │ ├── inspectionProfiles │ │ │ ├── Project_Default.xml │ │ │ └── profiles_settings.xml │ │ ├── misc.xml │ │ ├── modules.xml │ │ └── runConfigurations.xml │ ├── app │ │ ├── .gitignore │ │ ├── build.gradle │ │ ├── libs │ │ │ ├── AMap_Location_V3.3.0_20170118.jar │ │ │ ├── Android_Map3D_SDK_V4.1.2_20161104.jar │ │ │ ├── android-support-v4.jar │ │ │ └── armeabi │ │ │ │ ├── libgdinamapv4sdk752.so │ │ │ │ └── libgdinamapv4sdk752ex.so │ │ ├── proguard-rules.pro │ │ └── src │ │ │ ├── main │ │ │ ├── AndroidManifest.xml │ │ │ ├── java │ │ │ │ └── com │ │ │ │ │ └── amap │ │ │ │ │ └── location │ │ │ │ │ └── demo │ │ │ │ │ ├── Alarm_Location_Activity.java │ │ │ │ │ ├── Assistant_Location_Activity.java │ │ │ │ │ ├── CheckPermissionsActivity.java │ │ │ │ │ ├── Const.java │ │ │ │ │ ├── ErrorCode_Activity.java │ │ │ │ │ ├── GeoFence_Activity.java │ │ │ │ │ ├── GeoFence_District_Activity.java │ │ │ │ │ ├── GeoFence_Keyword_Activity.java │ │ │ │ │ ├── GeoFence_Multiple_Activity.java │ │ │ │ │ ├── GeoFence_Nearby_Activity.java │ │ │ │ │ ├── GeoFence_Old_Activity.java │ │ │ │ │ ├── GeoFence_Polygon_Activity.java │ │ │ │ │ ├── GeoFence_Round_Activity.java │ │ │ │ │ ├── LastLocation_Activity.java │ │ │ │ │ ├── Location_Activity.java │ │ │ │ │ ├── StartActivity.java │ │ │ │ │ ├── Tools_Activity.java │ │ │ │ │ ├── Utils.java │ │ │ │ │ └── view │ │ │ │ │ └── FeatureView.java │ │ │ └── res │ │ │ │ ├── drawable-hdpi │ │ │ │ ├── gps_point.png │ │ │ │ └── ic_launcher.png │ │ │ │ ├── drawable-ldpi │ │ │ │ └── ic_launcher.png │ │ │ │ ├── drawable-mdpi │ │ │ │ └── ic_launcher.png │ │ │ │ ├── drawable-xhdpi │ │ │ │ └── ic_launcher.png │ │ │ │ ├── layout │ │ │ │ ├── activity_alarm_location.xml │ │ │ │ ├── activity_assistant_location.xml │ │ │ │ ├── activity_errcode.xml │ │ │ │ ├── activity_geofence.xml │ │ │ │ ├── activity_geofence_new.xml │ │ │ │ ├── activity_geofence_old.xml │ │ │ │ ├── activity_lastlocation.xml │ │ │ │ ├── activity_location.xml │ │ │ │ ├── activity_start.xml │ │ │ │ ├── activity_tools.xml │ │ │ │ ├── feature.xml │ │ │ │ ├── plugin_geofence_map.xml │ │ │ │ ├── plugin_geofence_option.xml │ │ │ │ └── plugin_location_option.xml │ │ │ │ └── values │ │ │ │ ├── colors.xml │ │ │ │ ├── dimens.xml │ │ │ │ └── strings.xml │ │ │ └── test │ │ │ └── java │ │ │ └── com │ │ │ └── amap │ │ │ └── location │ │ │ └── demo │ │ │ └── ExampleUnitTest.java │ ├── build.gradle │ ├── gradle.properties │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ └── settings.gradle └── Readme.md └── eclipse ├── AMapLocationDemo ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.core.resources.prefs │ └── org.eclipse.jdt.core.prefs ├── AndroidManifest.xml ├── assets │ └── location.html ├── ic_launcher-web.png ├── libs │ ├── AMap_Location_V3.3.0_20170118.jar │ ├── Android_Map3D_SDK_V4.1.2_20161104.jar │ ├── android-support-v4.jar │ └── armeabi │ │ ├── libgdinamapv4sdk752.so │ │ └── libgdinamapv4sdk752ex.so ├── proguard-project.txt ├── project.properties ├── res │ ├── drawable-hdpi │ │ ├── gps_point.png │ │ └── ic_launcher.png │ ├── drawable-ldpi │ │ └── ic_launcher.png │ ├── drawable-mdpi │ │ └── ic_launcher.png │ ├── drawable-xhdpi │ │ └── ic_launcher.png │ ├── layout │ │ ├── activity_alarm_location.xml │ │ ├── activity_assistant_location.xml │ │ ├── activity_errcode.xml │ │ ├── activity_geofence.xml │ │ ├── activity_geofence_new.xml │ │ ├── activity_geofence_old.xml │ │ ├── activity_lastlocation.xml │ │ ├── activity_location.xml │ │ ├── activity_start.xml │ │ ├── activity_tools.xml │ │ ├── feature.xml │ │ ├── plugin_geofence_map.xml │ │ ├── plugin_geofence_option.xml │ │ └── plugin_location_option.xml │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ └── strings.xml └── src │ └── com │ └── amap │ └── location │ └── demo │ ├── Alarm_Location_Activity.java │ ├── Assistant_Location_Activity.java │ ├── CheckPermissionsActivity.java │ ├── Const.java │ ├── ErrorCode_Activity.java │ ├── GeoFence_Activity.java │ ├── GeoFence_District_Activity.java │ ├── GeoFence_Keyword_Activity.java │ ├── GeoFence_Multiple_Activity.java │ ├── GeoFence_Nearby_Activity.java │ ├── GeoFence_Old_Activity.java │ ├── GeoFence_Polygon_Activity.java │ ├── GeoFence_Round_Activity.java │ ├── LastLocation_Activity.java │ ├── Location_Activity.java │ ├── StartActivity.java │ ├── Tools_Activity.java │ ├── Utils.java │ └── view │ └── FeatureView.java └── Readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | # Intellij 36 | *.iml 37 | .idea/workspace.xml 38 | 39 | # Keystore files 40 | *.jks 41 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Android_Location_Demo 2 | 高德定位SDK示例 3 | ------------ 4 | 5 | 本工程为高德定位Android SDK官方Demo 6 | 7 | ##前述: 8 | - [高德官方网站申请key](http://lbs.amap.com/dev/#/). 9 | - 阅读[参考手册](http://amappc.cn-hangzhou.oss-pub.aliyun-inc.com/lbs/static/unzip/Android_Location_Doc/index.html). 10 | - 如果有任何疑问也可以发问题到[官方论坛](http://lbsbbs.amap.com/forum.php?mod=forumdisplay&fid=48). 11 | 12 | ##包含的功能点 13 | - 获取定位数据. 14 | - 地理围栏 15 | - H5辅助定位 16 | - 地图计算工具 17 | - 获取最后一次位置 18 | - 定时唤起CPU 19 | - 错误码说明 20 | 21 | ##使用方法: 22 | [使用方法](http://lbs.amap.com/api/android-location-sdk/locationsummary/) -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/.idea/.name: -------------------------------------------------------------------------------- 1 | AMapLocationDemo -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/.idea/dictionaries/hongming_wang.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 36 | -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | /*.apk 3 | -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | sourceSets { 5 | main { 6 | jniLibs.srcDirs = ['libs'] 7 | } 8 | } 9 | signingConfigs { 10 | /** 11 | * 设置你的keystore相关 12 | * demo中只是一个示例,使用时请根据实际情况进行配置 13 | */ 14 | // debug { 15 | // //keystore中key的别名 16 | // keyAlias 'androiddebugkey' 17 | // //keystore中key的密码 18 | // keyPassword 'android' 19 | // //keystore的文件路径,可以是绝对路径也可以是相对路径 20 | // storeFile file('D:/keystore/debug_loc.keystore') 21 | // //keystore的密码 22 | // storePassword 'android' 23 | // } 24 | } 25 | compileSdkVersion 23 26 | buildToolsVersion '23.0.1' 27 | 28 | defaultConfig { 29 | applicationId "com.amap.location.demo" 30 | minSdkVersion 14 31 | targetSdkVersion 22 32 | versionCode 2 33 | versionName "2.0" 34 | } 35 | buildTypes { 36 | release { 37 | minifyEnabled false 38 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 39 | } 40 | } 41 | } 42 | 43 | dependencies { 44 | compile fileTree(include: ['*.jar'], dir: 'libs') 45 | } 46 | -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/app/libs/AMap_Location_V3.3.0_20170118.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amapapi/Android_Location_Demo/a1f994a22f3400556142d6273aa48e2de87040aa/android_studio/AMapLocationDemo/app/libs/AMap_Location_V3.3.0_20170118.jar -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/app/libs/Android_Map3D_SDK_V4.1.2_20161104.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amapapi/Android_Location_Demo/a1f994a22f3400556142d6273aa48e2de87040aa/android_studio/AMapLocationDemo/app/libs/Android_Map3D_SDK_V4.1.2_20161104.jar -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/app/libs/android-support-v4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amapapi/Android_Location_Demo/a1f994a22f3400556142d6273aa48e2de87040aa/android_studio/AMapLocationDemo/app/libs/android-support-v4.jar -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/app/libs/armeabi/libgdinamapv4sdk752.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amapapi/Android_Location_Demo/a1f994a22f3400556142d6273aa48e2de87040aa/android_studio/AMapLocationDemo/app/libs/armeabi/libgdinamapv4sdk752.so -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/app/libs/armeabi/libgdinamapv4sdk752ex.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amapapi/Android_Location_Demo/a1f994a22f3400556142d6273aa48e2de87040aa/android_studio/AMapLocationDemo/app/libs/armeabi/libgdinamapv4sdk752ex.so -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\android-sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 55 | 56 | 59 | 60 | 61 | 62 | 63 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 79 | 80 | 85 | 86 | 91 | 92 | 97 | 98 | 103 | 104 | 109 | 110 | 115 | 116 | 121 | 122 | 127 | 128 | 133 | 134 | 139 | 140 | 145 | 146 | 151 | 152 | 157 | 158 | 159 | 160 | 161 | 162 | -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/app/src/main/java/com/amap/location/demo/Alarm_Location_Activity.java: -------------------------------------------------------------------------------- 1 | package com.amap.location.demo; 2 | 3 | import com.amap.api.location.AMapLocation; 4 | import com.amap.api.location.AMapLocationClient; 5 | import com.amap.api.location.AMapLocationClientOption; 6 | import com.amap.api.location.AMapLocationClientOption.AMapLocationMode; 7 | import com.amap.api.location.AMapLocationListener; 8 | 9 | import android.app.Activity; 10 | import android.app.AlarmManager; 11 | import android.app.PendingIntent; 12 | import android.content.BroadcastReceiver; 13 | import android.content.Context; 14 | import android.content.Intent; 15 | import android.content.IntentFilter; 16 | import android.os.Bundle; 17 | import android.os.Handler; 18 | import android.os.Message; 19 | import android.os.SystemClock; 20 | import android.text.TextUtils; 21 | import android.view.View; 22 | import android.view.View.OnClickListener; 23 | import android.widget.Button; 24 | import android.widget.CheckBox; 25 | import android.widget.EditText; 26 | import android.widget.TextView; 27 | import android.widget.Toast; 28 | 29 | /** 30 | * 后台唤醒定位 31 | * @创建时间:2016年1月8日 上午11:25:01 32 | * @项目名称: AMapLocationDemo2.x 33 | * @author hongming.wang 34 | * @文件名称:Alarm_Location_Activity.java 35 | * @类型名称:Alarm_Location_Activity 36 | * @since 2.3.0 37 | */ 38 | public class Alarm_Location_Activity extends CheckPermissionsActivity implements 39 | OnClickListener, AMapLocationListener { 40 | private EditText etInterval; 41 | private EditText etAlarm; 42 | private CheckBox cbAddress; 43 | private CheckBox cbGpsFirst; 44 | private TextView tvReult; 45 | private Button btLocation; 46 | 47 | private AMapLocationClient locationClient = null; 48 | private AMapLocationClientOption locationOption = null; 49 | 50 | private Intent alarmIntent = null; 51 | private PendingIntent alarmPi = null; 52 | private AlarmManager alarm = null; 53 | 54 | @Override 55 | protected void onCreate(Bundle savedInstanceState) { 56 | super.onCreate(savedInstanceState); 57 | setContentView(R.layout.activity_alarm_location); 58 | setTitle(R.string.title_alarmCPU); 59 | 60 | etInterval = (EditText) findViewById(R.id.et_interval); 61 | etAlarm = (EditText) findViewById(R.id.et_alarm); 62 | cbAddress = (CheckBox) findViewById(R.id.cb_needAddress); 63 | cbGpsFirst = (CheckBox) findViewById(R.id.cb_gpsFirst); 64 | tvReult = (TextView) findViewById(R.id.tv_result); 65 | btLocation = (Button) findViewById(R.id.bt_location); 66 | 67 | btLocation.setOnClickListener(this); 68 | 69 | locationClient = new AMapLocationClient(this.getApplicationContext()); 70 | locationOption = new AMapLocationClientOption(); 71 | // 设置定位模式为高精度模式 72 | locationOption.setLocationMode(AMapLocationMode.Hight_Accuracy); 73 | // 设置定位监听 74 | locationClient.setLocationListener(this); 75 | 76 | // 创建Intent对象,action为LOCATION 77 | alarmIntent = new Intent(); 78 | alarmIntent.setAction("LOCATION"); 79 | IntentFilter ift = new IntentFilter(); 80 | 81 | // 定义一个PendingIntent对象,PendingIntent.getBroadcast包含了sendBroadcast的动作。 82 | // 也就是发送了action 为"LOCATION"的intent 83 | alarmPi = PendingIntent.getBroadcast(this, 0, alarmIntent, 0); 84 | // AlarmManager对象,注意这里并不是new一个对象,Alarmmanager为系统级服务 85 | alarm = (AlarmManager) getSystemService(ALARM_SERVICE); 86 | 87 | //动态注册一个广播 88 | IntentFilter filter = new IntentFilter(); 89 | filter.addAction("LOCATION"); 90 | registerReceiver(alarmReceiver, filter); 91 | } 92 | 93 | @Override 94 | protected void onDestroy() { 95 | super.onDestroy(); 96 | if (null != locationClient) { 97 | /** 98 | * 如果AMapLocationClient是在当前Activity实例化的, 99 | * 在Activity的onDestroy中一定要执行AMapLocationClient的onDestroy 100 | */ 101 | locationClient.onDestroy(); 102 | locationClient = null; 103 | locationOption = null; 104 | } 105 | 106 | if(null != alarmReceiver){ 107 | unregisterReceiver(alarmReceiver); 108 | alarmReceiver = null; 109 | } 110 | } 111 | 112 | /** 113 | * 设置控件的可用状态 114 | */ 115 | private void setViewEnable(boolean isEnable) { 116 | etInterval.setEnabled(isEnable); 117 | etAlarm.setEnabled(isEnable); 118 | cbAddress.setEnabled(isEnable); 119 | cbGpsFirst.setEnabled(isEnable); 120 | } 121 | 122 | @Override 123 | public void onClick(View v) { 124 | if (v.getId() == R.id.bt_location) { 125 | if (btLocation.getText().equals( 126 | getResources().getString(R.string.startLocation))) { 127 | setViewEnable(false); 128 | initOption(); 129 | int alarmInterval = 5; 130 | String str = etAlarm.getText().toString(); 131 | if(!TextUtils.isEmpty(str)){ 132 | alarmInterval = Integer.parseInt(str); 133 | } 134 | 135 | btLocation.setText(getResources().getString( 136 | R.string.stopLocation)); 137 | // 设置定位参数 138 | locationClient.setLocationOption(locationOption); 139 | // 启动定位 140 | locationClient.startLocation(); 141 | mHandler.sendEmptyMessage(Utils.MSG_LOCATION_START); 142 | 143 | if(null != alarm){ 144 | //设置一个闹钟,2秒之后每隔一段时间执行启动一次定位程序 145 | alarm.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 2*1000, 146 | alarmInterval * 1000, alarmPi); 147 | } 148 | } else { 149 | setViewEnable(true); 150 | btLocation.setText(getResources().getString( 151 | R.string.startLocation)); 152 | // 停止定位 153 | locationClient.stopLocation(); 154 | mHandler.sendEmptyMessage(Utils.MSG_LOCATION_STOP); 155 | 156 | //停止定位的时候取消闹钟 157 | if(null != alarm){ 158 | alarm.cancel(alarmPi); 159 | } 160 | } 161 | } 162 | } 163 | 164 | // 根据控件的选择,重新设置定位参数 165 | private void initOption() { 166 | // 设置是否需要显示地址信息 167 | locationOption.setNeedAddress(cbAddress.isChecked()); 168 | /** 169 | * 设置是否优先返回GPS定位结果,如果30秒内GPS没有返回定位结果则进行网络定位 170 | * 注意:只有在高精度模式下的单次定位有效,其他方式无效 171 | */ 172 | locationOption.setGpsFirst(cbGpsFirst.isChecked()); 173 | String strInterval = etInterval.getText().toString(); 174 | if (!TextUtils.isEmpty(strInterval)) { 175 | // 设置发送定位请求的时间间隔,最小值为1000,如果小于1000,按照1000算 176 | locationOption.setInterval(Long.valueOf(strInterval)); 177 | } 178 | 179 | } 180 | 181 | Handler mHandler = new Handler() { 182 | public void dispatchMessage(android.os.Message msg) { 183 | switch (msg.what) { 184 | //开始定位 185 | case Utils.MSG_LOCATION_START: 186 | tvReult.setText("正在定位..."); 187 | break; 188 | // 定位完成 189 | case Utils.MSG_LOCATION_FINISH: 190 | AMapLocation loc = (AMapLocation) msg.obj; 191 | String result = Utils.getLocationStr(loc); 192 | tvReult.setText(result); 193 | break; 194 | //停止定位 195 | case Utils.MSG_LOCATION_STOP: 196 | tvReult.setText("定位停止"); 197 | break; 198 | default: 199 | break; 200 | } 201 | }; 202 | }; 203 | 204 | // 定位监听 205 | @Override 206 | public void onLocationChanged(AMapLocation loc) { 207 | if (null != loc) { 208 | Message msg = mHandler.obtainMessage(); 209 | msg.obj = loc; 210 | msg.what = Utils.MSG_LOCATION_FINISH; 211 | mHandler.sendMessage(msg); 212 | } 213 | } 214 | 215 | 216 | private BroadcastReceiver alarmReceiver = new BroadcastReceiver(){ 217 | @Override 218 | public void onReceive(Context context, Intent intent) { 219 | if(intent.getAction().equals("LOCATION")){ 220 | if(null != locationClient){ 221 | locationClient.startLocation(); 222 | } 223 | } 224 | } 225 | }; 226 | 227 | } 228 | -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/app/src/main/java/com/amap/location/demo/Assistant_Location_Activity.java: -------------------------------------------------------------------------------- 1 | package com.amap.location.demo; 2 | 3 | import com.amap.api.location.AMapLocationClient; 4 | 5 | import android.app.Activity; 6 | import android.graphics.Bitmap; 7 | import android.os.Bundle; 8 | import android.view.View; 9 | import android.view.View.OnClickListener; 10 | import android.view.Window; 11 | import android.webkit.GeolocationPermissions.Callback; 12 | import android.webkit.JsResult; 13 | import android.webkit.WebChromeClient; 14 | import android.webkit.WebSettings; 15 | import android.webkit.WebView; 16 | import android.webkit.WebViewClient; 17 | import android.widget.Button; 18 | 19 | /** 20 | * H5辅助定位模式功能演示 21 | * 22 | * @创建时间: 2015年11月24日 下午4:24:07 23 | * @项目名称: AMapLocationDemo2.x 24 | * 25 | * @author hongming.wang 26 | * @文件名称: Battery_Saving_Activity.java 27 | * @类型名称: Battery_Saving_Activity 28 | */ 29 | public class Assistant_Location_Activity extends CheckPermissionsActivity implements OnClickListener { 30 | private Button btAssistant; 31 | private Button btLocation; 32 | 33 | private AMapLocationClient locationClient = null; 34 | 35 | private WebView webView; 36 | 37 | @Override 38 | protected void onCreate(Bundle savedInstanceState) { 39 | super.onCreate(savedInstanceState); 40 | setContentView(R.layout.activity_assistant_location); 41 | setTitle(R.string.title_assistantLocation); 42 | 43 | btAssistant = (Button) findViewById(R.id.bt_assistant); 44 | btLocation = (Button) findViewById(R.id.bt_location); 45 | 46 | locationClient = new AMapLocationClient(getApplicationContext()); 47 | btAssistant.setOnClickListener(this); 48 | btLocation.setOnClickListener(this); 49 | 50 | initWebView(); 51 | } 52 | 53 | private void initWebView(){ 54 | webView = (WebView) findViewById(R.id.webView); 55 | 56 | WebSettings webSettings = webView.getSettings(); 57 | // 允许webview执行javaScript脚本 58 | webSettings.setJavaScriptEnabled(true); 59 | // 设置是否允许定位,这里为了使用H5辅助定位,设置为false。 60 | //设置为true不一定会进行H5辅助定位,设置为true时只有H5定位失败后才会进行辅助定位 61 | webSettings.setGeolocationEnabled(false); 62 | 63 | 64 | webView.setWebViewClient(new WebViewClient() { 65 | 66 | public void onPageFinished(WebView view, String url) { 67 | super.onPageFinished(view, url); 68 | } 69 | 70 | public void onPageStarted(WebView view, String url, Bitmap favicon) { 71 | super.onPageStarted(view, url, favicon); 72 | } 73 | }); 74 | 75 | webView.setWebChromeClient(new WebChromeClient() { 76 | // 处理javascript中的alert 77 | public boolean onJsAlert(WebView view, String url, String message, 78 | final JsResult result) { 79 | return true; 80 | }; 81 | 82 | // 处理javascript中的confirm 83 | public boolean onJsConfirm(WebView view, String url, 84 | String message, final JsResult result) { 85 | return true; 86 | }; 87 | 88 | // 处理定位权限请求 89 | @Override 90 | public void onGeolocationPermissionsShowPrompt(String origin, 91 | Callback callback) { 92 | callback.invoke(origin, true, false); 93 | super.onGeolocationPermissionsShowPrompt(origin, callback); 94 | } 95 | @Override 96 | // 设置网页加载的进度条 97 | public void onProgressChanged(WebView view, int newProgress) { 98 | Assistant_Location_Activity.this.getWindow().setFeatureInt( 99 | Window.FEATURE_PROGRESS, newProgress * 100); 100 | super.onProgressChanged(view, newProgress); 101 | } 102 | 103 | // 设置应用程序的标题title 104 | public void onReceivedTitle(WebView view, String title) { 105 | super.onReceivedTitle(view, title); 106 | } 107 | }); 108 | } 109 | 110 | @Override 111 | protected void onDestroy() { 112 | super.onDestroy(); 113 | if (null != locationClient) { 114 | locationClient.stopAssistantLocation(); 115 | locationClient.onDestroy(); 116 | } 117 | if(null != webView){ 118 | webView.destroy(); 119 | } 120 | } 121 | 122 | @Override 123 | public void onClick(View v) { 124 | if (v.getId() == R.id.bt_assistant) { 125 | if (btAssistant.getText().equals(getResources().getString(R.string.startAssistantLocation))) { 126 | btAssistant.setText(getResources().getString(R.string.stopAssistantLocation)); 127 | locationClient.startAssistantLocation(); 128 | btLocation.setEnabled(true); 129 | } else { 130 | btAssistant.setText(getResources().getString(R.string.startAssistantLocation)); 131 | locationClient.stopAssistantLocation(); 132 | btLocation.setEnabled(false); 133 | webView.setVisibility(View.GONE); 134 | } 135 | } else if (v.getId() == R.id.bt_location) { 136 | webView.setVisibility(View.VISIBLE); 137 | //浏览器定位,加载预置的H5定位页面 138 | webView.loadUrl(Utils.URL_H5LOCATION); 139 | } 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/app/src/main/java/com/amap/location/demo/CheckPermissionsActivity.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package com.amap.location.demo; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | import android.Manifest; 10 | import android.app.Activity; 11 | import android.app.AlertDialog; 12 | import android.content.DialogInterface; 13 | import android.content.Intent; 14 | import android.content.pm.PackageManager; 15 | import android.net.Uri; 16 | import android.provider.Settings; 17 | import android.support.v4.app.ActivityCompat; 18 | import android.support.v4.content.ContextCompat; 19 | import android.view.KeyEvent; 20 | 21 | /** 22 | * 继承了Activity,实现Android6.0的运行时权限检测 23 | * 需要进行运行时权限检测的Activity可以继承这个类 24 | * 25 | * @创建时间:2016年5月27日 下午3:01:31 26 | * @项目名称: AMapLocationDemo 27 | * @author hongming.wang 28 | * @文件名称:PermissionsChecker.java 29 | * @类型名称:PermissionsChecker 30 | * @since 2.5.0 31 | */ 32 | public class CheckPermissionsActivity extends Activity 33 | implements 34 | ActivityCompat.OnRequestPermissionsResultCallback { 35 | /** 36 | * 需要进行检测的权限数组 37 | */ 38 | protected String[] needPermissions = { 39 | Manifest.permission.ACCESS_COARSE_LOCATION, 40 | Manifest.permission.ACCESS_FINE_LOCATION, 41 | Manifest.permission.WRITE_EXTERNAL_STORAGE, 42 | Manifest.permission.READ_EXTERNAL_STORAGE, 43 | Manifest.permission.READ_PHONE_STATE 44 | }; 45 | 46 | private static final int PERMISSON_REQUESTCODE = 0; 47 | 48 | /** 49 | * 判断是否需要检测,防止不停的弹框 50 | */ 51 | private boolean isNeedCheck = true; 52 | 53 | @Override 54 | protected void onResume() { 55 | super.onResume(); 56 | if(isNeedCheck){ 57 | checkPermissions(needPermissions); 58 | } 59 | } 60 | 61 | /** 62 | * 63 | * @param needRequestPermissonList 64 | * @since 2.5.0 65 | * 66 | */ 67 | private void checkPermissions(String... permissions) { 68 | List needRequestPermissonList = findDeniedPermissions(permissions); 69 | if (null != needRequestPermissonList 70 | && needRequestPermissonList.size() > 0) { 71 | ActivityCompat.requestPermissions(this, 72 | needRequestPermissonList.toArray( 73 | new String[needRequestPermissonList.size()]), 74 | PERMISSON_REQUESTCODE); 75 | } 76 | } 77 | 78 | /** 79 | * 获取权限集中需要申请权限的列表 80 | * 81 | * @param permissions 82 | * @return 83 | * @since 2.5.0 84 | * 85 | */ 86 | private List findDeniedPermissions(String[] permissions) { 87 | List needRequestPermissonList = new ArrayList(); 88 | for (String perm : permissions) { 89 | if (ContextCompat.checkSelfPermission(this, 90 | perm) != PackageManager.PERMISSION_GRANTED 91 | || ActivityCompat.shouldShowRequestPermissionRationale( 92 | this, perm)) { 93 | needRequestPermissonList.add(perm); 94 | } 95 | } 96 | return needRequestPermissonList; 97 | } 98 | 99 | /** 100 | * 检测是否说有的权限都已经授权 101 | * @param grantResults 102 | * @return 103 | * @since 2.5.0 104 | * 105 | */ 106 | private boolean verifyPermissions(int[] grantResults) { 107 | for (int result : grantResults) { 108 | if (result != PackageManager.PERMISSION_GRANTED) { 109 | return false; 110 | } 111 | } 112 | return true; 113 | } 114 | 115 | @Override 116 | public void onRequestPermissionsResult(int requestCode, 117 | String[] permissions, int[] paramArrayOfInt) { 118 | if (requestCode == PERMISSON_REQUESTCODE) { 119 | if (!verifyPermissions(paramArrayOfInt)) { 120 | showMissingPermissionDialog(); 121 | isNeedCheck = false; 122 | } 123 | } 124 | } 125 | 126 | /** 127 | * 显示提示信息 128 | * 129 | * @since 2.5.0 130 | * 131 | */ 132 | private void showMissingPermissionDialog() { 133 | AlertDialog.Builder builder = new AlertDialog.Builder(this); 134 | builder.setTitle(R.string.notifyTitle); 135 | builder.setMessage(R.string.notifyMsg); 136 | 137 | // 拒绝, 退出应用 138 | builder.setNegativeButton(R.string.cancel, 139 | new DialogInterface.OnClickListener() { 140 | @Override 141 | public void onClick(DialogInterface dialog, int which) { 142 | finish(); 143 | } 144 | }); 145 | 146 | builder.setPositiveButton(R.string.setting, 147 | new DialogInterface.OnClickListener() { 148 | @Override 149 | public void onClick(DialogInterface dialog, int which) { 150 | startAppSettings(); 151 | } 152 | }); 153 | 154 | builder.setCancelable(false); 155 | 156 | builder.show(); 157 | } 158 | 159 | /** 160 | * 启动应用的设置 161 | * 162 | * @since 2.5.0 163 | * 164 | */ 165 | private void startAppSettings() { 166 | Intent intent = new Intent( 167 | Settings.ACTION_APPLICATION_DETAILS_SETTINGS); 168 | intent.setData(Uri.parse("package:" + getPackageName())); 169 | startActivity(intent); 170 | } 171 | 172 | @Override 173 | public boolean onKeyDown(int keyCode, KeyEvent event) { 174 | if(keyCode == KeyEvent.KEYCODE_BACK){ 175 | this.finish(); 176 | return true; 177 | } 178 | return super.onKeyDown(keyCode, event); 179 | } 180 | 181 | } 182 | -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/app/src/main/java/com/amap/location/demo/Const.java: -------------------------------------------------------------------------------- 1 | package com.amap.location.demo; 2 | 3 | import android.graphics.Color; 4 | 5 | /** 6 | * @since 3.3.0 7 | * Created by hongming.wang on 2016/12/19. 8 | */ 9 | 10 | public class Const { 11 | /** 12 | * 地图中绘制多边形、圆形的边界颜色 13 | * @since 3.3.0 14 | */ 15 | public static final int STROKE_COLOR = Color.argb(180, 63, 145, 252); 16 | /** 17 | * 地图中绘制多边形、圆形的填充颜色 18 | * @since 3.3.0 19 | */ 20 | public static final int FILL_COLOR = Color.argb(163, 118, 212, 243); 21 | 22 | /** 23 | * 地图中绘制多边形、圆形的边框宽度 24 | * @since 3.3.0 25 | */ 26 | public static final float STROKE_WIDTH = 5F; 27 | } 28 | -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/app/src/main/java/com/amap/location/demo/ErrorCode_Activity.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package com.amap.location.demo; 5 | 6 | 7 | import android.app.Activity; 8 | import android.os.Bundle; 9 | import android.view.KeyEvent; 10 | import android.widget.TextView; 11 | 12 | /** 13 | * 错误码说明 14 | * @创建时间:2016年5月31日 上午11:22:06 15 | * @项目名称: AMapLocationDemo 16 | * @author hongming.wang 17 | * @文件名称:ErrorCodeInfo.java 18 | * @类型名称:ErrorCodeInfo 19 | * @since 2.5.0 20 | */ 21 | public class ErrorCode_Activity extends Activity{ 22 | 23 | @Override 24 | protected void onCreate(Bundle savedInstanceState) { 25 | super.onCreate(savedInstanceState); 26 | setContentView(R.layout.activity_errcode); 27 | setTitle(R.string.title_errorCode); 28 | } 29 | 30 | @Override 31 | public boolean onKeyDown(int keyCode, KeyEvent event) { 32 | if(keyCode == KeyEvent.KEYCODE_BACK){ 33 | this.finish(); 34 | return true; 35 | } 36 | return super.onKeyDown(keyCode, event); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/app/src/main/java/com/amap/location/demo/GeoFence_Activity.java: -------------------------------------------------------------------------------- 1 | package com.amap.location.demo; 2 | 3 | import com.amap.location.demo.view.FeatureView; 4 | 5 | import android.app.ListActivity; 6 | import android.content.Context; 7 | import android.content.Intent; 8 | import android.os.Bundle; 9 | import android.view.KeyEvent; 10 | import android.view.View; 11 | import android.view.ViewGroup; 12 | import android.widget.ArrayAdapter; 13 | import android.widget.ListAdapter; 14 | import android.widget.ListView; 15 | 16 | /** 17 | * 地理围栏功能演示 18 | * 19 | * @创建时间: 2015年11月24日 下午5:49:52 20 | * @项目名称: AMapLocationDemo2.x 21 | * @author hongming.wang 22 | * @文件名称: GeoFence_Activity.java 23 | * @类型名称: GeoFence_Activity 24 | */ 25 | public class GeoFence_Activity extends ListActivity { 26 | private static class DemoDetails { 27 | private final int titleId; 28 | private final int descriptionId; 29 | private final Class activityClass; 30 | public DemoDetails(int titleId, int descriptionId, 31 | Class activityClass) { 32 | super(); 33 | this.titleId = titleId; 34 | this.descriptionId = descriptionId; 35 | this.activityClass = activityClass; 36 | } 37 | } 38 | 39 | private static class CustomArrayAdapter extends ArrayAdapter { 40 | public CustomArrayAdapter(Context context, DemoDetails[] demos) { 41 | super(context, R.layout.feature, R.id.title, demos); 42 | } 43 | 44 | @Override 45 | public View getView(int position, View convertView, ViewGroup parent) { 46 | FeatureView featureView; 47 | if (convertView instanceof FeatureView) { 48 | featureView = (FeatureView) convertView; 49 | } else { 50 | featureView = new FeatureView(getContext()); 51 | } 52 | DemoDetails demo = getItem(position); 53 | featureView.setTitleId(demo.titleId); 54 | featureView.setDescriptionId(demo.descriptionId); 55 | return featureView; 56 | } 57 | } 58 | 59 | private static final DemoDetails[] demos = { 60 | new DemoDetails(R.string.roundGeoFence, 61 | R.string.roundGeoFence_dec, GeoFence_Round_Activity.class), 62 | new DemoDetails(R.string.polygonGeoFence, 63 | R.string.polygonGeoFence_dec, GeoFence_Polygon_Activity.class), 64 | new DemoDetails(R.string.keywordGeoFence, 65 | R.string.keywordGeoFence_dec, GeoFence_Keyword_Activity.class), 66 | new DemoDetails(R.string.nearbyGeoFence, 67 | R.string.nearbyGeoFence_dec, GeoFence_Nearby_Activity.class), 68 | new DemoDetails(R.string.districtGeoFence, 69 | R.string.districtGeoFence_dec, GeoFence_District_Activity.class), 70 | new DemoDetails(R.string.multipleGeoFence, R.string.multipleGeoFence_dec, 71 | GeoFence_Multiple_Activity.class), 72 | new DemoDetails(R.string.oldGeoFence, R.string.oldGeoFence_dec, 73 | GeoFence_Old_Activity.class), 74 | }; 75 | 76 | @Override 77 | protected void onCreate(Bundle savedInstanceState) { 78 | super.onCreate(savedInstanceState); 79 | setContentView(R.layout.activity_geofence); 80 | setTitle(R.string.title_main); 81 | ListAdapter adapter = new CustomArrayAdapter( 82 | this.getApplicationContext(), demos); 83 | setListAdapter(adapter); 84 | // permChecker = new PermissionsChecker(this); 85 | } 86 | 87 | @Override 88 | public void onBackPressed() { 89 | super.onBackPressed(); 90 | System.exit(0); 91 | } 92 | 93 | @Override 94 | protected void onListItemClick(ListView l, View v, int position, long id) { 95 | DemoDetails demo = (DemoDetails) getListAdapter().getItem(position); 96 | startActivity( 97 | new Intent(this.getApplicationContext(), demo.activityClass)); 98 | } 99 | @Override 100 | public boolean onKeyDown(int keyCode, KeyEvent event) { 101 | if(keyCode == KeyEvent.KEYCODE_BACK){ 102 | this.finish(); 103 | return true; 104 | } 105 | return super.onKeyDown(keyCode, event); 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/app/src/main/java/com/amap/location/demo/GeoFence_Old_Activity.java: -------------------------------------------------------------------------------- 1 | package com.amap.location.demo; 2 | 3 | import com.amap.api.fence.GeoFence; 4 | import com.amap.api.location.AMapLocation; 5 | import com.amap.api.location.AMapLocationClient; 6 | import com.amap.api.location.AMapLocationClientOption; 7 | import com.amap.api.location.AMapLocationClientOption.AMapLocationMode; 8 | import com.amap.api.location.AMapLocationListener; 9 | 10 | import android.app.PendingIntent; 11 | import android.content.BroadcastReceiver; 12 | import android.content.Context; 13 | import android.content.Intent; 14 | import android.content.IntentFilter; 15 | import android.net.ConnectivityManager; 16 | import android.os.Bundle; 17 | import android.os.Handler; 18 | import android.os.Message; 19 | import android.text.TextUtils; 20 | import android.view.View; 21 | import android.view.View.OnClickListener; 22 | import android.widget.Button; 23 | import android.widget.CheckBox; 24 | import android.widget.EditText; 25 | import android.widget.TextView; 26 | import android.widget.Toast; 27 | 28 | /** 29 | * 地理围栏功能演示 30 | * 31 | * @创建时间: 2015年11月24日 下午5:49:52 32 | * @项目名称: AMapLocationDemo2.x 33 | * @author hongming.wang 34 | * @文件名称: GeoFence_Activity.java 35 | * @类型名称: GeoFence_Activity 36 | */ 37 | public class GeoFence_Old_Activity extends CheckPermissionsActivity implements OnClickListener, 38 | AMapLocationListener { 39 | private EditText etRadius; 40 | private TextView tvReult; 41 | private CheckBox cbAlertIn; 42 | private CheckBox cbAlertOut; 43 | private Button btFence; 44 | 45 | // 声明一个单次定位的客户端,获取当前位置的坐标,用于设置围栏的中心点坐标 46 | private AMapLocationClient onceClient = null; 47 | // 声明一个持续定位的客户端,用于添加地理围栏 48 | private AMapLocationClient locationClient = null; 49 | private AMapLocationClientOption locationOption = null; 50 | 51 | // 用于接收地理围栏提醒的pendingIntent 52 | private PendingIntent mPendingIntent = null; 53 | public static final String GEOFENCE_BROADCAST_ACTION = "com.location.apis.geofencedemo.broadcast"; 54 | 55 | @Override 56 | protected void onCreate(Bundle savedInstanceState) { 57 | super.onCreate(savedInstanceState); 58 | setContentView(R.layout.activity_geofence_old); 59 | setTitle(R.string.oldGeoFence); 60 | 61 | etRadius = (EditText) findViewById(R.id.et_radius); 62 | cbAlertIn = (CheckBox) findViewById(R.id.cb_alertIn); 63 | cbAlertOut = (CheckBox) findViewById(R.id.cb_alertOut); 64 | tvReult = (TextView) findViewById(R.id.tv_result); 65 | btFence = (Button) findViewById(R.id.bt_fence); 66 | 67 | btFence.setOnClickListener(this); 68 | 69 | IntentFilter fliter = new IntentFilter( 70 | ConnectivityManager.CONNECTIVITY_ACTION); 71 | fliter.addAction(GEOFENCE_BROADCAST_ACTION); 72 | registerReceiver(mGeoFenceReceiver, fliter); 73 | Intent intent = new Intent(GEOFENCE_BROADCAST_ACTION); 74 | mPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, 75 | intent, 0); 76 | onceClient = new AMapLocationClient(getApplicationContext()); 77 | locationClient = new AMapLocationClient(this.getApplicationContext()); 78 | locationOption = new AMapLocationClientOption(); 79 | 80 | // 设置定位模式高精度,添加地理围栏最好设置成高精度模式 81 | locationOption.setLocationMode(AMapLocationMode.Hight_Accuracy); 82 | // 设置定位监听 83 | locationClient.setLocationListener(this); 84 | 85 | AMapLocationClientOption onceOption = new AMapLocationClientOption(); 86 | onceOption.setOnceLocation(true); 87 | onceClient.setLocationOption(onceOption); 88 | onceClient.setLocationListener(new AMapLocationListener() { 89 | @Override 90 | public void onLocationChanged(AMapLocation loc) { 91 | if (loc != null) { 92 | if (loc.getErrorCode() == 0) { 93 | if (null != locationClient) { 94 | float radius = 1000; 95 | String strRadius = etRadius.getText().toString(); 96 | if (!TextUtils.isEmpty(strRadius)) { 97 | radius = Float.valueOf(strRadius); 98 | } 99 | 100 | // 添加地理围栏, 101 | // 第一个参数:围栏ID,可以自定义ID,示例中为了方便只使用一个ID;第二个:纬度;第三个:精度; 102 | // 第四个:半径;第五个:过期时间,单位毫秒,-1代表不过期;第六个:接收触发消息的PendingIntent 103 | locationClient.addGeoFenceAlert("fenceId", 104 | loc.getLatitude(), loc.getLongitude(), 105 | radius, -1, mPendingIntent); 106 | } 107 | } else { 108 | Toast.makeText(getApplicationContext(), "获取当前位置失败!", 109 | Toast.LENGTH_SHORT).show(); 110 | 111 | Message msg = mHandler.obtainMessage(); 112 | msg.obj = loc; 113 | msg.what = -1; 114 | mHandler.sendMessage(msg); 115 | } 116 | } 117 | } 118 | }); 119 | } 120 | 121 | Handler mHandler = new Handler() { 122 | public void dispatchMessage(android.os.Message msg) { 123 | switch (msg.what) { 124 | case 1: 125 | tvReult.setText("进入围栏区域"); 126 | break; 127 | case 2: 128 | tvReult.setText("离开围栏区域"); 129 | break; 130 | case -1: 131 | // 获取当前位置失败 132 | AMapLocation loc = (AMapLocation) msg.obj; 133 | tvReult.setText(Utils.getLocationStr(loc)); 134 | btFence.setText(getResources().getString(R.string.addFence)); 135 | break; 136 | default: 137 | break; 138 | } 139 | }; 140 | }; 141 | 142 | private BroadcastReceiver mGeoFenceReceiver = new BroadcastReceiver() { 143 | @Override 144 | public void onReceive(Context context, Intent intent) { 145 | // 接收广播 146 | if (intent.getAction().equals(GEOFENCE_BROADCAST_ACTION)) { 147 | Bundle bundle = intent.getExtras(); 148 | // 根据广播的event来确定是在区域内还是在区域外 149 | int status = bundle.getInt(GeoFence.BUNDLE_KEY_FENCESTATUS); 150 | String geoFenceId = bundle.getString(GeoFence.BUNDLE_KEY_FENCEID); 151 | if (status == 1) { 152 | // 进入围栏区域 153 | // 可以自定义提醒方式,示例中使用的是文字方式 154 | if (cbAlertIn.isChecked()) { 155 | mHandler.sendEmptyMessage(1); 156 | } 157 | } else if (status == 2) { 158 | // 离开围栏区域 159 | // 可以自定义提醒方式,示例中使用的是文字方式 160 | if (cbAlertOut.isChecked()) { 161 | mHandler.sendEmptyMessage(2); 162 | } 163 | } 164 | } 165 | } 166 | }; 167 | 168 | @Override 169 | protected void onDestroy() { 170 | super.onDestroy(); 171 | if (null != locationClient) { 172 | /** 173 | * 如果AMapLocationClient是在当前Activity实例化的, 174 | * 在Activity的onDestroy中一定要执行AMapLocationClient的onDestroy 175 | */ 176 | locationClient.onDestroy(); 177 | locationClient = null; 178 | locationOption = null; 179 | } 180 | 181 | if (null != onceClient) { 182 | onceClient.onDestroy(); 183 | } 184 | } 185 | 186 | @Override 187 | public void onClick(View v) { 188 | if (v.getId() == R.id.bt_fence) { 189 | if (btFence.getText().equals( 190 | getResources().getString(R.string.addFence))) { 191 | btFence.setText(getResources().getString(R.string.removeFence)); 192 | // 启动单次定位,获取当前位置 193 | onceClient.startLocation(); 194 | 195 | // 设置定位参数 196 | locationClient.setLocationOption(locationOption); 197 | // 启动定位,地理围栏依赖于持续定位 198 | locationClient.startLocation(); 199 | 200 | } else { 201 | tvReult.setText(""); 202 | btFence.setText(getResources().getString(R.string.addFence)); 203 | // 移除围栏 204 | locationClient.removeGeoFenceAlert(mPendingIntent); 205 | } 206 | } 207 | } 208 | 209 | // 定位监听 210 | @Override 211 | public void onLocationChanged(AMapLocation loc) { 212 | } 213 | } 214 | -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/app/src/main/java/com/amap/location/demo/LastLocation_Activity.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package com.amap.location.demo; 5 | 6 | import com.amap.api.location.AMapLocation; 7 | import com.amap.api.location.AMapLocationClient; 8 | 9 | import android.app.Activity; 10 | import android.os.Bundle; 11 | import android.view.View; 12 | import android.view.View.OnClickListener; 13 | import android.widget.Button; 14 | import android.widget.TextView; 15 | 16 | /** 17 | * 最后位置功能演示 18 | * @创建时间:2016年1月7日 下午4:36:22 19 | * @项目名称: AMapLocationDemo2.x 20 | * @author hongming.wang 21 | * @文件名称:LastLocation_Activity.java 22 | * @类型名称:LastLocation_Activity 23 | * @since 2.3.0 24 | */ 25 | public class LastLocation_Activity extends CheckPermissionsActivity implements OnClickListener{ 26 | private Button btnLastLoc; 27 | private TextView tvReult; 28 | private AMapLocationClient locationClient = null; 29 | @Override 30 | protected void onCreate(Bundle savedInstanceState) { 31 | super.onCreate(savedInstanceState); 32 | setContentView(R.layout.activity_lastlocation); 33 | setTitle(R.string.title_lastLocation); 34 | tvReult = (TextView) findViewById(R.id.tv_result); 35 | btnLastLoc = (Button) findViewById(R.id.bt_lastLoc); 36 | btnLastLoc.setOnClickListener(this); 37 | locationClient = new AMapLocationClient(this.getApplicationContext()); 38 | } 39 | 40 | @Override 41 | protected void onDestroy() { 42 | super.onDestroy(); 43 | if (null != locationClient) { 44 | locationClient.onDestroy(); 45 | locationClient = null; 46 | } 47 | } 48 | 49 | @Override 50 | public void onClick(View v) { 51 | if(v.getId() == R.id.bt_lastLoc){ 52 | AMapLocation location = locationClient.getLastKnownLocation(); 53 | String result = Utils.getLocationStr(location); 54 | tvReult.setText(result); 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/app/src/main/java/com/amap/location/demo/StartActivity.java: -------------------------------------------------------------------------------- 1 | package com.amap.location.demo; 2 | 3 | import com.amap.location.demo.view.FeatureView; 4 | 5 | import android.app.ListActivity; 6 | import android.content.Context; 7 | import android.content.Intent; 8 | import android.os.Bundle; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | import android.widget.ArrayAdapter; 12 | import android.widget.ListAdapter; 13 | import android.widget.ListView; 14 | 15 | /** 16 | * 高德定位SDK功能接口实例 更多SDK请进入官网“http://lbs.amap.com/api/”查看 17 | * 官方论坛:http://lbsbbs.amap.com/portal.php 18 | * 19 | * @项目名称: AMapLocationDemo2.x 20 | * 21 | * @author hongming.wang 22 | * @文件名称: StartActivity.java 23 | * @类型名称: StartActivity 24 | */ 25 | public class StartActivity extends ListActivity { 26 | private static class DemoDetails { 27 | private final int titleId; 28 | private final int descriptionId; 29 | private final Class activityClass; 30 | public DemoDetails(int titleId, int descriptionId, 31 | Class activityClass) { 32 | super(); 33 | this.titleId = titleId; 34 | this.descriptionId = descriptionId; 35 | this.activityClass = activityClass; 36 | } 37 | } 38 | 39 | private static class CustomArrayAdapter extends ArrayAdapter { 40 | public CustomArrayAdapter(Context context, DemoDetails[] demos) { 41 | super(context, R.layout.feature, R.id.title, demos); 42 | } 43 | 44 | @Override 45 | public View getView(int position, View convertView, ViewGroup parent) { 46 | FeatureView featureView; 47 | if (convertView instanceof FeatureView) { 48 | featureView = (FeatureView) convertView; 49 | } else { 50 | featureView = new FeatureView(getContext()); 51 | } 52 | DemoDetails demo = getItem(position); 53 | featureView.setTitleId(demo.titleId); 54 | featureView.setDescriptionId(demo.descriptionId); 55 | return featureView; 56 | } 57 | } 58 | 59 | private static final DemoDetails[] demos = { 60 | new DemoDetails(R.string.location, 61 | R.string.location_dec, Location_Activity.class), 62 | new DemoDetails(R.string.geoFence, R.string.geoFence_dec, 63 | GeoFence_Activity.class), 64 | new DemoDetails(R.string.assistantLocation, 65 | R.string.assistantLocation_dec, 66 | Assistant_Location_Activity.class), 67 | new DemoDetails(R.string.tools, R.string.tools_dec, 68 | Tools_Activity.class), 69 | new DemoDetails(R.string.lastLocation, R.string.lastLocation_dec, 70 | LastLocation_Activity.class), 71 | new DemoDetails(R.string.alarmCPU, R.string.alarmCPU_dec, 72 | Alarm_Location_Activity.class), 73 | new DemoDetails(R.string.errorCode, R.string.errorCode_dec, 74 | ErrorCode_Activity.class), 75 | }; 76 | 77 | @Override 78 | protected void onCreate(Bundle savedInstanceState) { 79 | super.onCreate(savedInstanceState); 80 | setContentView(R.layout.activity_start); 81 | /** 82 | * setApiKey必须在启动Activity或者Application的onCreate里进行, 在其他地方使用有可能无效, 83 | * 如果使用setApiKey设置key,则AndroidManifest.xml里的key会失效 84 | */ 85 | // AMapLocationClient.setApiKey("需要更换的key"); 86 | 87 | setTitle(R.string.title_main); 88 | ListAdapter adapter = new CustomArrayAdapter( 89 | this.getApplicationContext(), demos); 90 | setListAdapter(adapter); 91 | // permChecker = new PermissionsChecker(this); 92 | } 93 | 94 | @Override 95 | public void onBackPressed() { 96 | super.onBackPressed(); 97 | System.exit(0); 98 | } 99 | 100 | @Override 101 | protected void onListItemClick(ListView l, View v, int position, long id) { 102 | DemoDetails demo = (DemoDetails) getListAdapter().getItem(position); 103 | startActivity( 104 | new Intent(this.getApplicationContext(), demo.activityClass)); 105 | } 106 | 107 | } 108 | -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/app/src/main/java/com/amap/location/demo/Tools_Activity.java: -------------------------------------------------------------------------------- 1 | package com.amap.location.demo; 2 | 3 | import com.amap.api.location.CoordinateConverter; 4 | import com.amap.api.location.CoordinateConverter.CoordType; 5 | import com.amap.api.location.DPoint; 6 | 7 | import android.app.Activity; 8 | import android.os.Bundle; 9 | import android.view.View; 10 | import android.view.View.OnClickListener; 11 | import android.widget.Button; 12 | import android.widget.TextView; 13 | import android.widget.Toast; 14 | 15 | /** 16 | * 高精度定位模式功能演示 17 | * 18 | * @创建时间: 2015年11月24日 下午5:22:42 19 | * @项目名称: AMapLocationDemo2.x 20 | * @author hongming.wang 21 | * @文件名称: Hight_Accuracy_Activity.java 22 | * @类型名称: Hight_Accuracy_Activity 23 | */ 24 | public class Tools_Activity extends Activity 25 | implements OnClickListener { 26 | private TextView tvConvertReult; 27 | private TextView tvCheckReult; 28 | 29 | private Button btConvert; 30 | private Button btCheck; 31 | //构造一个示例坐标,第一个参数是纬度,第二个参数是经度 32 | DPoint examplePoint = new DPoint(39.911127, 116.433608); 33 | 34 | @Override 35 | protected void onCreate(Bundle savedInstanceState) { 36 | super.onCreate(savedInstanceState); 37 | setContentView(R.layout.activity_tools); 38 | setTitle(R.string.title_tools); 39 | 40 | initView(); 41 | } 42 | 43 | // 初始化控件 44 | private void initView() { 45 | 46 | tvConvertReult = (TextView) findViewById(R.id.tv_convertResult); 47 | tvCheckReult = (TextView) findViewById(R.id.tv_checkResult); 48 | 49 | btConvert = (Button) findViewById(R.id.bt_covert); 50 | btCheck = (Button) findViewById(R.id.bt_check); 51 | 52 | btConvert.setOnClickListener(this); 53 | btCheck.setOnClickListener(this); 54 | } 55 | 56 | @Override 57 | protected void onDestroy() { 58 | super.onDestroy(); 59 | } 60 | 61 | @Override 62 | public void onClick(View v) { 63 | switch (v.getId()) { 64 | case R.id.bt_covert : 65 | convert(); 66 | break; 67 | case R.id.bt_check : 68 | checkIsAvaliable(); 69 | break; 70 | } 71 | } 72 | 73 | //坐标转换 74 | private void convert() { 75 | try{ 76 | //初始化坐标转换类 77 | CoordinateConverter converter = new CoordinateConverter( 78 | getApplicationContext()); 79 | /** 80 | * 设置坐标来源,这里使用百度坐标作为示例 81 | * 可选的来源包括: 82 | *
  • CoordType.BAIDU : 百度坐标 83 | *
  • CoordType.MAPBAR : 图吧坐标 84 | *
  • CoordType.MAPABC : 图盟坐标 85 | *
  • CoordType.SOSOMAP : 搜搜坐标 86 | *
  • CoordType.ALIYUN : 阿里云坐标 87 | *
  • CoordType.GOOGLE : 谷歌坐标 88 | *
  • CoordType.GPS : GPS坐标 89 | */ 90 | converter.from(CoordType.BAIDU); 91 | //设置需要转换的坐标 92 | converter.coord(examplePoint); 93 | //转换成高德坐标 94 | DPoint destPoint = converter.convert(); 95 | if(null != destPoint){ 96 | tvConvertReult.setText("转换后坐标(经度、纬度):" + destPoint.getLongitude() + "," + destPoint.getLatitude()); 97 | } else { 98 | Toast.makeText(getApplicationContext(), "坐标转换失败", Toast.LENGTH_SHORT).show(); 99 | } 100 | }catch(Exception e){ 101 | Toast.makeText(getApplicationContext(), "坐标转换失败", Toast.LENGTH_SHORT).show(); 102 | e.printStackTrace(); 103 | } 104 | } 105 | 106 | //判断坐标是否高德地图可用 107 | private void checkIsAvaliable(){ 108 | //初始化坐标工具类 109 | CoordinateConverter converter = new CoordinateConverter( 110 | getApplicationContext()); 111 | //判断是否高德地图可用的坐标 112 | boolean result = converter.isAMapDataAvailable(examplePoint.getLatitude(), examplePoint.getLongitude()); 113 | if(result){ 114 | tvCheckReult.setText("该坐标是高德地图可用坐标"); 115 | } else { 116 | tvCheckReult.setText("该坐标不能用于高德地图"); 117 | } 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/app/src/main/java/com/amap/location/demo/Utils.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package com.amap.location.demo; 5 | 6 | import java.text.SimpleDateFormat; 7 | import java.util.Locale; 8 | 9 | import com.amap.api.location.AMapLocation; 10 | 11 | import android.text.TextUtils; 12 | 13 | /** 14 | * 辅助工具类 15 | * @创建时间: 2015年11月24日 上午11:46:50 16 | * @项目名称: AMapLocationDemo2.x 17 | * @author hongming.wang 18 | * @文件名称: Utils.java 19 | * @类型名称: Utils 20 | */ 21 | public class Utils { 22 | /** 23 | * 开始定位 24 | */ 25 | public final static int MSG_LOCATION_START = 0; 26 | /** 27 | * 定位完成 28 | */ 29 | public final static int MSG_LOCATION_FINISH = 1; 30 | /** 31 | * 停止定位 32 | */ 33 | public final static int MSG_LOCATION_STOP= 2; 34 | 35 | public final static String KEY_URL = "URL"; 36 | public final static String URL_H5LOCATION = "file:///android_asset/location.html"; 37 | /** 38 | * 根据定位结果返回定位信息的字符串 39 | * @param loc 40 | * @return 41 | */ 42 | public synchronized static String getLocationStr(AMapLocation location){ 43 | if(null == location){ 44 | return null; 45 | } 46 | StringBuffer sb = new StringBuffer(); 47 | //errCode等于0代表定位成功,其他的为定位失败,具体的可以参照官网定位错误码说明 48 | if(location.getErrorCode() == 0){ 49 | sb.append("定位成功" + "\n"); 50 | sb.append("定位类型: " + location.getLocationType() + "\n"); 51 | sb.append("经 度 : " + location.getLongitude() + "\n"); 52 | sb.append("纬 度 : " + location.getLatitude() + "\n"); 53 | sb.append("精 度 : " + location.getAccuracy() + "米" + "\n"); 54 | sb.append("提供者 : " + location.getProvider() + "\n"); 55 | 56 | sb.append("速 度 : " + location.getSpeed() + "米/秒" + "\n"); 57 | sb.append("角 度 : " + location.getBearing() + "\n"); 58 | // 获取当前提供定位服务的卫星个数 59 | sb.append("星 数 : " + location.getSatellites() + "\n"); 60 | sb.append("国 家 : " + location.getCountry() + "\n"); 61 | sb.append("省 : " + location.getProvince() + "\n"); 62 | sb.append("市 : " + location.getCity() + "\n"); 63 | sb.append("城市编码 : " + location.getCityCode() + "\n"); 64 | sb.append("区 : " + location.getDistrict() + "\n"); 65 | sb.append("区域 码 : " + location.getAdCode() + "\n"); 66 | sb.append("地 址 : " + location.getAddress() + "\n"); 67 | sb.append("兴趣点 : " + location.getPoiName() + "\n"); 68 | //定位完成的时间 69 | sb.append("定位时间: " + formatUTC(location.getTime(), "yyyy-MM-dd HH:mm:ss") + "\n"); 70 | } else { 71 | //定位失败 72 | sb.append("定位失败" + "\n"); 73 | sb.append("错误码:" + location.getErrorCode() + "\n"); 74 | sb.append("错误信息:" + location.getErrorInfo() + "\n"); 75 | sb.append("错误描述:" + location.getLocationDetail() + "\n"); 76 | } 77 | //定位之后的回调时间 78 | sb.append("回调时间: " + formatUTC(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss") + "\n"); 79 | return sb.toString(); 80 | } 81 | 82 | private static SimpleDateFormat sdf = null; 83 | public synchronized static String formatUTC(long l, String strPattern) { 84 | if (TextUtils.isEmpty(strPattern)) { 85 | strPattern = "yyyy-MM-dd HH:mm:ss"; 86 | } 87 | if (sdf == null) { 88 | try { 89 | sdf = new SimpleDateFormat(strPattern, Locale.CHINA); 90 | } catch (Throwable e) { 91 | } 92 | } else { 93 | sdf.applyPattern(strPattern); 94 | } 95 | return sdf == null ? "NULL" : sdf.format(l); 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/app/src/main/java/com/amap/location/demo/view/FeatureView.java: -------------------------------------------------------------------------------- 1 | package com.amap.location.demo.view; 2 | 3 | import android.content.Context; 4 | import android.view.LayoutInflater; 5 | import android.widget.FrameLayout; 6 | import android.widget.TextView; 7 | 8 | import com.amap.location.demo.R; 9 | 10 | public final class FeatureView extends FrameLayout { 11 | 12 | public FeatureView(Context context) { 13 | super(context); 14 | 15 | LayoutInflater layoutInflater = (LayoutInflater) context 16 | .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 17 | layoutInflater.inflate(R.layout.feature, this); 18 | } 19 | 20 | public synchronized void setTitleId(int titleId) { 21 | ((TextView) (findViewById(R.id.title))).setText(titleId); 22 | } 23 | 24 | public synchronized void setDescriptionId(int descriptionId) { 25 | ((TextView) (findViewById(R.id.description))).setText(descriptionId); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/app/src/main/res/drawable-hdpi/gps_point.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amapapi/Android_Location_Demo/a1f994a22f3400556142d6273aa48e2de87040aa/android_studio/AMapLocationDemo/app/src/main/res/drawable-hdpi/gps_point.png -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/app/src/main/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amapapi/Android_Location_Demo/a1f994a22f3400556142d6273aa48e2de87040aa/android_studio/AMapLocationDemo/app/src/main/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/app/src/main/res/drawable-ldpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amapapi/Android_Location_Demo/a1f994a22f3400556142d6273aa48e2de87040aa/android_studio/AMapLocationDemo/app/src/main/res/drawable-ldpi/ic_launcher.png -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/app/src/main/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amapapi/Android_Location_Demo/a1f994a22f3400556142d6273aa48e2de87040aa/android_studio/AMapLocationDemo/app/src/main/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/app/src/main/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amapapi/Android_Location_Demo/a1f994a22f3400556142d6273aa48e2de87040aa/android_studio/AMapLocationDemo/app/src/main/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /android_studio/AMapLocationDemo/app/src/main/res/layout/activity_alarm_location.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 12 | 13 | 18 | 19 | 27 | 28 | 29 | 30 | 31 | 32 | 36 | 37 | 41 | 42 | 50 | 51 | 52 | 53 | 54 | 58 | 59 | 65 | 66 | 72 | 73 | 74 | 78 | 79 |