Getting database values in Javascript variable dynamically

前端 未结 3 1223
长情又很酷
长情又很酷 2021-01-27 06:03

I researched this subject many times but I could not find the right answer to my question. Let me explain it.

I\'m creating an app with the Google Maps API where I want

3条回答
  •  春和景丽
    2021-01-27 06:35

    Try:

    PHP:

    function getMarkers(){
        $response = array();
    
        foreach ($query as $post) {
    
        $response[] = array(
            'name' => $post['naam'],
            'lat' => $post['plaats'],
            'lng' => $post['categorie'],
        );
    }
    echo json_encode($response);
    }
    

    then JS:

    function addMarkers(json){
        var title =json[index]["naam"];
        var lat =json[index]["plaats"];
        var lng =json[index]["categorie"];
    
        marker = new google.maps.Marker({
            position: new google.maps.LatLng (lat, lng),
            map: YOUR_MAP
        });
    }
    
    jQuery.getJSON("URL OF PAGE",{
        format: "json",
        dataType: 'json',
        contentType: "application/json; charset=utf-8"},
        function(json){addMarkers(json);
    });
    

    better, to send the info with json and retrieve it with jQuery's getJSON, this may need some tweaking.. But I have created what your talking about. Shout if any further help is required.

    You May have to look into this a tiny bit to configure it..

    GOOD LUCK!

提交回复
热议问题