2
What I need
I need that through PHP I receive locations and can mark them as points in Google Maps.
What I did
I created a part in Ajax to call the PHP script that does the query and returns the json. It receives the data. He’s adding the dots on the map, but when I click on one of them he doesn’t open the box with the information.
JS
var map;
var idInfoBoxAberto;
var infoBox = [];
var markers = [];
var localizacao = [];
//var markerPonto = new google.maps.Marker({});
var markerPonto;
var contador = 0;
var l = 0;
var contentString;
var infowindow = new google.maps.InfoWindow({
    content: contentString,
    maxWidth: 300
});
/*Método que inicia configurações iniciados do mapa*/
function initialize() {
    var latlng = new google.maps.LatLng(-23.5514565,-46.6224739);
    var options = {
        zoom: 6,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("mapa"), options);
    /*Novo parte - adiciona ponteiro geolocalizador(de acordo com as coordenadas informadas em 'latlng'*/
    geocoder = new google.maps.Geocoder();
    marker = new google.maps.Marker({
        map: map,
        draggable: true,
    });
    marker.setPosition(latlng);
    /*Parte de loop com banco de dados*/
    $.ajax({
        url : 'verificaAjax.php',
        success : function(msg){
            if (msg.status == 0) {
                msg.errorMsg.forEach(ShowResults);
                //JSON.parse(msg.errorMsg).forEach(ShowResults);
            }
        },
        error:function (xhr, ajaxOptions, thrownError) {
            alert("Erro no Processamento dos Dados. Entre em contato com o setor de Tecnologia e informe a mensagem abaixo:\n"+xhr.responseText);
        }
    });
}
// Função para retornar os valores
function ShowResults(value, index, ar) {
    contentString = '<h2>'+value['razao_social']+'</h2>';
    localizacao.push({
        nome: value['razao_social'],
        latlng: new google.maps.LatLng(value['latitude'],value['longitude'])
    });
    /*
    markerPonto.position(localizacao[l].latlng);
    markerPonto.icon('img/marcador.png');
    markerPonto.map(map);
    markerPonto.title(localizacao[l].nome);
    */
    markerPonto = new google.maps.Marker({
        position: localizacao[l].latlng,
        icon: 'img/marcador.png',
        map: map,
        title: localizacao[l].nome
    });
    google.maps.event.addListener(markerPonto, 'click', function() {
        infowindow.open(map,v);
    });
    ++l;
}
PHP
<?php
    header('Content-type: application/json;');
    require('bd.php');
    $bancoDeDados = new Bd();
    //Armazena o resultado
    $mensagemResultado = array("status" => 1, "errorMsg" => array());
    //Obtem todos os resultados de pontos de entrega
    $resultadoConsulta = $bancoDeDados->selectPontos();
    if (count($resultadoConsulta) > 0) {
        $mensagemResultado["status"] = 0;
        $mensagemResultado["errorMsg"] = array_merge($mensagemResultado["errorMsg"],$resultadoConsulta);
    }
    echo json_encode($mensagemResultado);
I think you’re forgetting to parse of this data coming from PHP. You have already tested
JSON.parse(msg.errorMsg).forEach(ShowResults);? or usetype: 'json'in the AJAX configuration?– Sergio
Do points change places over time? If they are fixed, you do not need Ajax, just generate the map source by PHP itself.
– Bacco
@Sergio I have another solution where I implemented the sending of the form data by json passing to a php (as well as in this code) and returning to JS. I even took it as a basis, and I didn’t have to do any data conversion.
– Allan Ramos
@Bacco Yes. They can be modified in the future.
– Allan Ramos