zoukankan      html  css  js  c++  java
  • android 定位

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

    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'

    package net.bwie.locationandsessior;
    
    import android.content.Context;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    /**
     * 1、获取定位管理者,这是一个系统级的服务
     * 2、请求定位更新,这里设置网络/GPS定位,定位监听器
     * 3、定位监听器中可以获取到定位对象,其中包括经纬度等信息
     */
    public class MainActivity extends AppCompatActivity implements
            View.OnClickListener, LocationListener {
    
        protected Button mLocationBtn;
        protected TextView mResultTv;
        private LocationManager mLocationManager;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            super.setContentView(R.layout.activity_main);
            initView();
        }
    
        private void initView() {
            mLocationBtn = (Button) findViewById(R.id.location_btn);
            mLocationBtn.setOnClickListener(MainActivity.this);
            mResultTv = (TextView) findViewById(R.id.result_tv);
        }
    
        @Override
        public void onClick(View view) {
            if (view.getId() == R.id.location_btn) {
                locate();
            }
        }
    
        // 定位
        private void locate() {
            // 定位管理者
            mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            // NETWORK_PROVIDER:使用网络定位(粗略定位)
            // GPS_PROVIDER:使用GPS定位(精准定位)
            mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 200, 200, this);
        }
    
        // 获取当前设备的经纬度
        @Override
        public void onLocationChanged(Location location) {
            double latitude = location.getLatitude();// 纬度
            double longitude = location.getLongitude();// 经度
    
            mResultTv.setText("纬度:" + latitude + ",经度:" + longitude);
        }
    
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
    
        }
    
        @Override
        public void onProviderEnabled(String provider) {
    
        }
    
        @Override
        public void onProviderDisabled(String provider) {
    
        }
    }


    <?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context="net.bwie.locationandsessior.MainActivity"> <Button android:id="@+id/location_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="定位"/> <TextView android:id="@+id/result_tv" android:text="结果" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>

      

  • 相关阅读:
    客户端模块化的精益求精
    从风控的角度解析如何防止客户刷单
    ubuntu 安装 codelite
    使用curl在命令行中下载文件
    awk的使用
    优秀web资源
    Asp.net 生成静态页面
    提高ASP.NET首页性能的十大方法
    ASP.NET伪静态
    URLRewriter.dll的使用
  • 原文地址:https://www.cnblogs.com/yudada/p/7881284.html
Copyright © 2011-2022 走看看