博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
andorid简易定位
阅读量:4574 次
发布时间:2019-06-08

本文共 2214 字,大约阅读时间需要 7 分钟。

权限:

 

布局:

 

main:

package com.example.fuxin_zhongji; import android.Manifest; import android.content.Context; import android.content.pm.PackageManager; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; public class dingwei extends AppCompatActivity { private LocationManager locationManager; private TextView tv1; private TextView tv2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_dingwei); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); initLinter(); initView(); } private void initLinter() { final LocationListener lo = new LocationListener() { @Override public void onLocationChanged(Location location) { //纬度 double latitude = location.getLatitude(); //经度 double longitude = location.getLongitude(); tv1.setText(String.valueOf(latitude)); tv2.setText(String.valueOf(longitude)); } @Override public void onStatusChanged(String s, int i, Bundle bundle) { } @Override public void onProviderEnabled(String s) { } @Override public void onProviderDisabled(String s) { } }; if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, lo); } private void initView() { tv1 = (TextView) findViewById(R.id.tv1); tv2 = (TextView) findViewById(R.id.tv2); } }

转载于:https://www.cnblogs.com/98k98k/p/7878875.html

你可能感兴趣的文章
SQL With As 用法Sql 四大排名函数(ROW_NUMBER、RANK、DENSE_RANK、NTILE)简介
查看>>
装饰者模式——Java设计模式
查看>>
39.递推练习: 菲波那契数列(2)
查看>>
排序精讲
查看>>
【bzoj3172】 Tjoi2013—单词
查看>>
【uoj2】 NOI2014—起床困难综合症
查看>>
js return的用法
查看>>
子节点填充父元素除去一固定高度后的剩余高度
查看>>
[原]IOS 后台发送邮件
查看>>
(转)JAVA Calendar详解
查看>>
转: 编码,charset,乱码,unicode,utf-8与net简单释义
查看>>
C#--正则匹配
查看>>
5.30 考试修改+总结
查看>>
BA-设计施工调试流程
查看>>
C#-CLR各版本特点
查看>>
css3背景透明文字不透明
查看>>
实验四
查看>>
mssql sqlserver 取消数值四舍五入的方法分享
查看>>
[记录] JavaScript 中的事件分类
查看>>
《java JDK7 学习笔记》之接口与多态
查看>>