GPS定位和高德地图1

GPS定位和高德地图1


GPS技术实现定位功能
1、加权限
2、LocationManager manager = (LocationManager) getSystemService(LOCATION_SERVICE);
3、Location位置类
location.getAltitude() 海拔
location.getLatitude() 纬度
location.getLongitude() 经度
当位置发生改变时获得地址信息,调用LocationManager中的方法
requestLocationUpdates(位置提供器,位置变化的时间间隔,位置变化的距离间隔,事件监听locationListener)

5、位置提供器
LocationManager.NETWORK_PROVIDER
LocationManager.GPS_PROVIDER

权限

   <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"/>

GPS定位

public class MainActivity extends AppCompatActivity {

LocationManager manager;
String[] permissions;

boolean flag;//没有权限
boolean isok;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    permissions = new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION};
    //获得定位的管理类
    //申请权限
    initpermissions();
    manager = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }


    manager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);

}
public void initpermissions(){

    //6.0以上的版本
    if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
        for (String per :permissions){
            if (checkSelfPermission(per) != PackageManager.PERMISSION_GRANTED){
                flag=true;
                break;
            }
        }

        if (flag){
            requestPermissions(permissions,100);
        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode==100){
        for (int resule :grantResults){
            if (resule != PackageManager.PERMISSION_GRANTED){
                isok=true;
                break;
            }
        }
        if (isok){
            Log.e("###","权限没有申请成功");
        }
    }
}

LocationListener locationListener= new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
        Log.e("##","海拔"+location.getAltitude());
        Log.e("##","经度"+location.getLatitude());
        Log.e("##","纬度"+location.getLongitude());
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
};

}

效果
在这里插入图片描述

高德地图

依赖
implementation ‘com.amap.api:location:4.2.0’
implementation ‘com.amap.api:map2d:5.2.0’
布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
<com.amap.api.maps2d.MapView
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</com.amap.api.maps2d.MapView>

</LinearLayout>

Activity

public class MainActivity extends AppCompatActivity implements LocationSource, AMapLocationListener {

MapView mapView;
AMap aMap;
OnLocationChangedListener mListener;
AMapLocationClient mapLocationClient;
AMapLocationClientOption mapLocationOption;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mapView = findViewById(R.id.map);
    checkpermission();

    mapView.onCreate(savedInstanceState);

    if (aMap ==null)
        aMap = mapView.getMap();

    init();

}

private void init() {

    MyLocationStyle myLocationStyle = new MyLocationStyle();
    myLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.drawable.dian));//设置定位符
    myLocationStyle.strokeColor(Color.BLACK);//设置边框颜色
    myLocationStyle.radiusFillColor(Color.argb(100,0,0,180));//填充颜色
    myLocationStyle.strokeWidth(1.0f);
    aMap.setMyLocationStyle(myLocationStyle);
    aMap.setLocationSource(this);
    aMap.setMyLocationEnabled(true);

}

@Override
protected void onDestroy() {
    super.onDestroy();

    mapView.onDestroy();
}

@Override
protected void onPause() {
    super.onPause();

    mapView.onPause();
}

@Override
protected void onResume() {
    super.onResume();

    mapView.onResume();
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mapView.onSaveInstanceState(outState);
}

@Override
public void activate(OnLocationChangedListener onLocationChangedListener) {

     mListener = onLocationChangedListener;
    //初始化定位
    if (mapLocationClient == null){
        mapLocationClient  = new AMapLocationClient(getApplicationContext());
        mapLocationOption = new AMapLocationClientOption();
        mapLocationClient.setLocationListener(this);//监听
        mapLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        mapLocationClient.setLocationOption(mapLocationOption);
        mapLocationClient.startLocation();
    }
}

@Override
public void deactivate() {

    mListener =null;

    if (mapLocationClient != null){
        mapLocationClient.stopLocation();
        mapLocationClient.onDestroy();
    }
    mapLocationClient = null;
}

//定位坐标改变
@Override
public void onLocationChanged(AMapLocation aMapLocation) {

    if (aMapLocation.getErrorCode()==0)
        mListener.onLocationChanged(aMapLocation);//显示定位符

}

private String[] permissions = new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION};


public void checkpermission(){

    boolean flag=true;//默认全部被申请过
    for (int i =0;i<permissions.length;i++){//只要有一个没有申请成功
        if (!(ActivityCompat.checkSelfPermission(this,permissions[i])!=PackageManager.PERMISSION_GRANTED)){
            flag=false;
        }
    }
    if (!flag){
        //动态申请权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(permissions,100);
        }
    }
}

//动态申请权限
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode==100){
        boolean flags= true;
        for (int i=0;i<grantResults.length;i++){
            if (grantResults[i]!=PackageManager.PERMISSION_GRANTED){
                flags=false;
            }
        }
        if (flags){
            Toast.makeText(this, "ok", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this, "error", Toast.LENGTH_SHORT).show();
        }
    }
}
}

效果
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值