Map positioning of Tencent developed by Android

Posted by phpPunk on Tue, 16 Jun 2020 09:33:53 +0200

In this paper, only the teaching positioning function is used. If you need to search, 2D or 3D map, you can go to Tencent map development platform to view the api documents. Link: Tencent map

1, To Tencent map development flat download location sdk, quick access: Tencent map positioning

2, Add permissions and configure AppKey in the Android manifest file of the project

<! -- get accurate position through GPS -- >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<! -- get rough position through network -- >
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<! -- access to the network. Some location information needs to be obtained from the network server -- >
<uses-permission android:name="android.permission.INTERNET" />
<! -- access WiFi status. Need WiFi information for network location -- >
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<! -- modify WiFi status. Launch WiFi scan, need WiFi information for network location -- >
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<! -- access network status and check network availability. Network operator related information is required for network location -- >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<! -- changes in access to the network require some information for network location -- >
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<! -- to access the current status of mobile phone, device id is required for network location -- >
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<! -- support A-GPS auxiliary positioning -- >
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<! -- for log log -- >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application>
    <meta-data  android:name= "TencentMapSDK"  android:value= "Key you applied for" / >
</application>

3, Android studio configuration engineering there are two ways to configure Android Studio:
Method 1:
a) Actively add so files and SDK s in libs (if there is a chat or other so file conflict in the project, you can put them together, so do I. at present, there is no bug)


b) At build.gradle Configuration in dependencies of files

  implementation fileTree(include: ['*.jar'], dir: 'libs')
  //Tencent map positioning
  implementation files('libs/TencentLocationSdk_v7.2.6.jar')

Method 2:
Gradle configuration reference
a) Modifying the root gradle configuration

llprojects {
    repositories {
        jcenter()
        google()
        mavenCentral()
    }
}

b) Modify the project directory gradle configuration
At build.gradle Add configuration to dependencies of files

implementation 'com.tencent.map.geolocation:TencentLocationSdk-openplatform:7.2.6'

4, Code obfuscation
In proguard-rules.pro Code confusion in files (copy and paste)

-keepclassmembers class ** {
    public void on*Event(...);
}

-keep class c.t.**{*;}
-keep class com.tencent.map.geolocation.**{*;}

-dontwarn  org.eclipse.jdt.annotation.**
-dontwarn  c.t.**

5, Next is the code

public class LocationActivity extends BaseActivity implements TencentLocationListener {
    private static final int MY_PERMISSION_REQUEST_CODE = 10000;
    private TencentLocationRequest request;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);
        initdata();
    }

    public void initdata() {
        checkPermission();
    }
    public void setLocation() {
        request = TencentLocationRequest.create();
        request.setInterval(60000);//Set positioning period (position listener callback period), in ms
        request.setRequestLevel(REQUEST_LEVEL_POI); // 0: contains latitude and longitude, 1: contains latitude and longitude, location name, location address, 3: contains latitude and longitude, location of the Chinese mainland administrative division, 4: contains latitude and longitude, location of the Chinese mainland administrative divisions and peripheral POI list.
        request.setAllowGPS(true);
        request.setAllowDirection(true);
        request.setIndoorLocationMode(true);

        TencentLocationListener listener = this;
        TencentLocationManager locationManager = TencentLocationManager.getInstance(LocationActivity.this);
        int error = locationManager.requestLocationUpdates(request, listener);
        if (error == 0) {
            //Successfully registered location listener
        } else {
            //Failed to register location listener keytool -v -list -keystore
            locationNowlocation.setText("seek failed");
        }
    }

    @Override
    public void onLocationChanged(TencentLocation tencentLocation, int error, String s) {
        if (TencentLocation.ERROR_OK == error) { // Positioning successful
            //Positioning successful
            if (tencentLocation != null) {
                String address = tencentLocation.getCity();
                locationNowlocation.setText(address);
            }
        } else { // seek failed
            Tools.ToastTextThread(LocationActivity.this, "seek failed");
        }
    }

    @Override
    public void onStatusUpdate(String name, int status, String desc) {
        /*if (name.equals("GPS")) {
            if (status == 0) {
                Tools.ToastTextThread(LocationActivity.this, "GPS Close ');
            }
        }*/
    }

    public void checkPermission() {
        //Check whether there are corresponding permissions, and add corresponding permissions according to their own needs
        boolean isAllGranted = checkPermissionAllGranted(
                new String[]{
                        Manifest.permission.ACCESS_COARSE_LOCATION
                }
        );
        // If all three permissions are owned, execute the backup code directly
        if (isAllGranted) {
            setLocation();
        } else {
            // Multiple permissions are requested at one time. If other permissions have been granted, they will be ignored automatically
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, MY_PERMISSION_REQUEST_CODE);
        }
    }

    /**
     * Check if you have all the specified permissions
     */
    private boolean checkPermissionAllGranted(String[] permissions) {
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                // As long as one permission is not granted, false will be returned directly
                return false;
            }
        }
        return true;
    }

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

        if (requestCode == MY_PERMISSION_REQUEST_CODE) {
            boolean isAllGranted = true;
            // Determine whether all permissions have been granted
            for (int grant : grantResults) {
                if (grant != PackageManager.PERMISSION_GRANTED) {
                    isAllGranted = false;
                    break;
                }
            }

            if (isAllGranted) {
                // All rights granted
                setLocation();
            } else {
                // The pop-up dialog box will tell the user why they need permission, and guide the user to open the permission button manually in permission management
                setLocationDialog("");
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //Turn off location monitor
        TencentLocationManager locationManager = TencentLocationManager.getInstance(this);
        locationManager.removeUpdates(this);
    }
}

Topics: Android network Gradle SDK