lingwei2020 2020-01-11 18:10 采纳率: 100%
浏览 588
已采纳

这个是关于定位的,在最后那里无法重写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条回答 默认 最新

  • 关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 就很莫名其妙,本来正常的Excel,突然变成了这种一格一页
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?