百度地图开发(五)个性化地图

 效果图:

百度地图开发(五)个性化地图_第1张图片百度地图开发(五)个性化地图_第2张图片

步骤:

1:在http://lbsyun.baidu.com/customv2/index.html里面编辑自己想要的个性化地图

2:编辑完成之后下载下来,是一个json格式的文件

3:把json文件放入到自己的as项目里面

百度地图开发(五)个性化地图_第3张图片

准备工作就已经做完了,下面就是代码部分:

布局文件:



    
        

        

        
    

MainActivity里面的代码:

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.baidu.mapapi.map.MapView;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private MapView mMapView;
    private TextView mGexinghua;
    private static String PATH = "custom_map_config.json";
    private Boolean GXH = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置个性化地图,一定要在父类构造方法前
        setMapCustomFile(this, PATH);
        setContentView(R.layout.activity_main);
        setInit();
    }

    public void setInit() {
        mMapView = findViewById(R.id.mapview);
        mGexinghua = findViewById(R.id.gexinghua);
        mGexinghua.setOnClickListener(this);
    }

    /**
     * 设置个性化地图config文件路径
     */
    public static void setMapCustomFile(Context context, String PATH) {
        FileOutputStream out = null;
        InputStream inputStream = null;
        String moduleName = null;
        try {
            inputStream = context.getAssets()
                    .open(PATH);
            byte[] b = new byte[inputStream.available()];
            inputStream.read(b);
            moduleName = context.getFilesDir().getAbsolutePath();
            File f = new File(moduleName + "/" + PATH);
            if (f.exists()) {
                f.delete();
            }
            f.createNewFile();
            out = new FileOutputStream(f);
            out.write(b);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        MapView.setCustomMapStylePath(moduleName + "/" + PATH);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.gexinghua:
                if (GXH == true) {
                    mGexinghua.setTextColor(this.getResources().getColor(R.color.colorffffff));
                    mGexinghua.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.bg_btn));
                    //设置开启个性化地图
                    MapView.setMapCustomEnable(true);
                    GXH = false;
                } else if (GXH == false) {
                    mGexinghua.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    mGexinghua.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.bg_btn2));
                    //设置关闭个性化地图
                    MapView.setMapCustomEnable(false);
                    GXH = true;
                }
                break;
        }
    }

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

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

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

下载地址:https://download.csdn.net/download/lanrenxiaowen/10743788

你可能感兴趣的:(自用,百度地图)