使用外部来源的位置更新多个Google地图

时间:2016-07-03 22:23:56

标签: android google-maps google-maps-markers

我制作了一个应用程序,可以从多个车辆的数据库中获取经度和纬度。我将标记放在地图上,10秒后我将更新它们。 问题是,在一段时间后,应用程序统计数据会滞后,直到它停止工作。有人能帮帮我吗?

这是我的代码:

private GoogleMap mMap;
String denumLinie;
double _lon[],_lat[];
private List<Marker> markers = new ArrayList<>();
final int interval_update = 10000; //10 secunde
Handler handler = new Handler();


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

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(MapsActivity.this);

    Intent intent = getIntent();
    denumLinie = intent.getStringExtra("DenumLinie");

}


void aflaLatLong(String denumLinie){  //here i use a doInBackground to get the coordinates from database
    String actiune = "aflaLatLong";

    BackgroundWorker backgoundWorker = new BackgroundWorker(this);
    backgoundWorker.execute(actiune, denumLinie);

    SharedPreferences sharedPreferences = getSharedPreferences("MyData3", Context.MODE_PRIVATE);
    String linii = sharedPreferences.getString("Coordonate", "");
    try {
        jsonParsing(linii);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

 void jsonParsing(String linii) throws JSONException {  //decoding JSON

     _lat = new double[50];
     _lon = new double[50];
     String v1,v2;

     JSONArray arrayLinii = new JSONArray(linii);

     JSONObject jsonObject = null;

     for (int i = 0; i < arrayLinii.length(); i++) {
         jsonObject = arrayLinii.getJSONObject(i);
         try {
             v1 = jsonObject.getString("Longitudine");
             v2 = jsonObject.getString("Latitudine");
             _lon[i] = Double.parseDouble(v1);
             _lat[i] = Double.parseDouble(v2);
         }catch (NumberFormatException e){e.printStackTrace();}
          catch (NullPointerException e) {e.printStackTrace();}
     }

     setMarker();


 }

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(44.847837,24.875965),10.0f));
    aflaLatLong(denumLinie);   //asking for location
}




void setMarker() throws NullPointerException{ //place the markes on the map


    for(int i = 0 ; i < _lat.length ; i++) {

        String titlu = "Autobuz ";
        if (_lat[i]!=0.0) {
            LatLng coordonate = new LatLng(_lat[i], _lon[i]);
            markers.add(mMap.addMarker(new MarkerOptions().position(coordonate).title(titlu)));
        //    mMap.moveCamera(CameraUpdateFactory.newLatLng(coordonate));
        }
    }
    handler.postDelayed(updatareMarker,interval_update);
}

Runnable updatareMarker = new Runnable() {  //clear the old markers and get the new location after 10 s
    @Override
    public void run() {
        for(Marker marker : markers)
            marker.remove();

        markers.clear();


        aflaLatLong(denumLinie);

        handler.postDelayed(this,interval_update);
    }
};

@Override
protected void onDestroy() {
    handler.removeCallbacks(updatareMarker);

    super.onDestroy();
}

0 个答案:

没有答案