lingwei2020
lingwei2020
采纳率60%
2020-01-11 18:10

这个是关于定位的,在最后那里无法重写onConnectHotSpotMessage(String var1, int var2)运行结果如图

图片说明
代码如下:package com.example.lbstest;

import android.Manifest;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.location.Location;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.mapapi.map.BaiduMap;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
public LocationClient mLocationClient;
private TextView positionText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mLocationClient = new LocationClient(getApplicationContext());
    mLocationClient.registerLocationListener(new MyLocationListener());
    setContentView(R.layout.activity_main);
    positionText = (TextView) findViewById(R.id.position_text_view);
    List<String> permissionList = new ArrayList<>();
    if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION);
    }
    if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
        permissionList.add(Manifest.permission.READ_PHONE_STATE);
    }
    if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }
    if (!permissionList.isEmpty()) {
        String[] permissions = permissionList.toArray(new String[permissionList.size()]);
        ActivityCompat.requestPermissions(MainActivity.this, permissions, 1);
        mLocationClient.start();
    } else {
        requestLocation();
    }
}
private void requestLocation() {
    mLocationClient.start();
}
@Override
public void onRequestPermissionsResult(int requestCode,  String[] permissions,  int[] grantResults) {
    switch (requestCode){
        case 1:if (grantResults.length>0){
            for (int result:grantResults){
                if (result!=PackageManager.PERMISSION_GRANTED){
                    Toast.makeText(this,"必须同意所有权限才能使用本程序",Toast.LENGTH_SHORT).show();
                    finish();
                    return;
                }
            }
            requestLocation();
        }else {
            Toast.makeText(this,"发生未知错误",Toast.LENGTH_SHORT).show();
            finish();
        }
            break;
        default:
    }
}

public class MyLocationListener implements BDLocationListener {

   @Override
   public void onReceiveLocation( final BDLocation bdLocation) {
       runOnUiThread(new Runnable() {
           @Override
           public void run() {
               StringBuilder currentPosition = new StringBuilder();
               currentPosition.append("纬度:").append(bdLocation.getLatitude()).append("/n");
               currentPosition.append("经度:").append(bdLocation.getLongitude()).append("/n");
               currentPosition.append("定位方式:");
               if (bdLocation.getLocType() == BDLocation.TypeGpsLocation) {
                   currentPosition.append("GPS");
               } else if (bdLocation.getLocType() == BDLocation.TypeNetWorkLocation) {
                   currentPosition.append("网络");
               }
               positionText.setText(currentPosition);
           }
       });
   }

}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答