javascript - 使用 if else 语句将谷歌地图添加到网页

标签 javascript html arrays

我有一个页面网站来显示具有许多不同品牌的公司的位置。其中一些我可以在谷歌地图上找到,但其他的找不到。所以我想,如果我能找到,它会显示 map ,如果找不到,则只显示消息,不显示 map 。但我不知道我应该在哪里设置条件?

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>

<script type="text/javascript">
    var a = [
      ["Sophat",
      "<b>Sophat Phone Shop</b><br/>#179,st 274, Sangkat Veal Vong, Khan7 makara <br/>PHNOM PENH <br/>Phone: 013 212 223 ", 
       11.555956,104.91638],
      ["Narithy Chouk",
      "<b>Narithy Chouk Phopne Shop</b><br/>#AD3, Street 187, Sangkat Phsar Depo1, Khan Touk Kork<br />Phnom Penh", 
       11.5632655,104.9069364],
      ["Kompong Thom",
      "<b>Lov Srey Moch Phone Shop</b><br/>Sales & Service Agent<br />Phum PealNhek, Phreah Prey Commnune, Sampov Meas District PURSAT Province, Tel : 013 580 000 "]];

    function getMap(location1,location2, id) {
        var myLatlng = new google.maps.LatLng(location1,location2);
        var mapOptions = {
        zoom: 18,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        }
        var map = new google.maps.Map(document.getElementById(id), mapOptions);
        var marker = new google.maps.Marker({
        position: myLatlng,
        title:"qb More"
        });
        // To add the marker to the map, call setMap();
        marker.setMap(map);
    }

    $(function(){
        //$("#logo").attr("src","");
        var ul = "<ul>"; var maps = "";
        for(var i=0; i<a.length; i++){
            if(i==0) 
                ul += "<li id='" + i + "' class='active'>" + a[i][0] + "</li>";
            else
                ul += "<li id='" + i + "'>" + a[i][0] + "</li>";
        }
        ul += "</ul>";
        $("#locations").html(ul); 
        $("#maps").html('<div id="address">' + a[0][1] + '</div><div class="map_canvas" id="map_canvas' + 0 + '" style="width:97%; height:80%"></div>');
        getMap(a[0][2],a[0][3],"map_canvas" + 0);
        $("#nav #locations ul li").click(function(){
        if($(this).hasClass('active')){
        }
        else{
            $("#nav #locations ul li").removeClass('active');
            $(this).addClass('active');
            $(this).each(function(index, element) {
               var id = $(this).attr('id');
               $("#maps").html('<div id="address">' + a[id][1] + '</div><div class="map_canvas" id="map_canvas' + id + '" style="width:97%; height:80%"></div>');
               getMap(a[id][2],a[id][3],"map_canvas" + id);
            }); 
        }
        });
    });
</script>

最佳答案

在调用getMap函数之前,您可以检查该位置是否有纬度和经度if (a[id][2] !== undefined && a[id][3 ] !== 未定义)

关于javascript - 使用 if else 语句将谷歌地图添加到网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30749662/

相关文章:

html - 我需要图像水平对齐并在页面上居中

javascript - 使用 html 和 javascript 添加星标

javascript - 循环遍历数组对象并创建另一个对象jquery js

c - 警告 : assignment from incompatible pointer type by 2d pointers in the struct

javascript - 如何将缩小的 Javascript 文件部署到 Web 服务器而不更改文件的时间戳?

javascript - 将数组存储在 html data 属性中

html - 使用 HTML/CSS 创建 ARIA 角色菜单

javascript indexOf 无法按预期使用数组

javascript - 理解 JavaScript 中的 OOP

javascript - Underscore.js 模板会破坏 notepad++ 中的语法突出显示吗?