zoukankan      html  css  js  c++  java
  • andorid简易定位

    权限:

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

    布局:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_dingwei"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.fuxin_zhongji.dingwei">
    <TextView
    android:id="@+id/tv1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
    <TextView
    android:id="@+id/tv2"
    android:layout_below="@+id/tv1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
    </RelativeLayout>

    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);
    }
    }
  • 相关阅读:
    (待续)【转载】 Deep Reinforcement Learning Doesn't Work Yet(这里有一篇深度强化学习劝退文)
    【转载】 深度强化学习走入「死胡同」,继续死磕电子游戏还是另辟蹊径?
    【转载】 Docker-关于docker cpu的限制后,实际效果的研究
    个人常用的 matplotlib 绘图模板
    【转载】共轭梯度法(视频讲解) 数值分析6(3共轭梯度法) ——苏州大学
    【转载】 向量,标量对向量求导数
    【转载】 Linux Hang Task 简介
    AOC U2790PC对比上一代的AOC U2790PQU怎么样?
    屏幕ppi
    ubuntu杀死进程
  • 原文地址:https://www.cnblogs.com/98k98k/p/7878875.html
Copyright © 2011-2022 走看看