How to set different icons on the map at the beginning and end of the route?

Asked

Viewed 73 times

0

I’m developing a tracking system and I need you to consult the route the vehicle went through. I want the initial and final point to be identified with another icon according to the image. It also follows PHP code:

    while($RS= mysql_fetch_array($RSS)){
    $x = $x + 1;
    $kx .= "new google.maps.LatLng(".str_replace(",", ".", $RS["vl_latitude"]).", ".str_replace(",", ".", $RS["vl_longitude"])."),";

    if($RS["ig"] == "1"){$ig="LIGADA";}else{$ig="DESLIGADA";}
    if($x == 1){$pt = $pt . chr(91)."'INICIO: ".date("d/m/Y H:i:s", strtotime($RS["dt_hora"]))." - ".$RS["ds_posicao"]." | Ig: ".$ig."', ".str_replace(",", ".", $RS["vl_latitude"]).", ".str_replace(",", ".", $RS["vl_longitude"]).", ".$x."],";}else{$pt = $pt . chr(91)."' ".date("d/m/Y H:i:s", strtotime($RS["dt_hora"]))." - ".$RS["ds_posicao"]." | Ig: ".$ig."', ".str_replace(",", ".", $RS["vl_latitude"]).", ".str_replace(",", ".", $RS["vl_longitude"]).", ".$x."],";}
    $adata  = $RS["dt_hora"];
    if($RS["ig"] == "1"){$aig="LIGADA";}else{$aig="DESLIGADA";}
    //$aig  = $RS["ig"];
    $apos   = $RS["ds_posicao"];
    $alat   = $RS["vl_latitude"];
    $alng   = $RS["vl_longitude"];
}
$pt = $pt . chr(91)."'FIM: ".date("d/m/Y H:i:s", strtotime($adata))." - ".$apos." | Ig: ".$aig."', ".str_replace(",", ".", $alat).", ".str_replace(",", ".", $alng).", ".$x."],";

inserir a descrição da imagem aqui

Function for creating the Marker:

function setMarkers(map, locations) {
    var image = new google.maps.MarkerImage('../../imagens/car_mini.png',
        new google.maps.Size(20, 32),
        new google.maps.Point(0, 0),
        new google.maps.Point(0, 10));
    var shape = {
        coord: [1, 1, 1, 20, 18, 20, 18, 1],
        type: 'poly'
    };

    for (var i = 0; i < locations.length; i++) {
        var beach = locations[i];
        var myLatLng = new google.maps.LatLng(beach[1], beach[2]);
        var marker = new google.maps.Marker({
            position: myLatLng,
            map: map,
            icon: image,
            shape: shape,
            title: beach[0],
            zIndex: beach[3]
        });
    }
}
  • 1

    Please remove the image from the code in Javascript and enter the code itself. This makes it easy for those who will help you.

1 answer

1


Just create inside the loop a repeat Marker different creating a condition to identify which first and last recorded coordinate. See below for what your code should look like:

var image = new google.maps.MarkerImage('../../imagens/car_mini.png',
  new google.maps.Size(20, 32),
  new google.maps.Point(0,0),
  new google.maps.Point(0, 10));

var outraImagem = new google.maps.MarkerImage('../../imagens/car.png',
  new google.maps.Size(20, 32),
  new google.maps.Point(0,0),
  new google.maps.Point(0, 10));

var shape = {
  coord: [1, 1, 1, 20, 18, 20, 18 , 1],
  type: 'poly'
};

for (var i = 0; locations.lenght; i++) {

    var beach = locations[i];
    var myLatLng = new google.maps.LatLng(beach[1], beach[2]);
    var marker = new google.maps.Marker({
        position: myLatLng,
        map: map,
        icon: image,
        shape: shape,
        title: beach[0],
        zIndex: beach[3]
    });

    // aqui é a primeira posição
    if (i == 0) {
        var marker = new google.maps.Marker({
            position: myLatLng,
            map: map,
            icon: outraImagem,
            shape: shape,
            title: beach[0],
            zIndex: beach[3]
        });
    }

    // aqui é a ultima posição
    if (i == (locations.length - 1)) {
        var marker = new google.maps.Marker({
            position: myLatLng,
            map: map,
            icon: outraImagem,
            shape: shape,
            title: beach[0],
            zIndex: beach[3]
        });
    }
}
  • I understand, and how do I declare the second image? &#xA;//imagem1 &#xA;var image = new google.maps.MarkerImage('../../imagens/car_mini.png',&#xA; new google.maps.Size(20, 32),&#xA; new google.maps.Point(0,0),&#xA; new google.maps.Point(0, 10));&#xA;&#xA;//imagem2 &#xA; var imagem = new google.maps.MarkerImage('../../images/car.png', new google.maps.Size(20, 32), new google.maps.Point(0,0), new google.maps.Point(0, 10)); ?

  • @Lucasspielmann does just below the first but with a different name. As in the example I gave: another image

  • Can you call me on Whats? Please, our will help me a lot. 49 999290123

  • @Lucasspielmann that’s what you did right there.

  • @Lucasspielmann if you have any more questions, you can ask here! = D

  • strange that is not displaying the icons in this way

  • @Lucasspielmann is showing some error?!

  • Não, simplesmente quando insiro duas vezes:&#xA; var image = new google.maps.MarkerImage('../../imagens/car_mini.png',&#xA; new google.maps.Size(20, 32),&#xA; new google.maps.Point(0,0),&#xA; new google.maps.Point(0, 10));&#xA; var imagem = new google.maps.MarkerImage('../../images/car.png', new google.maps.Size(20, 32), new google.maps.Point(0,0), new google.maps.Point(0, 10)); it does not display the icons

  • 1

    @Lucasspielmann saw that I commented on your question. Edit the question and put the code instead of the image. This makes it easier for those who are helping you. There’s a way you can do it?!

  • the javascript code is not indented, but I will post it anyway..

  • @Lucasspielmann I’m not seeing your code after the change, so it’s hard for me to tell you what’s wrong.

  • can I send your email the file? maybe it gets easier

  • @Lucasspielmann no guy. We have to settle here or in chat. But I don’t know if you have reputation to join the chat.

  • 1
Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.