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>

  

转载于:https://www.cnblogs.com/yudada/p/7881284.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值