• 地图应用之 :获取当前位置的经纬度


    一:百度地图

    说明:

    1. 设置定位的模式是 LocationMode.Hight_Accuracy 时,在室内可能无法获取到准确的经纬度,会得到默认的值是4.9E-324

        处理办法是将模式改为Battery_Saving,或到室外

    2.可以根据当前设备网络连接情况和GPS是否开启来设定定位模式

       //获得网络连接情况

              ConnectivityManager cManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

             NetworkInfo info = cManager.getActiveNetworkInfo();

       //获取GPS的连接情况
            LocationManager lManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
         if(lManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                   Toast.makeText(this, "GPS已开启", 0).show();
             }

    3.使用经纬度要在public void onReceiveLocation(BDLocation loc) {}方法中操作

    步骤一:导入百度定位SDK包,以下包中,定位只需要BaiduLBS_Android.jar包

    第二部:在AndroidManifest.xml清单文件中添加访问权限和服务等,代码如下:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     3     package="com.qf.location"
     4     android:versionCode="1"
     5     android:versionName="1.0" >
     6 
     7     <uses-sdk
     8         android:minSdkVersion="8"
     9         android:targetSdkVersion="18" />
    10     
    11     <!-- 添加的内容 1 (访问权限) -->
    12    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    13     <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    14     <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
    15     <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
    16     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    17     <uses-permission android:name="android.permission.INTERNET" />
    18     <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
    19     <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    20     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    21     <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    22     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    23     <uses-permission android:name="android.permission.BROADCAST_STICKY" />
    24     <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    25     <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    26     <!-- 这个权限用于进行网络定位 -->
    27     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
    28     </uses-permission>
    29     <!-- 这个权限用于访问GPS定位 -->
    30     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
    31     </uses-permission>
    32     <!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位 -->
    33     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
    34     </uses-permission>
    35     <!-- 获取运营商信息,用于支持提供运营商信息相关的接口 -->
    36     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
    37     </uses-permission>
    38     <!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位 -->
    39     <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >
    40     </uses-permission>
    41     <!-- 用于读取手机当前的状态 -->
    42     <uses-permission android:name="android.permission.READ_PHONE_STATE" >
    43     </uses-permission>
    44     <!-- 写入扩展存储,向扩展卡写入数据,用于写入离线定位数据 -->
    45     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
    46     </uses-permission>
    47     <!-- 访问网络,网络定位需要上网 -->
    48     <uses-permission android:name="android.permission.INTERNET" />
    49     <!-- SD卡读取权限,用户写入离线定位数据 -->
    50     <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" >
    51     </uses-permission>
    52     <!-- 允许应用读取低级别的系统日志文件 -->
    53     <uses-permission android:name="android.permission.READ_LOGS" >
    54     </uses-permission>
    55     
    56     <application
    57         android:allowBackup="true"
    58         android:icon="@drawable/ic_launcher"
    59         android:label="@string/app_name"
    60         android:theme="@style/AppTheme" >
    61         <activity
    62             android:name="com.qf.location.MainActivity"
    63             android:label="@string/app_name" >
    64             <intent-filter>
    65                 <action android:name="android.intent.action.MAIN" />
    66 
    67                 <category android:name="android.intent.category.LAUNCHER" />
    68             </intent-filter>
    69         </activity>
    70         
    71         <!-- 添加的内容 2 -->
    72          <meta-data
    73             android:name="com.baidu.lbsapi.API_KEY"
    74             android:value="Dt7Q30nVQMKbM3NT6GHnRnGq" />
    75 
    76         <service
    77             android:name="com.baidu.location.f"
    78             android:enabled="true"
    79             android:process=":remote" >
    80         </service>
    81     </application>
    82  
    83 </manifest>
    View Code

    第三步: 申请 百度地图开发密钥:在上步中显示的<meta_data >标签的value属性上设置

    第四步:写代码获取当前位置经纬度:代码如下:

     1 private LocationClient locClient;
     2     
     3     @Override
     4     protected void onCreate(Bundle savedInstanceState) {
     5         super.onCreate(savedInstanceState);
     6         
     7         //显示界面之前初始化 BaiduMap SDK
     8         SDKInitializer.initialize(getApplicationContext());
     9         setContentView(R.layout.activity_main);
    10         
    11   
    12         getLocation();
    13     }
    14    
    15 
    16      //获取手机当前所在位置的经纬度
    17     public void  getLocation(){
    18         //实例化位置客户端
    19         locClient = new LocationClient(getApplicationContext());
    20         //创建一个位置option对象
    21         LocationClientOption locOption = new LocationClientOption();
    22         
    23         //设置option的属性
    24         locOption.setCoorType("bd09II");  // bd09II表示返回的结果是百度的经纬度
    25         locOption.setIsNeedAddress(true); //返回的信息包含当前的地址
    26         locOption.setNeedDeviceDirect(true); //返回的内容包含手机机头的方向
    27         locOption.setScanSpan(5000); //每5秒发起一次定位请求
    28         locOption.setLocationMode(LocationMode.Hight_Accuracy); //高精度模式,网络和GPS
    29         
    30         
    31         //将封装的参数设置到位置客户端
    32         locClient.setLocOption(locOption);
    33         
    34         //给位置客户端注册位置监听器
    35         locClient.registerLocationListener(new BDLocationListener() {
    36             
    37             @Override
    38             public void onReceiveLocation(BDLocation loc) {
    39               double latitude = loc.getLatitude();
    40               double longitude = loc.getLongitude();
    41               Toast.makeText(getApplicationContext(), latitude+":"+longitude, 0).show();
    42               
    43               /*================================此处定义接口=====================*/
    44                 
    45             } 
    46         });
    47         
    48         //启动位置客户端
    49         locClient.start();
    50         
    51     }
    View Code

    二. 高德地图

    说明:高德地图提供了三种定位模式,下面以混合模式为例,

    注册监听的方法为 requestLocationData(String provider, long minTime, float minDistance, AMapLocationListener listener) 方法,其中参数:

    • provider:有三种定位Provider供用户选择,分别是:LocationManagerProxy.GPS_PROVIDER,代表使用手机GPS定位;LocationManagerProxy.NETWORK_PROVIDER,代表使用手机网络定位;LocationProviderProxy.AMapNetwork,代表高德网络定位服务,混合定位。
    • minTime:位置变化的通知时间,单位为毫秒。如果为-1,定位只定位一次。
    • minDistance:位置变化通知距离,单位为米。
    • listener:定位监听者。

    步骤一:将高德定位包拷贝到libs目录下

    步骤二:在AndroidManifest.xml文件中添加相应的内容

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     3     package="com.don.amaplocationdemotest"
     4     android:versionCode="1"
     5     android:versionName="1.0" >
     6 
     7     <uses-sdk
     8         android:minSdkVersion="8"
     9         android:targetSdkVersion="18" />
    10     
    11     <!-- 添加内容1 :访问权限 -->
    12     <uses-permission android:name="android.permission.INTERNET" />
    13     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    14     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    15     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    16     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    17     <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    18     <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    19     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    20     <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
    21     <uses-permission android:name="android.permission.WAKE_LOCK" />
    22     <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    23     
    24 
    25     <application
    26         android:allowBackup="true"
    27         android:icon="@drawable/ic_launcher"
    28         android:label="@string/app_name"
    29         android:theme="@style/AppTheme" >
    30         
    31          <!-- 添加内容2 -->
    32        <meta-data
    33             android:name="com.amap.api.v2.apikey"
    34             android:value="b772627bc616d5bf0b4f9371807cd60a" />
    35        
    36         <activity
    37             android:name="com.don.amaplocationdemotest.MainActivity"
    38             android:label="@string/app_name" >
    39             <intent-filter>
    40                 <action android:name="android.intent.action.MAIN" />
    41 
    42                 <category android:name="android.intent.category.LAUNCHER" />
    43             </intent-filter>
    44         </activity>
    45         <activity
    46             android:name="com.don.amaplocationdemotest.MNetLocationActivity"
    47             android:label="@string/title_activity_mnet_location" >
    48         </activity>
    49         <activity
    50             android:name="com.don.amaplocationdemotest.MGpsLocationActivity"
    51             android:label="@string/title_activity_mnet_location" >
    52         </activity>
    53         <activity
    54             android:name="com.don.amaplocationdemotest.MMultyLocationActivity"
    55             android:label="@string/title_activity_mnet_location" >
    56         </activity>
    57     </application>
    58 
    59 </manifest>
    View Code

    步骤三:在官网申请key,添加到步骤二的清单文件中

    步骤四:编写代码

      1 package com.don.amaplocationdemotest;
      2 
      3 import android.app.Activity;
      4 import android.location.Location;
      5 import android.os.Bundle;
      6 import android.os.Handler;
      7 import android.os.Message;
      8 import android.view.Window;
      9 import android.widget.TextView;
     10 
     11 import com.amap.api.location.AMapLocation;
     12 import com.amap.api.location.AMapLocationListener;
     13 import com.amap.api.location.LocationManagerProxy;
     14 import com.amap.api.location.LocationProviderProxy;
     15 
     16 public class MMultyLocationActivity extends Activity implements
     17         AMapLocationListener {
     18     private LocationManagerProxy locationManagerProxy;
     19     private TextView latitude_txt; // 纬度
     20     private TextView longitude_txt; // 经度
     21     private TextView address_txt; // 地址信息
     22     private Handler handler = new Handler() {
     23         public void handleMessage(android.os.Message msg) {
     24             if (msg != null) {
     25                 AMapLocation loc = (AMapLocation) msg.obj;
     26                 latitude_txt.setText("纬度:" + loc.getLatitude());
     27                 longitude_txt.setText("经度:" + loc.getLongitude());
     28                 address_txt.setText("地址信息:" + loc.getAddress());
     29 
     30             }
     31 
     32         };
     33 
     34     };
     35 
     36     @Override
     37     protected void onCreate(Bundle savedInstanceState) {
     38         super.onCreate(savedInstanceState);
     39         requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置没有标题栏
     40         setContentView(R.layout.activity_mmulty_location);
     41         init();
     42         initView();
     43     }
     44 
     45     public void init() {
     46         locationManagerProxy = LocationManagerProxy.getInstance(this);
     47         locationManagerProxy.setGpsEnable(false);
     48         locationManagerProxy.requestLocationData(
     49                 LocationProviderProxy.AMapNetwork, 30 * 1000, 10, this);
     50     }
     51 
     52     public void initView() {
     53         latitude_txt = (TextView) findViewById(R.id.mutxt_latitude);
     54         longitude_txt = (TextView) findViewById(R.id.mutxt_longitude);
     55         address_txt = (TextView) findViewById(R.id.mutxt_address);
     56 
     57     }
     58 
     59     @Override
     60     public void onLocationChanged(AMapLocation aMapLocation) {
     61         if (aMapLocation != null
     62                 && aMapLocation.getAMapException().getErrorCode() == 0) {
     63             Message msg = Message.obtain(handler, 0, aMapLocation);
     64             handler.sendMessage(msg);
     65         }
     66     }
     67 
     68     @Override
     69     public void onLocationChanged(Location arg0) {
     70         // TODO Auto-generated method stub
     71 
     72     }
     73 
     74     @Override
     75     public void onProviderDisabled(String arg0) {
     76         // TODO Auto-generated method stub
     77 
     78     }
     79 
     80     @Override
     81     public void onProviderEnabled(String arg0) {
     82         // TODO Auto-generated method stub
     83 
     84     }
     85 
     86     @Override
     87     public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
     88         // TODO Auto-generated method stub
     89 
     90     }
     91 
     92     @Override
     93     protected void onPause() {
     94         super.onPause();
     95         // 移除定位请求
     96         locationManagerProxy.removeUpdates(this);
     97         // 销毁定位
     98         locationManagerProxy.destroy();
     99     }
    100 
    101 }
    View Code
  • 相关阅读:
    如何解决"应用程序无法启动,因为应用程序的并行配置不正确"问题
    C/C++时间函数使用方法
    vim: C++文件之间快速切换(含视频)
    HOWTO install commonlisp on ubuntu
    TagSoup home page
    Quicklisp beta
    What is Lispbox?
    猫人女王
    Lisp in a box 安装指南 JAAN的专栏 博客频道 CSDN.NET
    Ubuntu 12.04 改造指南 | Ubuntusoft
  • 原文地址:https://www.cnblogs.com/d-on/p/4106704.html
Copyright © 2020-2023  润新知