我有一个JSON文件,其中有大约1800个路径。在1条路径中,有多个点像单个路径一样。
我已经完成了这段代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
import com.golfcart.bean.VillagesListBean;
import com.golfcart.utils.JSONParser;
import com.golfcart.utils.UtilFunction;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
public class MyNavigationGolfcart extends FragmentActivity implements OnClickListener {
private GoogleMap googleMap;
double sourcelat, sourcelng, destlat, destlng;
private String result1;
boolean flag = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mynav);
addMap();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
getPath();
}
}).start();
}
private void getPath() {
BufferedReader br;
try {
br = new BufferedReader(new InputStreamReader(getAssets()
.open("finalpath.txt")));
String strLine = null;
String s = "";
while ((strLine = br.readLine()) != null) {
s = s + strLine;
}
JSONObject json = new JSONObject(s);
if (json != null) {
JSONArray features = json.getJSONArray("features");
for (int i = 0; i < features.length(); i++) {
JSONObject obj = features.getJSONObject(i);
// Log.e("i"+i,"s"+obj.getJSONObject("geometry"));
if (obj.getJSONObject("geometry").getString("type").equalsIgnoreCase("LineString")) {
final JSONArray array = obj.getJSONObject("geometry").getJSONArray("coordinates");
if (array != null && array.length() > 0) {
MyNavigationGolfcart.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (flag) {
try {
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(array.getJSONArray(0).getDouble(1), array.getJSONArray(0).getDouble(0)), 12));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
flag = false;
}
for (int z = 0; z < array.length() - 1; z++) {
try {
googleMap.addPolyline(new PolylineOptions()
.add(new LatLng(array.getJSONArray(z).getDouble(1), array.getJSONArray(z).getDouble(0)), new LatLng(array.getJSONArray(z + 1).getDouble(1), array.getJSONArray(z + 1).getDouble(0)))
.width(8)
.color(Color.BLUE).geodesic(true));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void addMap() {
if (googleMap == null) {
// GoogleMapOptions googleMapsOptions = new GoogleMapOptions();
// googleMapsOptions.zOrderOnTop( true );
// MapFragment mapFragment = MapFragment.newInstance(googleMapsOptions);
// mapFragment.getMap();
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if (googleMap != null) {
// googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
}
}
}
}
最初,我使用
addmap()
添加了地图,然后从文件中获取了JSON,其中有1800个路径。然后我走了一条路,并使用
polyline
在地图上绘制。我没有得到我的代码有什么问题。 最佳答案
您必须在for循环中将所有LatLng点添加到PolyLineOptions中,然后仅将此一次PolyLineOptions添加到地图中
PolylineOptions polylineOptions;
List<LatLng> totalLanLngPoints = new ArrayList<LatLng>();
for (int z = 0; z < array.length() - 1; z++) {
try {
totalLanLngPoints.add(new LatLng(array.getJSONArray(z).getDouble(1), array.getJSONArray(z).getDouble(0));
totalLanLngPoints.add(new LatLng(array.getJSONArray(z + 1).getDouble(1), array.getJSONArray(z + 1).getDouble(0));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
polylineOptions = new PolylineOptions().width(8).color(Color.BLUE).addAll(totalLanLngPoints).geodesic(true);
googleMap.addPolyline(polylineOptions);
您也可以添加For Lopp,这会将点添加到线程中的PolyLineOptions中,而仅将最后一行添加到地图中的PolyLineOptions中添加到onUIThread方法中