如何防止谷歌地图android中的多段线重叠?
大家好,这可能是一个愚蠢的问题,但到目前为止,我还没有找到任何解决办法。现在让我问一下我的疑问,我正在使用多条多段线绘制多条管线,每条多段线都有不同的颜色,但当两个点与最后一条多段线相交时,如何防止它被覆盖。它必须看起来是唯一的第一条路线应该有一种颜色,所有其他路线必须有相同的颜色如何做到这一点,让我张贴代码,我已经尝试了到目前为止:如何防止谷歌地图android中的多段线重叠?,android,google-maps,google-polyline,Android,Google Maps,Google Polyline,大家好,这可能是一个愚蠢的问题,但到目前为止,我还没有找到任何解决办法。现在让我问一下我的疑问,我正在使用多条多段线绘制多条管线,每条多段线都有不同的颜色,但当两个点与最后一条多段线相交时,如何防止它被覆盖。它必须看起来是唯一的第一条路线应该有一种颜色,所有其他路线必须有相同的颜色如何做到这一点,让我张贴代码,我已经尝试了到目前为止: public class GetDistance extends AsyncTask<Double, Void, String> {
public class GetDistance extends AsyncTask<Double, Void, String> {
private ProgressDialog pd;
private static final int READ_TIMEOUT = 6000;
private static final int CONNECTION_TIMEOUT = 6000;
private int flag;
public GetDistance(int flag) {
this.flag=flag;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(VisitTravel.this);
pd.setMessage("Please wait");
pd.show();
}
@Override
protected String doInBackground(Double... strings) {
URL url;
try {
url = new URL("http://maps.googleapis.com/maps/api/directions/json?origin=" + strings[0] + "," + strings[1] + "&destination=" + strings[2] + "," + strings[3] + "&sensor=false&units=metric&mode=driving&alternatives=true");
HttpURLConnection conn;
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("POST");
InputStream in;
in = new BufferedInputStream(conn.getInputStream());
StringBuilder buffer = new StringBuilder();
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(in));
String inputLine;
while ((inputLine = reader.readLine()) != null)
buffer.append(inputLine).append("\n");
if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
Log.e("empty", "empty");
}
JsonResponse = buffer.toString();
Log.d("response", JsonResponse);
} catch (IOException e1) {
e1.printStackTrace();
}
return JsonResponse;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
pd.dismiss();
if(flag==1) {
new ParserTask().execute(result);
}}
}
private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String, String>>>> {
private ArrayList<LatLng> points;
@Override
protected List<List<HashMap<String, String>>> doInBackground(String... jsonData) {
JSONObject jObject;
List<List<HashMap<String, String>>> routes = null;
try {
jObject = new JSONObject(jsonData[0]);
DirectionJSONParser parser = new DirectionJSONParser();
// Starts parsing data
routes = parser.parse(jObject);
} catch (Exception e) {
e.printStackTrace();
}
return routes;
}
@Override
protected void onPostExecute(List<List<HashMap<String, String>>> result) {
PolylineOptions polylineOptionss=null;
// MarkerOptions markerOptions = new MarkerOptions();
// Traversing through all the routes
for (int i = 0; i < result.size(); i++) {
points = new ArrayList<>();
// Fetching i-th route
List<HashMap<String, String>> path = result.get(i);
// Fetching all the points in i-th route
for (int j = 0; j < path.size(); j++) {
HashMap<String, String> point = path.get(j);
if (j == 0) {
duration = point.get("duration");
Log.d("duration", duration);
continue;
}
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);
points.add(position);
}
polylineOptionss=new PolylineOptions();
// Adding all the points in the route to LineOptions
polylineOptionss.addAll(points);
// polylineOptions.width(7);
// Random rnd = new Random();
// int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
if(i==0) {
polylineOptions0=new PolylineOptions();
polylineOptions0.addAll(points);
// mGoogleMap.setTrafficEnabled(true);
polylineOptions0.width(15);
polylineOptions0.color(Color.parseColor("#9c27b0"));
polylineOptions0.geodesic(true);
Polyline polyline= mGoogleMap.addPolyline(polylineOptions0);
polyline.setTag(duration);
polyline.setClickable(true);
}
//Here only differentiating each and every route.
else if(i==1){
polylineOptions1=new PolylineOptions();
polylineOptions1.addAll(points);
polylineOptions1.geodesic(true);
polylineOptions1.width(15);
// mGoogleMap.setTrafficEnabled(true);
polylineOptions1.color(Color.parseColor("#9e9e9e"));
Polyline polyline= mGoogleMap.addPolyline(polylineOptions1);
polyline.setTag(duration);
polyline.setClickable(true);
///
}
else if(i==2){
polylineOptions2=new PolylineOptions();
polylineOptions2.addAll(points);
polylineOptions2.geodesic(true);
polylineOptions2.width(15);
polylineOptions2.color(Color.parseColor("#9c27b0"));
Polyline polyline= mGoogleMap.addPolyline(polylineOptions2);
polyline.setTag(duration);
polyline.setClickable(true);
// mGoogleMap.setTrafficEnabled(true);
//
}
else {
polylineOptions3=new PolylineOptions();
polylineOptions3.addAll(points);
// mGoogleMap.setTrafficEnabled(true);
polylineOptions3.width(15);
polylineOptions3.geodesic(true);
polylineOptions3.color(Color.parseColor("#9e9e9e"));
Polyline polyline= mGoogleMap.addPolyline(polylineOptions3);
polyline.setTag(duration);
polyline.setClickable(true);
/// polylineOptions3.color(Color.parseColor("#ffffff"));
}
}
setBottomSheet(jsonresponse, edt.getText().toString(),1);
CameraAnimation(polylineOptionss);
// mGoogleMap.addPolyline(polylineOptions);
// Drawing polyline in the Google Map for the i-th route
}
}
公共类GetDistance扩展异步任务{
私营部门;
专用静态最终整型读取超时=6000;
专用静态最终int连接\u超时=6000;
私人int标志;
公共GetDistance(int标志){
这个.flag=flag;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pd=新建进度对话框(访问travel.this);
pd.setMessage(“请稍候”);
pd.show();
}
@凌驾
受保护字符串背景(双…字符串){
网址;
试一试{
url=新url(“http://maps.googleapis.com/maps/api/directions/json?origin=“+strings[0]+”、“+strings[1]+”&destination=“+strings[2]+”、“+strings[3]+”&sensor=false&units=metric&mode=driving&alternatives=true”);
httpurl连接连接;
conn=(HttpURLConnection)url.openConnection();
conn.setReadTimeout(读取超时);
连接设置连接超时(连接超时);
conn.setRequestMethod(“POST”);
输入流输入;
in=新的BufferedInputStream(conn.getInputStream());
StringBuilder缓冲区=新的StringBuilder();
缓冲读取器;
reader=新的BufferedReader(新的InputStreamReader(in));
字符串输入线;
而((inputLine=reader.readLine())!=null)
buffer.append(inputLine.append(“\n”);
if(buffer.length()==0){
//流为空。在分析中没有意义。
Log.e(“空”、“空”);
}
JsonResponse=buffer.toString();
Log.d(“响应”,JsonResponse);
}捕获(IOE1异常){
e1.printStackTrace();
}
返回JsonResponse;
}
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
pd.解散();
如果(标志==1){
新建ParserTask()。执行(结果);
}}
}
私有类ParserTask扩展了AsyncTask{
私有数组列表点;
@凌驾
受保护列表doInBackground(字符串…jsonData){
JSONObject jObject;
列表路由=空;
试一试{
jObject=新的JSONObject(jsonData[0]);
DirectionJSONParser=新的DirectionJSONParser();
//开始解析数据
routes=parser.parse(jObject);
}捕获(例外e){
e、 printStackTrace();
}
返回路线;
}
@凌驾
受保护的void onPostExecute(列表结果){
多段线选项多段线选项S=null;
//MarkerOptions MarkerOptions=新MarkerOptions();
//穿越所有路线
对于(int i=0;i final List<Polyline> polylines = new ArrayList<>();
for(int i= 0; i<paths.size(); i++ ){
polylines.add(mMap.addPolyline(paths.get(i)));
}
mMap.setOnPolylineClickListener(new GoogleMap.OnPolylineClickListener() {
@Override
public void onPolylineClick(Polyline polyline) {
for(int i= 0; i<polylines.size(); i++ ){
polylines.get(i).setColor(Color.argb(255,187,189,191));
polylines.get(i).setZIndex(0);
}
polyline.setColor(Color.argb(255,102,157,246));
polyline.setZIndex(2);
}
});