我制作的应用程序使用谷歌地图显示您偏离之前拍摄的路径。我可以用不同的颜色显示偏差发生的位置,但我必须初始化一个新的PolyOptions(),导致"波涛汹涌的"折线代替平滑折线,就像我的基本路线一样。如果我没有初始化新的PolyOptions(),那么当我开始偏离时,它会将路径的很大一部分变为红色,即使我只是尝试将颜色应用于该特定折线。当我回到路上时,在偏离之后,我失去了偏离的颜色,这一切都回到了黑色。任何人都可以向我解释发生了什么以及如何将新的PolyOptions()方法中的正确着色与使用一个PolyLineOptions()
的平滑度相结合for(HaulEvent event:cycle){ LatLng locToAdd = new LatLng(event.Latitude,event.Longitude);
if (event.cycle_num < 2) {
m_aPL.add(mMap.addPolyline(m_PolyLines.geodesic(true)
.color(Color.BLACK)
.add(locToAdd)
.clickable(true)));
m_aPolyLineList = (ArrayList<LatLng>) m_PolyLines.getPoints();
} else { //after we've got a base cycle drawn
if (PolyUtil.isLocationOnPath(locToAdd, m_aBaseRoute, true, tolerance)) {
m_aPL.add(mMap.addPolyline(new PolylineOptions().geodesic(true)
.color(Color.BLACK)
.add(new LatLng(prevLocation.getLatitude(), prevLocation.getLongitude()), locToAdd)
.clickable(true)));
//m_aPolyLineList = (ArrayList<LatLng>) m_PolyLines.getPoints();
} else {
m_aPL.add(mMap.addPolyline(new PolylineOptions().geodesic(true)
.color(Color.RED)
.add(new LatLng(prevLocation.getLatitude(),prevLocation.getLongitude()),locToAdd)
.clickable(true)));
//m_aPolyLineList = (ArrayList<LatLng>) m_PolyLines.getPoints();
}
}
prevLocation.setLatitude(locToAdd.latitude);
prevLocation.setLongitude(locToAdd.longitude);
}
}
使用此代码,我得到了所需的着色行为,但它并不流畅。我知道解决这个问题的方法是一个折线选项,但正如您将看到的那样,这并不能为我提供不同颜色所需的基本行为。这是一个放大的屏幕截图。我很难看到锯齿状,因为我只是在行走,但通常是移动车辆,所以差距更容易看到here:
一个PolyLineOptions()方法:
for(HaulEvent event:cycle){ LatLng locToAdd = new LatLng(event.Latitude,event.Longitude);
if (event.cycle_num < 2) {
m_aPL.add(mMap.addPolyline(m_PolyLines.geodesic(true)
.color(Color.BLACK)
.add(locToAdd)
.clickable(true)));
m_aPolyLineList = (ArrayList<LatLng>) m_PolyLines.getPoints();
} else { //after we've got a base cycle drawn
if (PolyUtil.isLocationOnPath(locToAdd, m_aBaseRoute, true, tolerance)) {
m_aPL.add(mMap.addPolyline(m_PolyLines.geodesic(true)
.color(Color.BLACK)
.add(new LatLng(prevLocation.getLatitude(), prevLocation.getLongitude()), locToAdd)
.clickable(true)));
m_aPolyLineList = (ArrayList<LatLng>) m_PolyLines.getPoints();
} else {
m_aPL.add(mMap.addPolyline(m_PolyLines.geodesic(true)
.color(Color.RED)
.add(new LatLng(prevLocation.getLatitude(),prevLocation.getLongitude()),locToAdd)
.clickable(true)));
m_aPolyLineList = (ArrayList<LatLng>) m_PolyLines.getPoints();
}
}
prevLocation.setLatitude(locToAdd.latitude);
prevLocation.setLongitude(locToAdd.longitude);
}
m_PolyLines是我的PolyLineOptions。通过这种方式,当我没有偏离我的基本路线时,我得到:
然后当我开始偏离我的基本路线时,我得到一堆红色折线。每次我偏离时,红色都会一直回到那个特定点,似乎没有什么特别的事情发生。
答案 0 :(得分:0)
您必须向addPolyline()提供针对googlemaps对象的折线选项。并且要更改PolylineOptions的颜色,您可以使用color()。或者要动态更改googlemaps中折线的颜色,您可以将线对象保存在折线中,然后使用setColor()在任何代码点更改折线的颜色,只要折线对象不为空即可。
要更改在谷歌地图上绘制的任何折线的颜色或要绘制,每种方法都有一种方法。
第一种情况(polylineoptions)
polylineoptions是您传递给addpolyline()以进入googlemaps的对象。所以要在这种情况下改变颜色,你必须在分配之前改变它。使用color()方法。但问题是,一旦你改变了颜色并分配了你就无法再改变它。要再次更改它,您必须清除谷歌地图并重新绘制折线。
第二种情况(polyline)
当您通过polylineoptions将addpolyline()分配给google地图时,您可以将此对象保存到polyline。然后,您可以在googlemaps上绘制后,在任何时间点更改此折线的颜色。如果要在绘制折线后动态更改颜色,这将很容易。您将使用的方法是setColor()。
我希望这能解决您的问题。
谢谢
答案 1 :(得分:0)
您的问题无法直接解决,因为您只能控制折线的几个绘图参数,但我提出了一种混合使用这两种方法的解决方法。
也就是说,addíng指向现有的折线,直到它必须改变颜色,然后开始一个新的折线。这样,只有在颜色发生变化时才会看到跳跃。
这是示例代码(未测试):
int currentColor = Color.BLACK;
for (HaulEvent event : cycle) { LatLng locToAdd = new LatLng(event.Latitude, event.Longitude);
if (event.cycle_num < 2) {
m_aPL.add(mMap.addPolyline(m_PolyLines.geodesic(true)
.color(Color.BLACK)
.add(locToAdd)
.clickable(true)));
m_aPolyLineList = (ArrayList<LatLng>) m_PolyLines.getPoints();
currentColor = Color.BLACK;
} else { //after we've got a base cycle drawn
if (PolyUtil.isLocationOnPath(locToAdd, m_aBaseRoute, true, tolerance)) {
if (currentColor == Color.BLACK) {
m_aPL.add(mMap.addPolyline(m_PolyLines.geodesic(true)
.color(Color.BLACK)
.add(new LatLng(prevLocation.getLatitude(), prevLocation.getLongitude()), locToAdd)
.clickable(true)));
} else {
m_aPL.add(mMap.addPolyline(new PolylineOptions().geodesic(true)
.color(Color.BLACK)
.add(new LatLng(prevLocation.getLatitude(), prevLocation.getLongitude()), locToAdd)
.clickable(true)));
}
currentColor = Color.BLACK;
} else {
if (currentColor == Color.RED) {
m_aPL.add(mMap.addPolyline(m_PolyLines.geodesic(true)
.color(Color.RED)
.add(new LatLng(prevLocation.getLatitude(),prevLocation.getLongitude()),locToAdd)
.clickable(true)));
} else {
m_aPL.add(mMap.addPolyline(new PolylineOptions().geodesic(true)
.color(Color.RED)
.add(new LatLng(prevLocation.getLatitude(),prevLocation.getLongitude()),locToAdd)
.clickable(true)));
}
currentColor = Color.RED;
}
}
prevLocation.setLatitude(locToAdd.latitude);
prevLocation.setLongitude(locToAdd.longitude);
}
<强>更新强>
使用https://github.com/antoniocarlon/richmaps进行改进(我是项目的所有者):
RichLayer richLayer = new RichLayer.Builder(mMap).zIndex(0).build();
RichPolylineOptions polylineOpts = new RichPolylineOptions(null)
.zIndex(0)
.strokeWidth(5)
.strokeColor(Color.BLACK)
.linearGradient(true);
RichPolyline polyline = polylineOpts.build();
richLayer.addShape(polyline);
// ...
for (HaulEvent event : cycle) {
LatLng locToAdd = new LatLng(event.Latitude, event.Longitude);
if (event.cycle_num < 2) {
polyline.add(new RichPoint(locToAdd));
} else { //after we've got a base cycle drawn
if (PolyUtil.isLocationOnPath(locToAdd, m_aBaseRoute, true, tolerance)) {
polyline.add(new RichPoint(locToAdd));
} else {
polyline.add(new RichPoint(locToAdd).color(Color.RED));
}
}
}