亲宝软件园·资讯

展开

Android实现定位功能 Android实现地理定位功能

GISuuser 人气:0
想了解Android实现地理定位功能的相关内容吗,GISuuser在本文为您仔细讲解Android实现定位功能的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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(); 
 } 
} 

最后上图:

加载全部内容

相关教程
猜你喜欢
用户评论