[英]How to change marker's color in maps?
I need some help to change a marker color, in android using android studio. 我需要一些帮助来更改标记颜色,在使用android studio的android中。
I put all the markers in the map. 我将所有标记都放在地图上。 Perfect. 完善。
I would like to show a message when the user select the point. 当用户选择点时,我想显示一条消息。 To get sure, the user must click twice in the marker. 为了确保安全,用户必须在标记中单击两次。
Everything ok, the user does this, I change the color and the marker changes. 一切正常,用户做到了,我改变了颜色,标记也改变了。
BUT, when I click in another mark or whathever place in the map, the color returns the original. 但是,当我单击另一个标记或在地图中的任何位置时,颜色将返回原始颜色。
It seems that I change the wrong mark (another object). 看来我更改了错误的标记(另一个对象)。
This is the method to print the markers 这是打印标记的方法
public void plot(){
NumberFormat format = new DecimalFormat("0");
for (Coordinate c: pointsList) {
LatLng temp = new LatLng(c.getLat(), c.getLng());
Marker marker = mMap.addMarker(new MarkerOptions()
.position(temp)
.title("Ponto " + c.getDescricao())
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
marker.setTag("Ponto " + c.getDescricao());
}
}
I have a listener to him and its show a message wit 2 options, confirm or cancel: 我有一个听众,他会向他显示一条带有2个选项的消息,确认或取消:
@Override
public void onDialogPositiveClick(DialogFragment dialog) {
getSelectedPoint().setSnippet("Selected!");
getSelectedPoint().setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
}
and 和
@Override public void onDialogNegativeClick(DialogFragment dialog) { Toast.makeText(getBaseContext(), "Cancel the dialog", Toast.LENGTH_SHORT).show(); }
Somebody can help-me? 有人可以帮我吗? I Need that color stays AZURE, not returns to RED. 我需要颜色保持蓝色,而不返回红色。
EDIT1: When I click in the marker again, the color is correct, AZURE. EDIT1:当我再次单击标记时,颜色是正确的,AZURE。 But, when click in another place, the color returns to RED. 但是,在其他位置单击时,颜色将恢复为红色。
EDIT2: The code EDIT2:代码
public class Navigator extends FragmentActivity
implements FragmentoDialogo.NoticeDialogListener, OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private static Marker pontoSelecionado;
public Marker getPontoSelecionado() {
return pontoSelecionado;
}
public void setPontoSelecionado(Marker pontoSelecionado) {
this.pontoSelecionado = pontoSelecionado;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */,
this /* OnConnectionFailedListener */)
.addConnectionCallbacks(this)
.addApi(LocationServices.API)
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.build();
mGoogleApiClient.connect();
Bundle b = getIntent().getExtras();
pointsList = b.getParcelableArrayList("Points");
setContentView(R.layout.navigator_ac);
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
plotarPontosDeColeta();
plotarCaminhamento();
escutarCliqueMarcador();
// Turn on the My Location layer and the related control on the map.
updateLocationUI();
// Get the current location of the device and set the position of the map.
getDeviceLocation();
LatLng ponto1 = new LatLng(pointsList.get(1).getLat(), pointsList.get(1).getLng());
CameraPosition cameraPos = new CameraPosition.Builder().target(ponto1)
.zoom(45).bearing(20).tilt(80).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPos), null);
}
public void plotarPontosDeColeta(){
NumberFormat format = new DecimalFormat("0");
for (Coordinate c: pointsList) {
LatLng temp = new LatLng(c.getLat(), c.getLng());
Marker marker = mMap.addMarker(new MarkerOptions()
.position(temp)
.title("Ponto " + c.getDescricao())
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
marker.setTag("Ponto " + c.getDescricao());
}
}
private void plotarCaminhamento() {
NumberFormat format = new DecimalFormat("0");
ArrayList<Polyline> listaLinhas = new ArrayList<>();
final ArrayList<MarkerOptions> listaMarcadores = new ArrayList<>();
PolylineOptions lineOptions;
try {
for (int i=0; i<pointsList.size(); i++){
LatLng aux1 = new LatLng(pointsList.get(i).getLat(),pointsList.get(i).getLng());
LatLng aux2 = new LatLng(pointsList.get(i+1).getLat(),pointsList.get(i+1).getLng());
lineOptions = new PolylineOptions().clickable(true).add(aux1).add(aux2);
Polyline polyline = mMap.addPolyline(lineOptions);
polyline.setTag(i);
listaLinhas.add(polyline);
listaMarcadores.add(new MarkerOptions()
.position(PointsHandler.encontrarPontoMedio(aux1, aux2))
.alpha(0f)
.title(""+ format.format(pointsList.get(i).getDistanciaProximoPonto()) + "m"));
}
} catch (Exception e){
//TODO
}
mMap.setOnPolylineClickListener(new GoogleMap.OnPolylineClickListener(){
@Override
public void onPolylineClick(Polyline polyline)
{
int tag = (Integer) polyline.getTag();
mMap.addMarker(listaMarcadores.get(tag)).showInfoWindow();
}
});
}
public void escutarCliqueMarcador(){
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
if (getPontoSelecionado() == null) {
setPontoSelecionado(marker);
Toast.makeText(getBaseContext(), "Clique novamente para confirmar!", Toast.LENGTH_SHORT).show();
return false;
}
if (getPontoSelecionado().getTag().equals(marker.getTag())) {
showDialog();
} else {
setPontoSelecionado(marker);
Toast.makeText(getBaseContext(), "Clique novamente para confirmar!", Toast.LENGTH_SHORT).show();
}
return false;
}
});
}
public void showDialog() {
DialogFragment dialog = new FragmentoDialogo();
dialog.show(getFragmentManager(), "FragmentoDialogo");
}
@Override
public void onDialogPositiveClick(DialogFragment dialog) {
getPontoSelecionado().setSnippet("Coleta efetuada");
getPontoSelecionado().setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
getPontoSelecionado().showInfoWindow();
}
@Override
public void onDialogNegativeClick(DialogFragment dialog) {
Toast.makeText(getBaseContext(), "Ponto não confirmado", Toast.LENGTH_SHORT).show();
}
}
I solved it myself. 我自己解决了。
I created a Coordinate
vector as a class attribute, started in the constructor and in the plot()
method I add to that vector. 我创建了一个Coordinate
向量作为类属性,从构造函数和我添加到该向量的plot()
方法开始。 For some reason, I was working with different instances. 由于某种原因,我正在使用不同的实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.