Customizing markers based on variable in javascript












-1















I am trying to create a different markers based on a particular variable. There are three coordinates on my map, with and info window. Each coordinate has a different "Report type" (donation, request,disaster). I am trying to customize each coordinate to have a different marker based on the report type, but I am only able to create the same marker for all coordinates. Map



var map;
var infowindow = new google.maps.InfoWindow();

function initialization() {
showAllReports();
}

function showAllReports() {
$.ajax({
url: 'HttpServlet',
type: 'POST',
data: { "tab_id": "1"},
success: function(reports) {
mapInitialization(reports);
},
error: function(xhr, status, error) {
alert("An AJAX error occured: " + status + "nError: " + error);
}
});
}

function mapInitialization(reports) {
var mapOptions = {
mapTypeId : google.maps.MapTypeId.ROADMAP, // Set the type of Map
};

// Render the map within the empty div
map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);

var bounds = new google.maps.LatLngBounds ();

$.each(reports, function(i, e) {
var long = Number(e['longitude']);
var lat = Number(e['latitude']);
var latlng = new google.maps.LatLng(lat, long);

bounds.extend(latlng);

// Create the infoWindow content
var contentStr = '<h4>Report Details</h4><hr>';
contentStr += '<p><b>' + 'Disaster' + ':</b>&nbsp' + e['disaster'] + '</p>';
contentStr += '<p><b>' + 'Report Type' + ':</b>&nbsp' + e['report_type'] +
'</p>';
if (e['report_type'] == 'request' || e['report_type'] == 'donation') {
contentStr += '<p><b>' + 'Resource Type' + ':</b>&nbsp' +
e['resource_type'] + '</p>';
}
else if (e['report_type'] == 'damage') {
contentStr += '<p><b>' + 'Damage Type' + ':</b>&nbsp' + e['damage_type']
+ '</p>';
}
contentStr += '<p><b>' + 'Reporter' + ':</b>&nbsp' +
e['reporter_id'] + '</p>';

contentStr += '<p><b>' + 'Timestamp' + ':</b>&nbsp' +
e['time_stamp'].substring(0,19) + '</p>';
if ('message' in e){
contentStr += '<p><b>' + 'Message' + ':</b>&nbsp' + e['message'] + '</p>';
}

var icon = {
url: 'https://1adn3cp4l2-flywheel.netdna-ssl.com/wp-content/uploads/2013/10/Donation-plugins-for-WordPress1.jpg', // url
scaledSize: new google.maps.Size(40, 30), // scaled size
origin: new google.maps.Point(0, 0), // origin
anchor: new google.maps.Point(0, 0) // anchor
};

var marker = new google.maps.Marker({
position: latlng,
map: map,
icon: icon,
customInfo: contentStr,

});


// Add a Click Listener to the marker
google.maps.event.addListener(marker, 'click', function() {
// use 'customInfo' to customize infoWindow
infowindow.setContent(marker['customInfo']);
infowindow.open(map, marker); // Open InfoWindow
});

});

map.fitBounds (bounds);

}


//Execute our 'initialization' function once the page has loaded.
google.maps.event.addDomListener(window, 'load', initialization);









share|improve this question

























  • @ScaryWombat , this is Javascript.

    – Deepak
    Nov 19 '18 at 7:52






  • 1





    change the marker image icon based on report_type.

    – Shiv Kumar Baghel
    Nov 19 '18 at 7:54


















-1















I am trying to create a different markers based on a particular variable. There are three coordinates on my map, with and info window. Each coordinate has a different "Report type" (donation, request,disaster). I am trying to customize each coordinate to have a different marker based on the report type, but I am only able to create the same marker for all coordinates. Map



var map;
var infowindow = new google.maps.InfoWindow();

function initialization() {
showAllReports();
}

function showAllReports() {
$.ajax({
url: 'HttpServlet',
type: 'POST',
data: { "tab_id": "1"},
success: function(reports) {
mapInitialization(reports);
},
error: function(xhr, status, error) {
alert("An AJAX error occured: " + status + "nError: " + error);
}
});
}

function mapInitialization(reports) {
var mapOptions = {
mapTypeId : google.maps.MapTypeId.ROADMAP, // Set the type of Map
};

// Render the map within the empty div
map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);

var bounds = new google.maps.LatLngBounds ();

$.each(reports, function(i, e) {
var long = Number(e['longitude']);
var lat = Number(e['latitude']);
var latlng = new google.maps.LatLng(lat, long);

bounds.extend(latlng);

// Create the infoWindow content
var contentStr = '<h4>Report Details</h4><hr>';
contentStr += '<p><b>' + 'Disaster' + ':</b>&nbsp' + e['disaster'] + '</p>';
contentStr += '<p><b>' + 'Report Type' + ':</b>&nbsp' + e['report_type'] +
'</p>';
if (e['report_type'] == 'request' || e['report_type'] == 'donation') {
contentStr += '<p><b>' + 'Resource Type' + ':</b>&nbsp' +
e['resource_type'] + '</p>';
}
else if (e['report_type'] == 'damage') {
contentStr += '<p><b>' + 'Damage Type' + ':</b>&nbsp' + e['damage_type']
+ '</p>';
}
contentStr += '<p><b>' + 'Reporter' + ':</b>&nbsp' +
e['reporter_id'] + '</p>';

contentStr += '<p><b>' + 'Timestamp' + ':</b>&nbsp' +
e['time_stamp'].substring(0,19) + '</p>';
if ('message' in e){
contentStr += '<p><b>' + 'Message' + ':</b>&nbsp' + e['message'] + '</p>';
}

var icon = {
url: 'https://1adn3cp4l2-flywheel.netdna-ssl.com/wp-content/uploads/2013/10/Donation-plugins-for-WordPress1.jpg', // url
scaledSize: new google.maps.Size(40, 30), // scaled size
origin: new google.maps.Point(0, 0), // origin
anchor: new google.maps.Point(0, 0) // anchor
};

var marker = new google.maps.Marker({
position: latlng,
map: map,
icon: icon,
customInfo: contentStr,

});


// Add a Click Listener to the marker
google.maps.event.addListener(marker, 'click', function() {
// use 'customInfo' to customize infoWindow
infowindow.setContent(marker['customInfo']);
infowindow.open(map, marker); // Open InfoWindow
});

});

map.fitBounds (bounds);

}


//Execute our 'initialization' function once the page has loaded.
google.maps.event.addDomListener(window, 'load', initialization);









share|improve this question

























  • @ScaryWombat , this is Javascript.

    – Deepak
    Nov 19 '18 at 7:52






  • 1





    change the marker image icon based on report_type.

    – Shiv Kumar Baghel
    Nov 19 '18 at 7:54
















-1












-1








-1








I am trying to create a different markers based on a particular variable. There are three coordinates on my map, with and info window. Each coordinate has a different "Report type" (donation, request,disaster). I am trying to customize each coordinate to have a different marker based on the report type, but I am only able to create the same marker for all coordinates. Map



var map;
var infowindow = new google.maps.InfoWindow();

function initialization() {
showAllReports();
}

function showAllReports() {
$.ajax({
url: 'HttpServlet',
type: 'POST',
data: { "tab_id": "1"},
success: function(reports) {
mapInitialization(reports);
},
error: function(xhr, status, error) {
alert("An AJAX error occured: " + status + "nError: " + error);
}
});
}

function mapInitialization(reports) {
var mapOptions = {
mapTypeId : google.maps.MapTypeId.ROADMAP, // Set the type of Map
};

// Render the map within the empty div
map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);

var bounds = new google.maps.LatLngBounds ();

$.each(reports, function(i, e) {
var long = Number(e['longitude']);
var lat = Number(e['latitude']);
var latlng = new google.maps.LatLng(lat, long);

bounds.extend(latlng);

// Create the infoWindow content
var contentStr = '<h4>Report Details</h4><hr>';
contentStr += '<p><b>' + 'Disaster' + ':</b>&nbsp' + e['disaster'] + '</p>';
contentStr += '<p><b>' + 'Report Type' + ':</b>&nbsp' + e['report_type'] +
'</p>';
if (e['report_type'] == 'request' || e['report_type'] == 'donation') {
contentStr += '<p><b>' + 'Resource Type' + ':</b>&nbsp' +
e['resource_type'] + '</p>';
}
else if (e['report_type'] == 'damage') {
contentStr += '<p><b>' + 'Damage Type' + ':</b>&nbsp' + e['damage_type']
+ '</p>';
}
contentStr += '<p><b>' + 'Reporter' + ':</b>&nbsp' +
e['reporter_id'] + '</p>';

contentStr += '<p><b>' + 'Timestamp' + ':</b>&nbsp' +
e['time_stamp'].substring(0,19) + '</p>';
if ('message' in e){
contentStr += '<p><b>' + 'Message' + ':</b>&nbsp' + e['message'] + '</p>';
}

var icon = {
url: 'https://1adn3cp4l2-flywheel.netdna-ssl.com/wp-content/uploads/2013/10/Donation-plugins-for-WordPress1.jpg', // url
scaledSize: new google.maps.Size(40, 30), // scaled size
origin: new google.maps.Point(0, 0), // origin
anchor: new google.maps.Point(0, 0) // anchor
};

var marker = new google.maps.Marker({
position: latlng,
map: map,
icon: icon,
customInfo: contentStr,

});


// Add a Click Listener to the marker
google.maps.event.addListener(marker, 'click', function() {
// use 'customInfo' to customize infoWindow
infowindow.setContent(marker['customInfo']);
infowindow.open(map, marker); // Open InfoWindow
});

});

map.fitBounds (bounds);

}


//Execute our 'initialization' function once the page has loaded.
google.maps.event.addDomListener(window, 'load', initialization);









share|improve this question
















I am trying to create a different markers based on a particular variable. There are three coordinates on my map, with and info window. Each coordinate has a different "Report type" (donation, request,disaster). I am trying to customize each coordinate to have a different marker based on the report type, but I am only able to create the same marker for all coordinates. Map



var map;
var infowindow = new google.maps.InfoWindow();

function initialization() {
showAllReports();
}

function showAllReports() {
$.ajax({
url: 'HttpServlet',
type: 'POST',
data: { "tab_id": "1"},
success: function(reports) {
mapInitialization(reports);
},
error: function(xhr, status, error) {
alert("An AJAX error occured: " + status + "nError: " + error);
}
});
}

function mapInitialization(reports) {
var mapOptions = {
mapTypeId : google.maps.MapTypeId.ROADMAP, // Set the type of Map
};

// Render the map within the empty div
map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);

var bounds = new google.maps.LatLngBounds ();

$.each(reports, function(i, e) {
var long = Number(e['longitude']);
var lat = Number(e['latitude']);
var latlng = new google.maps.LatLng(lat, long);

bounds.extend(latlng);

// Create the infoWindow content
var contentStr = '<h4>Report Details</h4><hr>';
contentStr += '<p><b>' + 'Disaster' + ':</b>&nbsp' + e['disaster'] + '</p>';
contentStr += '<p><b>' + 'Report Type' + ':</b>&nbsp' + e['report_type'] +
'</p>';
if (e['report_type'] == 'request' || e['report_type'] == 'donation') {
contentStr += '<p><b>' + 'Resource Type' + ':</b>&nbsp' +
e['resource_type'] + '</p>';
}
else if (e['report_type'] == 'damage') {
contentStr += '<p><b>' + 'Damage Type' + ':</b>&nbsp' + e['damage_type']
+ '</p>';
}
contentStr += '<p><b>' + 'Reporter' + ':</b>&nbsp' +
e['reporter_id'] + '</p>';

contentStr += '<p><b>' + 'Timestamp' + ':</b>&nbsp' +
e['time_stamp'].substring(0,19) + '</p>';
if ('message' in e){
contentStr += '<p><b>' + 'Message' + ':</b>&nbsp' + e['message'] + '</p>';
}

var icon = {
url: 'https://1adn3cp4l2-flywheel.netdna-ssl.com/wp-content/uploads/2013/10/Donation-plugins-for-WordPress1.jpg', // url
scaledSize: new google.maps.Size(40, 30), // scaled size
origin: new google.maps.Point(0, 0), // origin
anchor: new google.maps.Point(0, 0) // anchor
};

var marker = new google.maps.Marker({
position: latlng,
map: map,
icon: icon,
customInfo: contentStr,

});


// Add a Click Listener to the marker
google.maps.event.addListener(marker, 'click', function() {
// use 'customInfo' to customize infoWindow
infowindow.setContent(marker['customInfo']);
infowindow.open(map, marker); // Open InfoWindow
});

});

map.fitBounds (bounds);

}


//Execute our 'initialization' function once the page has loaded.
google.maps.event.addDomListener(window, 'load', initialization);






javascript google-maps markers






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 7:56







mreyes0211

















asked Nov 19 '18 at 7:40









mreyes0211mreyes0211

63




63













  • @ScaryWombat , this is Javascript.

    – Deepak
    Nov 19 '18 at 7:52






  • 1





    change the marker image icon based on report_type.

    – Shiv Kumar Baghel
    Nov 19 '18 at 7:54





















  • @ScaryWombat , this is Javascript.

    – Deepak
    Nov 19 '18 at 7:52






  • 1





    change the marker image icon based on report_type.

    – Shiv Kumar Baghel
    Nov 19 '18 at 7:54



















@ScaryWombat , this is Javascript.

– Deepak
Nov 19 '18 at 7:52





@ScaryWombat , this is Javascript.

– Deepak
Nov 19 '18 at 7:52




1




1





change the marker image icon based on report_type.

– Shiv Kumar Baghel
Nov 19 '18 at 7:54







change the marker image icon based on report_type.

– Shiv Kumar Baghel
Nov 19 '18 at 7:54














1 Answer
1






active

oldest

votes


















0














change the marker image icon based on report_type.



below is an example for the same.



var icon_img = '';

if(report_type === 'Donation') {
icon_img = 'https://1adn3cp4l2-flywheel.netdna-ssl.com/wp-content/uploads/2013/10/Donation-plugins-for-WordPress1.jpg';
}else if(report_type === 'Disaster'){
icon_img = 'Disaster.jpg';
}else if(report_type === 'damage'){
icon_img = 'damage.jpg';
}


var icon = {
url: icon_img, // url
scaledSize: new google.maps.Size(40, 30), // scaled size
origin: new google.maps.Point(0, 0), // origin
anchor: new google.maps.Point(0, 0) // anchor
};





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370240%2fcustomizing-markers-based-on-variable-in-javascript%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    change the marker image icon based on report_type.



    below is an example for the same.



    var icon_img = '';

    if(report_type === 'Donation') {
    icon_img = 'https://1adn3cp4l2-flywheel.netdna-ssl.com/wp-content/uploads/2013/10/Donation-plugins-for-WordPress1.jpg';
    }else if(report_type === 'Disaster'){
    icon_img = 'Disaster.jpg';
    }else if(report_type === 'damage'){
    icon_img = 'damage.jpg';
    }


    var icon = {
    url: icon_img, // url
    scaledSize: new google.maps.Size(40, 30), // scaled size
    origin: new google.maps.Point(0, 0), // origin
    anchor: new google.maps.Point(0, 0) // anchor
    };





    share|improve this answer




























      0














      change the marker image icon based on report_type.



      below is an example for the same.



      var icon_img = '';

      if(report_type === 'Donation') {
      icon_img = 'https://1adn3cp4l2-flywheel.netdna-ssl.com/wp-content/uploads/2013/10/Donation-plugins-for-WordPress1.jpg';
      }else if(report_type === 'Disaster'){
      icon_img = 'Disaster.jpg';
      }else if(report_type === 'damage'){
      icon_img = 'damage.jpg';
      }


      var icon = {
      url: icon_img, // url
      scaledSize: new google.maps.Size(40, 30), // scaled size
      origin: new google.maps.Point(0, 0), // origin
      anchor: new google.maps.Point(0, 0) // anchor
      };





      share|improve this answer


























        0












        0








        0







        change the marker image icon based on report_type.



        below is an example for the same.



        var icon_img = '';

        if(report_type === 'Donation') {
        icon_img = 'https://1adn3cp4l2-flywheel.netdna-ssl.com/wp-content/uploads/2013/10/Donation-plugins-for-WordPress1.jpg';
        }else if(report_type === 'Disaster'){
        icon_img = 'Disaster.jpg';
        }else if(report_type === 'damage'){
        icon_img = 'damage.jpg';
        }


        var icon = {
        url: icon_img, // url
        scaledSize: new google.maps.Size(40, 30), // scaled size
        origin: new google.maps.Point(0, 0), // origin
        anchor: new google.maps.Point(0, 0) // anchor
        };





        share|improve this answer













        change the marker image icon based on report_type.



        below is an example for the same.



        var icon_img = '';

        if(report_type === 'Donation') {
        icon_img = 'https://1adn3cp4l2-flywheel.netdna-ssl.com/wp-content/uploads/2013/10/Donation-plugins-for-WordPress1.jpg';
        }else if(report_type === 'Disaster'){
        icon_img = 'Disaster.jpg';
        }else if(report_type === 'damage'){
        icon_img = 'damage.jpg';
        }


        var icon = {
        url: icon_img, // url
        scaledSize: new google.maps.Size(40, 30), // scaled size
        origin: new google.maps.Point(0, 0), // origin
        anchor: new google.maps.Point(0, 0) // anchor
        };






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 8:12









        Shiv Kumar BaghelShiv Kumar Baghel

        1,3763820




        1,3763820
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370240%2fcustomizing-markers-based-on-variable-in-javascript%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini