Java Android百度地图聚合算法

2018-09-03  本文已影响0人  IT码哥

开发中遇到一个技术问题,要求自己重组服务器返回的数据,实现百度地图聚合效果
package com.kyexpress.kydemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

private List<PositionInfo> plist = new ArrayList<>();
private List<List<PositionInfo>> s = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initList();
   ;
  Log.i("TAG", new Gson().toJson( selectAray(plist,s)));
}

/**
 *  生成测试数据
 * [
 {
 "lat": 11.11111,
 "lng": 121.324344,
 "pId": "p_0",
 "pName": "北京-0"
 },
 {
 "lat": 11.11111,
 "lng": 121.324344,
 "pId": "p_1",
 "pName": "北京-1"
 },
 {
 "lat": 11.11112,
 "lng": 121.324345,
 "pId": "p_2",
 "pName": "北京-2"
 },
 {
 "lat": 11.11112,
 "lng": 121.324345,
 "pId": "p_3",
 "pName": "北京-3"
 },
 {
 "lat": 11.11113,
 "lng": 121.324346,
 "pId": "p_4",
 "pName": "北京-4"
 },
 {
 "lat": 11.11113,
 "lng": 121.324346,
 "pId": "p_5",
 "pName": "北京-5"
 },
 {
 "lat": 11.11113,
 "lng": 121.324346,
 "pId": "p_6",
 "pName": "北京-6"
 },
 {
 "lat": 77.77777,
 "lng": 849.270408,
 "pId": "p_7",
 "pName": "北京-7"
 },
 {
 "lat": 88.88888,
 "lng": 970.594752,
 "pId": "p_8",
 "pName": "北京-8"
 },
 {
 "lat": 99.99999,
 "lng": 1091.919096,
 "pId": "p_9",
 "pName": "北京-9"
 }
 ]
  */

public  void initList() {

    for (int i = 0; i < 10; i++) {
        PositionInfo position = new PositionInfo();
        position.setpName("北京-" + i);
        position.setpId("p_" + i);
        if (i < 2) {
            position.setLat(11.11111);
            position.setLng(121.324344);

        } else if (i < 4) {

            position.setLat(11.11112);
            position.setLng(121.324345);
        } else if (i < 7) {

            position.setLat(11.11113);
            position.setLng(121.324346);
        } else {

            position.setLat(11.11111 * i);
            position.setLng(121.324344 * i);
        }
        plist.add(position);
    }

    String result = new Gson().toJson(plist);

    Log.i("TAG", "=======" + result);

}


/**
 *
 * [
 [
 {
 "lat": 11.11111,
 "lng": 121.324344,
 "pId": "p_0",
 "pName": "北京-0"
 },
 {
 "lat": 11.11111,
 "lng": 121.324344,
 "pId": "p_1",
 "pName": "北京-1"
 }
 ],
 [
 {
 "lat": 11.11112,
 "lng": 121.324345,
 "pId": "p_2",
 "pName": "北京-2"
 },
 {
 "lat": 11.11112,
 "lng": 121.324345,
 "pId": "p_3",
 "pName": "北京-3"
 }
 ],
 [
 {
 "lat": 11.11113,
 "lng": 121.324346,
 "pId": "p_4",
 "pName": "北京-4"
 },
 {
 "lat": 11.11113,
 "lng": 121.324346,
 "pId": "p_5",
 "pName": "北京-5"
 },
 {
 "lat": 11.11113,
 "lng": 121.324346,
 "pId": "p_6",
 "pName": "北京-6"
 }
 ],
 [
 {
 "lat": 77.77777,
 "lng": 849.270408,
 "pId": "p_7",
 "pName": "北京-7"
 }
 ],
 [
 {
 "lat": 88.88888,
 "lng": 970.594752,
 "pId": "p_8",
 "pName": "北京-8"
 }
 ],
 [
 {
 "lat": 99.99999,
 "lng": 1091.919096,
 "pId": "p_9",
 "pName": "北京-9"
 }
 ]
 ]
 * 聚合算法
 * @param list
 * @param res
 * @return
 */
public List<List<PositionInfo>>  selectAray(List<PositionInfo>  list,List<List<PositionInfo>>  res){

    if (list.size() == 0){
        return null;
    }
    List<PositionInfo>  diffAry = new ArrayList<>();
    List<PositionInfo>  sameAry = new ArrayList<>();
    sameAry.add(list.get(0));
    for (int i = 1 ; i < list.size();i++){
        PositionInfo p = list.get(i);
        double lat= p.getLat();
        double  lng = p.getLng();

        PositionInfo t = sameAry.get(0);
        double lat2= t.getLat();
        double  lng2 = t.getLng();
        if (lat  == lat2 && lng == lng2){
            sameAry.add(p);
        } else {
            diffAry.add(p);
        }
    }
    res.add(sameAry);
    if (diffAry.size() != 0){
        selectAray(diffAry,res);
    }
    return res;
}

}

上一篇 下一篇

猜你喜欢

热点阅读