Android Baidu map draws points and tracks

1. Title

Android Baidu map draws points and tracks

2. Environment

win10 AndroidStudio

Three, code implementation

Note: The commented out part is that I directly fixed the data before, just to see if the code can run normally, and see if I can get the results I expected. No comments, I use JSON strings as the data carrier.

Draw the part of the marked point:

package com.example.adressandtrace;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import org.json.JSONException;
import java.util.List;

import Entity.Address;

public class MainActivity extends AppCompatActivity {
    
    
    private MapView mMapView = null;
    private BaiduMap mBaiduMap = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_main);
        //自4.3.0起,百度地图SDK所有接口均支持百度坐标和国测局坐标,用此方法设置您使用的坐标类型.
        //包括BD09LL和GCJ02两种坐标,默认是BD09LL坐标。
        SDKInitializer.setCoordType(CoordType.BD09LL);
        //获取地图控件引用
        mMapView = (MapView) findViewById(R.id.bmapView);
        //定义Maker坐标点
//        mBaiduMap= mMapView.getMap();
//        LatLng point = new LatLng(39.963175, 116.400244);
构建Marker图标
//        BitmapDescriptor bitmap = BitmapDescriptorFactory
//                .fromResource(R.drawable.icon_marka);
构建MarkerOption,用于在地图上添加Marker
        OverlayOptions option = new MarkerOptions()
                .position(point)
                .icon(bitmap);
//        Log.v("MY1","Has Done");
//        OverlayOptions option = new MarkerOptions().icon(bitmap).position(point);
在地图上添加Marker,并显示
//        Log.v("MY2","Has Done");
//        mBaiduMap.addOverlay(option);
//        Log.v("MY3","Has Done");


        Intent intent = getIntent();
        String jsonString = intent.getStringExtra("jsonString");
        Log.i("jsonString",jsonString);
        Log.i("jsonString","这是绘制点activity");
        JieXiJson jieXiJson = new JieXiJson();
        List<Address> list = null;
        try {
    
    
            list = jieXiJson.getAddress(jsonString);
            Log.i("jsonString",list.toString());
            Log.i("jsonString","map输出后的检查语句");
        } catch (JSONException e) {
    
    
            e.printStackTrace();
        }
//        定义Maker坐标点
        mBaiduMap= mMapView.getMap();
        if(list!= null ){
    
    
            for(Address address : list){
    
    
                double latitude = Double.valueOf(address.getLatitude());
                double longitude = Double.valueOf(address.getLongitude());
                LatLng point = new LatLng(latitude, longitude);
                System.out.println(latitude);
                BaiduMap mBaiduMap = mMapView.getMap();
                //定义Maker坐标点
//        LatLng point = new LatLng(27.904198, 112.930552);七栋比较准确的位置
//        LatLng point = new LatLng(27.901332, 112.92163);
                //构建Marker图标
                BitmapDescriptor bitmap = BitmapDescriptorFactory
                        .fromResource(R.drawable.icon_gcoding);
                //构建MarkerOption,用于在地图上添加Marker
                OverlayOptions option = new MarkerOptions()
                        .position(point)
                        .icon(bitmap);
                //在地图上添加Marker,并显示
                mBaiduMap.addOverlay(option);
            }

        }else{
    
    
            Toast.makeText(MainActivity.this, "没有获取到位置信息或者位置数据库为空!", Toast.LENGTH_LONG).show();
        }



    }
    @Override
    protected void onResume() {
    
    
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
        mMapView.onResume();
    }
    @Override
    protected void onPause() {
    
    
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
        mMapView.onPause();
    }
    @Override
    protected void onDestroy() {
    
    
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
        mMapView.onDestroy();
    }
}


Part of the trace:

package com.example.adressandtrace;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.model.LatLng;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import Entity.Address;

public class TraceActivity extends AppCompatActivity {
    
    
    private MapView mMapView = null;
    private BaiduMap mBaiduMap = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_main);
        //自4.3.0起,百度地图SDK所有接口均支持百度坐标和国测局坐标,用此方法设置您使用的坐标类型.
        //包括BD09LL和GCJ02两种坐标,默认是BD09LL坐标。
        SDKInitializer.setCoordType(CoordType.BD09LL);
        //获取地图控件引用
        mMapView = (MapView) findViewById(R.id.bmapView);
//        Intent intent = getIntent();
//        String jsonString = intent.getStringExtra("jsonString");
//        JieXiJson jieXiJson = new JieXiJson();
//        Address address = jieXiJson.getAddress(jsonString);
        定义Maker坐标点
//        mBaiduMap= mMapView.getMap();
//        double latitude = Double.valueOf(address.getLatitude());
//        double longitude = Double.valueOf(address.getLongitude());
//         LatLng point1 = new LatLng(latitude, longitude);
//        LatLng point1 = new LatLng(27.101299, 112.94076);
//        LatLng point2 = new LatLng(27.901299, 112.94076);
        Intent intent = getIntent();
        String jsonString = intent.getStringExtra("jsonString");
        JieXiJson jieXiJson = new JieXiJson();
        List<Address> list = null;
        try {
    
    
            list = jieXiJson.getAddress(jsonString);
            Log.i("jsonString",list.toString());
            Log.i("jsonString","map输出后的检查语句");
        } catch (JSONException e) {
    
    
            e.printStackTrace();
        }
        if(list != null){
    
    
            BaiduMap mBaiduMap = mMapView.getMap();
            //构建Marker图标
            BitmapDescriptor bitmap1 = BitmapDescriptorFactory
                    .fromResource(R.drawable.icon_marka);
            BitmapDescriptor bitmap2 = BitmapDescriptorFactory
                    .fromResource(R.drawable.icon_markb);
//构建MarkerOption,用于在地图上添加Marker
//        OverlayOptions option = new MarkerOptions()
//                .position(point)
//                .icon(bitmap);
            Log.v("MY1","Has Done");
            Address address1 = list.get(0);
            Address address2 = list.get(list.size()-1);
            double latitude1 = Double.valueOf(address1.getLatitude());
            double longitude1 = Double.valueOf(address1.getLongitude());
            LatLng point1= new LatLng(latitude1, longitude1);
            double latitude2 = Double.valueOf(address2.getLatitude());
            double longitude2 = Double.valueOf(address2.getLongitude());
            LatLng point2 = new LatLng(latitude2, longitude2);
            OverlayOptions option1 = new MarkerOptions().icon(bitmap1).position(point1);
            OverlayOptions option2 = new MarkerOptions().icon(bitmap2).position(point2);
//在地图上添加Marker,并显示
            Log.v("MY2","Has Done");
            mBaiduMap.addOverlay(option1);
            mBaiduMap.addOverlay(option2);
            Log.v("MY3","Has Done");
            List<LatLng> points = new ArrayList<LatLng>();
            for(Address address :list) {
    
    
                //定义Maker坐标点
//        LatLng point = new LatLng(27.904198, 112.930552);七栋比较准确的位置
                double latitude = Double.valueOf(address.getLatitude());
                double longitude = Double.valueOf(address.getLongitude());
                LatLng point = new LatLng(latitude, longitude);
                points.add(point);
            }
                //设置折线的属性
                OverlayOptions mOverlayOptions = new PolylineOptions()
                        .width(10)
                        .color(0xAAFF0000)
                        .points(points);
                //在地图上绘制折线
                //mPloyline 折线对象
                Overlay mPolyline = mBaiduMap.addOverlay(mOverlayOptions);

        }else{
    
    
            Toast.makeText(TraceActivity.this, "没有获取到位置信息或者位置数据库为空!", Toast.LENGTH_LONG).show();
        }
    }
    @Override
    protected void onResume() {
    
    
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
        mMapView.onResume();
    }
    @Override
    protected void onPause() {
    
    
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
        mMapView.onPause();
    }
    @Override
    protected void onDestroy() {
    
    
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
        mMapView.onDestroy();
    }
}


Four, experiment screenshots

This is the json array displayed on the browser page after viewing the real-time location (that is, the last data in the database).
json

This is the data I locate outdoors. The data is accurate and the error is very small.
(PS: When the emulator turns on the layer, the center of the screen is Beijing by default, so when using the emulator, you need to know where the location you are positioning is roughly, and then look for it on the map)
Mark point



This is an experimental screenshot of drawing a trajectory. Because I use a local server to transfer data, the mobile phone and computer must be in the same local area network to connect. Data collection is not convenient, so I just input a few longitude and latitude information into MySQL to simulate it. Therefore, if you have a small partner who needs a demo of the database storage location information, it is recommended to try a cloud server or cloud service. This kind of data does not need to be in the same local area network, and the data can be uploaded with direct traffic.

This picture shows the input time period, and Baidu map shows the trajectory of the corresponding time period.
time

Point A in the figure below represents the starting point, which is the location of the starting time.
Point B represents the end point, which is the location of the end time.
Track

If you have any questions, please leave a message to exchange!

Guess you like

Origin blog.csdn.net/weixin_43752257/article/details/113041298