[英]How to change marker's color in maps?
我需要一些幫助來更改標記顏色,在使用android studio的android中。
我將所有標記都放在地圖上。 完善。
當用戶選擇點時,我想顯示一條消息。 為了確保安全,用戶必須在標記中單擊兩次。
一切正常,用戶做到了,我改變了顏色,標記也改變了。
但是,當我單擊另一個標記或在地圖中的任何位置時,顏色將返回原始顏色。
看來我更改了錯誤的標記(另一個對象)。
這是打印標記的方法
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());
}
}
我有一個聽眾,他會向他顯示一條帶有2個選項的消息,確認或取消:
@Override
public void onDialogPositiveClick(DialogFragment dialog) {
getSelectedPoint().setSnippet("Selected!");
getSelectedPoint().setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
}
和
@Override public void onDialogNegativeClick(DialogFragment dialog) { Toast.makeText(getBaseContext(), "Cancel the dialog", Toast.LENGTH_SHORT).show(); }
有人可以幫我嗎? 我需要顏色保持藍色,而不返回紅色。
EDIT1:當我再次單擊標記時,顏色是正確的,AZURE。 但是,在其他位置單擊時,顏色將恢復為紅色。
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();
}
}
我自己解決了。
我創建了一個Coordinate
向量作為類屬性,從構造函數和我添加到該向量的plot()
方法開始。 由於某種原因,我正在使用不同的實例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.