前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >位置定位(LocationManager)

位置定位(LocationManager)

作者头像
李小白是一只喵
发布2020-06-11 16:46:29
2.6K0
发布2020-06-11 16:46:29
举报
文章被收录于专栏:算法微时光

image.png

目录

LocationManager

LocationManager是Android 提供的Location 服务,来获得当前的位置信息和卫星信息。

要使用它,先得获得系统所提供的location_service.

代码语言:javascript
复制
        LocationManager lm;
        lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

定位服务的提供一般有3种:

  • GPS_PROVIDER GPS获得
  • NETWORK_PROVIDER 网络获得
  • PASSIVE_PROVIDER 被动提供其他应用程序提供

通过api获取现有支持的定位服务模式:

代码语言:javascript
复制
lm.getAllProviders();

代码实战

xml文件:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <ListView
        android:id="@+id/pro"
        android:layout_width="368dp"
        android:layout_height="495dp"
        tools:layout_editor_absoluteX="20dp"
        tools:layout_editor_absoluteY="20dp" />
</android.support.constraint.ConstraintLayout>

代码文件:

代码语言:javascript
复制
package com.example.user.location;

import android.content.Context;
import android.location.LocationManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    ListView providers;
    LocationManager lm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        providers = (ListView)findViewById(R.id.pro);
        lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

        List<String> provideNames = lm.getAllProviders();

        ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1,
                provideNames);

        providers.setAdapter(adapter);
    }
}

获取定位

使用requestLocationUpdates注册监听,定时获取定位hal service返回的数据

整体项目如下:

manifest.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.user.gps">
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <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">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

active_main.xml

代码语言:javascript
复制
package com.example.user.gps;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Test
    public void useAppContext() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getTargetContext();

        assertEquals("com.example.user.gps", appContext.getPackageName());
    }
}

代码:

代码语言:javascript
复制
package com.example.user.gps;

import android.annotation.SuppressLint;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONObject;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TextView positionTextView;
    private TextView positionLatLng;
    private LocationManager locationManager;
    private String provider;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        positionTextView=(TextView)findViewById(R.id.position_text_view);
        positionLatLng=(TextView)findViewById(R.id.position_plain_text);
        locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        //获取所有可用的位置提供器

        provider=LocationManager.GPS_PROVIDER;

        Location location=locationManager.getLastKnownLocation(provider);
        if(location!=null){
            //显示当前设备的位置信息
            Log.d("lidu---", "location!=null");
            showLocation(location);
        }

        Log.d("lidu---", "location==null");
        locationManager.requestLocationUpdates(provider, 1000, 1, locationListener);
    }


    //LocationListener 用于当位置信息变化时由 locationManager 调用
    LocationListener locationListener=new LocationListener(){

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            Log.d("test", "onLocationChanged");
            //更新当前设备的位置信息
            showLocation(location);
        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

    };

    private void showLocation(final Location location){
        StringBuilder sb = new StringBuilder();
        sb.append(location.getLatitude());
        positionTextView.setText(sb.toString());

    }

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 目录
  • LocationManager
  • 代码实战
  • 获取定位
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档