gpt4 book ai didi

java - 如何更改 map 中标记的颜色?

转载 作者:行者123 更新时间:2023-11-29 02:28:40 25 4
gpt4 key购买 nike

我需要一些帮助来更改标记颜色,在 android 中使用 android studio。

我把所有的标记都放在了 map 上。完美。

我想在用户选择点时显示一条消息。为了确定,用户必须在标记中单击两次。

一切正常,用户这样做,我改变颜色和标记改变。

但是,当我点击另一个标记或 map 中的任何地方时,颜色会恢复原来的颜色。

看来我改错了标记(另一个对象)。

这是打印标记的方法

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:当我再次点击标记时,颜色是正确的,天蓝色。但是,当点击另一个地方时,颜色会变回红色。

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 vector 作为类属性,在构造函数中启动并在 plot() 方法中添加到该 vector 。出于某种原因,我正在处理不同的实例。

关于java - 如何更改 map 中标记的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50803925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com