Android地图获取位置

作者: Karma1026 | 来源:发表于2016-03-02 17:01 被阅读935次

    Android地图获取位置

    • 1.首先获取LocationManager管理类
    LocationManager locationManager = (LocationManager)getSystemServices(Context.LOCATION_SERVICE);
    
    • 2.获得支持哪些定位服务
    String provider = LocationManager.GPS_PROVIDER;
    
    • 3.获得Location类,里面包含经纬度信息
    Location location=locationManager.getLastknowLocation(provider);
    
    • 4.添加权限
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    

    完整代码部分

    package com.example.kevin.locationtest;
    
    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.support.v4.app.ActivityCompat;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import java.util.List;
    
    public class MainActivity extends AppCompatActivity {
        private TextView locationView;
        private LocationManager locationManager;
        private String provider;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            locationView = (TextView) findViewById(R.id.location_view);
            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            List<String> locationList = locationManager.getProviders(true);
            if (locationList.contains(LocationManager.GPS_PROVIDER)) {
                provider = LocationManager.GPS_PROVIDER;
            } else if (locationList.contains(LocationManager.NETWORK_PROVIDER)) {
                provider = LocationManager.NETWORK_PROVIDER;
            } else {
                Toast.makeText(getApplicationContext(), "没有可用的定位服务", Toast.LENGTH_LONG).show();
            }
    
            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;
            }
            Location location = locationManager.getLastKnownLocation(provider);
            if (location != null) {
                showLocation(location);
            }
            /**
             * 第一个参数是提供定位的方式
             * 第二个参数是多少秒刷新
             * 第三个参数是移动多少距离
             * 第四个参数是监听器
             */
            locationManager.requestLocationUpdates(provider, 5000, 1, locationListener);
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            //将监听器移除
            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.removeUpdates(locationListener);
        }
    
        LocationListener locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                showLocation(location);
            }
    
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
    
            }
    
            @Override
            public void onProviderEnabled(String provider) {
    
            }
    
            @Override
            public void onProviderDisabled(String provider) {
    
            }
        };
    
        private void showLocation(Location location) {
            String content = "--->" + location.getLatitude() + "\n" + location.getLongitude();
            locationView.setText(content);
        }
    }
    

    相关文章

      网友评论

      • 末日之花:LocationManager locationManager = (LocationManager)getSystemServices(Context.LOCATION_SERVICE);
        是 getSystemService
        Karma1026:@末日之花 感谢指出
      • 岁月无痕灬灬:为什么我按照你的获取不到地址信息
        Karma1026:6.0添加动态权限就好了啊
        岁月无痕灬灬:@王小賢 是6.0权限问题么
        Karma1026:@岁月无痕灬灬 不会吧!我自己测试都可以

      本文标题: Android地图获取位置

      本文链接:https://www.haomeiwen.com/subject/lvsnkttx.html