1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package cn.flightfeather.thirdappmodule.activity;
 
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
 
import com.amap.api.maps.CoordinateConverter;
import com.amap.api.maps.model.LatLng;
import com.ping.greendao.gen.ScenseDao;
import com.ping.greendao.gen.SiteDao;
 
import java.util.List;
 
import cn.flightfeather.thirdappmodule.CommonApplication;
import cn.flightfeather.thirdappmodule.R;
import cn.flightfeather.thirdappmodule.bean.entity.Scense;
import cn.flightfeather.thirdappmodule.bean.entity.Site;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.http.Body;
import retrofit2.http.POST;
 
public class DebugTransSiteLatlngActivity extends AppCompatActivity {
    private TextView tv_main;
    private Button btn_scense;
    private Button btn_site;
    private ProgressBar pb_main;
    private CommonApplication application;
    private SiteDao siteDao;
    private ScenseDao scenseDao;
    private DebugService debugService;
    private List<Scense> scenseList;
    private List<Site> siteList;
    private int position =0;
    private  CoordinateConverter converter;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_debug_trans_site_latlng);
        tv_main = (TextView) findViewById(R.id.tv_main);
        btn_scense = (Button) findViewById(R.id.btn_scense);
        btn_site = (Button) findViewById(R.id.btn_site);
        pb_main = (ProgressBar) findViewById(R.id.pb_main);
 
        application = (CommonApplication) getApplication();
        siteDao = application.getDaoSession().getSiteDao();
        scenseDao = application.getDaoSession().getScenseDao();
        debugService = application.getRetrofit().create(DebugService.class);
 
        converter = new CoordinateConverter(this);
        converter.from(CoordinateConverter.CoordType.BAIDU);
 
        btn_site.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                position = 0;
                siteList = siteDao.loadAll();
                pb_main.setMax(siteList.size());
                updateOneSite(siteList.get(position));
            }
        });
        btn_scense.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                position = 0;
                scenseList = scenseDao.queryBuilder().where(ScenseDao.Properties.Districtname.eq("金山区")).list();
                pb_main.setMax(scenseList.size());
                updateOneScense(scenseList.get(position));
            }
        });
    }
 
    private void updateOneScense(Scense scense){
       if (scense.getRemark()==null){
           if (scense.getLongitude()!=null&&scense.getLatitude()!=null){
               LatLng oldLatLng = new LatLng(scense.getLatitude(),scense.getLongitude());
               converter.coord(oldLatLng);
               LatLng newLatlng = converter.convert();
               scense.setLatitude(newLatlng.latitude);
               scense.setLongitude(newLatlng.longitude);
 
           }
           scense.setRemark("已转换为高德坐标");
       }
 
        Call<ResponseBody> updateScense = debugService.updateScense(scense);
            updateScense.enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                    if (response.body()!=null){
                        position++;
                        pb_main.setProgress(position);
                        tv_main.setText(position+"/"+scenseList.size());
                        if (position<scenseList.size()){
                            updateOneScense(scenseList.get(position));
                        }
 
                    }else if (response.errorBody()!=null){
                        Toast.makeText(application, "失败", Toast.LENGTH_SHORT).show();
                    }
                }
 
                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {
                    Toast.makeText(application, "网络链接错误", Toast.LENGTH_SHORT).show();
                }
            });
        }
 
        private void updateOneSite(Site site){
            if (site.getRemark() ==null){
                if (site.getSitelatitude()!=null&&site.getSitelongitude()!=null){
                    LatLng oldLatLng = new LatLng(site.getSitelatitude(),site.getSitelongitude());
                    converter.coord(oldLatLng);
                    LatLng newLatlng = converter.convert();
                    site.setSitelatitude(newLatlng.latitude);
                    site.setSitelongitude(newLatlng.longitude);
                }
                site.setRemark("已转换为高德坐标");
            }
 
            Call<ResponseBody> updateSite = debugService.updateSite(site);
            updateSite.enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                    if (response.body()!=null){
                        position++;
                        pb_main.setProgress(position);
                        tv_main.setText(position+"/"+siteList.size());
                        if (position<siteList.size()){
                            updateOneSite(siteList.get(position));
                        }
                    }else if (response.errorBody()!=null){
                        Toast.makeText(application, "失败", Toast.LENGTH_SHORT).show();
                    }
                }
 
                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {
                    Toast.makeText(application, "网络链接错误", Toast.LENGTH_SHORT).show();
                }
            });
        }
 
 
    interface DebugService{
        @POST("scense/")
        Call<ResponseBody> updateScense(@Body Scense scense);
 
        @POST("site/")
        Call<ResponseBody> updateSite(@Body Site site);
    }
}