Android中获取当前位置信息

发布时间:2019-06-13 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Android中获取当前位置信息脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

这篇教程主要介绍了在AndROId平台上如何使用服务完成定位功能。众所周知,Android设备的当前位置信息,对开发创新性App、解决人们日常生活问题有极大帮助。在Android平台开发定位相关的应用程序,需要位置提供者。有两种类型的位置提供者:

  1. GPS定位
  2. 网络定位

以上两种类型,任何一种都可以获得用户或者用户设备的位置信息。但是,它们各有优劣,推荐两者同时使用。GPS 定位,在室内反应迟缓,比较耗时;网络定位,在没有网络的时候无法获得位置信息。

GPS定位 VS 网络定位

  • 获取位置坐标时,网络定位比GPS定位稍快。
  • GPS在室内定位非常缓慢,并且比较耗
  • 网络定位依赖蜂窝网络,获取的是最近的网络基站的位置。
  • GPS定位数据相对精确,得到我们当前的位置信息。

获取定位数据

  1. Manifest文件中授权,接收定位数据。
  2. 创建LocationManager实例,将其指向定位服务。
  3. LocationManager请求定位数据。
  4. 定数数据改变时,LocationListener接收更新的定位数据。

授权接收定位更新数据

Manifest文件中获取如下权限,然后可以通过定位提供者获得定位数据:

<manifest ... >
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
   <uses-permission android:name="android.permission. ACCESS_COARSE_LOCATION" />
   <uses-permission android:name="android.permission.internet" />
</manifest>

定位提供者必需要有INTERNET权限和ACCESS_FINE_LOCATION权限。同时,网络定位还需要ACCESS_COARSE _LOCATION权限。

创建LocationManager实例,指向定位服务

无论何种类型的Android后台Service,需要获得其引用才能使用。同样,通过getSystemService()方法获得定位服务的引用,然后将这个引用将添加到新创建的LocationManager实例中,示例如下:

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

从LocationManager请求当前位置

穿件位置服务引用后,通过LocationManagerrequestLocationUpdates()方法可以请求位置更新信息。调用方法时,需要位置提供者、最后一次更新距今的时间(秒)、距离LocationListener对象。调用后LocationListener对象会根据位置进行更新。

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

通过LocationListener,获得更新位置数据

根据指定的距离或时间间隔,LocationListener会收到更新通知。

示例:获取当前位置

这个示例通过GPS定位获取当前位置数据。主要代码如下:

package com.javapapers.android.geolocationfinder;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.widget.TextView;

import android.util.LOG;

public class MainActivity extends Activity implements LocationListener {
    protected LocationManager locationManager;
    protected LocationListener locationListener;
    protected Context context;
    TextView txtLat;
    String lat;
    String provider;
    protected String latitude, longitude;
    protected boolean gps_enabled, network_enabled;

    @override
    protected void onCreate(Bundle savedInstancestate) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtLat = (TextView) findViewById(R.id.textview1);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                0, this);
    }

    @Override
    public void onLocationChanged(Location location) {
        txtLat = (TextView) findViewById(R.id.textview1);
        txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:"
                + location.getLongitude());
    }

    @Override
    public void onProviderDisabled(String provider) {
        Log.d("Latitude", "disable");
    }

    @Override
    public void onProviderEnabled(String provider) {
        Log.d("Latitude", "enable");
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.d("Latitude", "status");
    }
}

布局文件如下,

<RelativeLayout >"http://schemas.android.COM/apk/res/android"
    >"http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

</RelativeLayout>

Manifest文件如下,

<?XMl version="1.0" encoding="utf-8"?>
<manifest >"http://schemas.android.com/apk/res/android"
    package="com.javapapers.android.geolocationfinder"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17"        />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppBaseTheme" >
        <activity
            android:name="com.javapapers.android.geolocationfinder.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

输出效果

Android中获取当前位置信息

提示:如果使用模拟器运行这个示例,需要将准确的经纬度发送到模拟器。

如何发送经纬度到模拟器

  • 打开Eclipse中 DDMS 视图(Window>Open Perspective
  • 选择模拟器
  • 选择模拟器控制选项
  • 在位置控制面板,选择手动输入,添加经纬度数据,点击“发送”

原文 Get Current Location in Android
翻译 伯乐在线 - imesong

脚本宝典总结

以上是脚本宝典为你收集整理的Android中获取当前位置信息全部内容,希望文章能够帮你解决Android中获取当前位置信息所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。