欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android实现地理定位功能

时间:2021-09-17 09:39:24|栏目:Android代码|点击:

最近试着写了一下Android的定位,代码在下面,比较简单
首先需要的权限有

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

然后是里面的代码

package com.example.a5.myapplication; 
 
import android.Manifest; 
import android.content.Context; 
import android.content.pm.PackageManager; 
import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.support.v4.app.ActivityCompat; 
import android.support.v7.app.AlertDialog; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.Window; 
import android.view.WindowManager; 
import android.webkit.WebView; 
import android.widget.Button; 
import android.widget.TextView; 
 
public class MainActivity extends AppCompatActivity { 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  requestWindowFeature(Window.FEATURE_NO_TITLE); 
  //全屏 
  getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
    WindowManager.LayoutParams.FLAG_FULLSCREEN); 
  setContentView(R.layout.activity_main); 
  Button button = (Button) findViewById(R.id.button); 
  button.setOnClickListener(new View.OnClickListener() { 
   @Override 
   public void onClick(View v) { 
    //获取位置服务 
    LocationManager locationManager = (LocationManager) MainActivity.this.getSystemService(Context.LOCATION_SERVICE); 
    Criteria criteria = new Criteria(); 
    //ACCURACY_HIGH/ACCURACY_LOW精度选择 
    criteria.setAccuracy(Criteria.ACCURACY_FINE); 
    //高度 
    criteria.setAltitudeRequired(true); 
    //方位信息 
    criteria.setBearingRequired(true); 
    //是否允许付费 
    criteria.setCostAllowed(true); 
    //对电量的要求 
    criteria.setPowerRequirement(Criteria.POWER_LOW); 
    //速度 
    criteria.setSpeedRequired(true); 
    //获取最佳服务 
    String provider = locationManager.getBestProvider(criteria, true); 
    //权限检查 
    if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     return; 
    } 
    //获取到了位置 
    Location location = locationManager.getLastKnownLocation(provider); 
    //开启地理位置监听定位类型、毫秒、米、监听时间 
    locationManager.requestLocationUpdates(provider,3000,1,new listener()); 
    locateResult(location); 
 
   } 
  }); 
 } 
 public class listener implements LocationListener { 
 
  @Override 
  public void onLocationChanged(Location location) { 
   //位置变化,获取最新的位置 
   locateResult(location); 
  } 
 
  @Override 
  public void onStatusChanged(String provider, int status, Bundle extras) { 
 
  } 
 
  @Override 
  public void onProviderEnabled(String provider) { 
 
  } 
 
  @Override 
  public void onProviderDisabled(String provider) { 
 
  } 
 } 
 
 public void locateResult(Location location){ 
  new AlertDialog.Builder(MainActivity.this) 
    .setMessage("纬度" + String.valueOf(location.getLatitude()) + 
      "\n经度" + String.valueOf(location.getLongitude()) + 
      "\n速度" + String.valueOf(location.getSpeed()) + 
      "\n高度" + String.valueOf(location.getAltitude()) + 
      "\n朝向" + String.valueOf(location.getBearing()) 
    ) 
    .setNegativeButton("取消",null) 
    .show(); 
 } 
} 

最后上图:

上一篇:android播放gif格式图片示例

栏    目:Android代码

下一篇:Android开发之ProgressDialog进度对话框用法示例

本文标题:Android实现地理定位功能

本文地址:http://www.codeinn.net/misctech/175665.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有