作为ID和show infowinow的URL参数在Googlemaps上具有此ID

时间:2015-05-05 07:00:15

标签: javascript google-maps-api-3

此js代码用于显示经过切割的Google地图。 我想为这段代码添加功能。 作为ID和show infowinow的URL参数在Googlemaps上具有此ID。 例如“http://www.tabikobo.com/newyork/map/uptown/?=2962”(此ID为2966).ID在XMLdata中作为“id”加载。

var uptownMap,
defLng = -73.963245,
defLat = 40.779438,
san ={

    init :
    function(){
        $(document).ready(function(){

            var script = document.createElement("script");
            script.type = "text/javascript";
            script.src = "http://maps.googleapis.com/maps/api/js?sensor=false&callback=san.putGmap";
            document.body.appendChild(script);
        });


    },


    putGmap : 

    function(){

        var myLatlng = new google.maps.LatLng(defLat, defLng);
        var myOptions = {
            zoom: 15,
                            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        uptownMap = new google.maps.Map(document.getElementById("gmapArea"), myOptions);
        san.getXmlData();
    },
    getXmlData : function(){
        $.ajax({
            type: 'GET',
            cache: true,
            async: true,
            url: '/newyork/map_uptown_xml/',
            datatype: 'xml',
            success: san.parseXmlData
        });
    },
        parseXmlData: function (xml) {

        //a single infowindow for all markers
        var infowindow = new google.maps.InfoWindow({
            pixelOffset: new google.maps.Size(0, 10)
        });

        var i = 0,
            id, name, url, lat, lng, copy, lead, ename, tag;

        $("<ol/>")
            .attr('id', 'gmapAnchor')
            .appendTo('div#gmapController');
        $(xml)
            .find('item')
            .each(function (i) {
                i++;
                id = $(this)
                    .find('description id')
                    .text();
                name = $(this)
                    .find('description name')
                    .text();
                url = $(this)
                    .find('description path')
                    .text();
                lat = $(this)
                    .find('description lat')
                    .text();
                lng = $(this)
                    .find('description lng')
                    .text();
                tag = $(this)
                    .find('description tag')
                    .text();
                tag = tag.slice(5, 20);
                console.log(tag)


                var customIcons = {
                    hotel:
                    {
                        icon: 'http://www.tabikobo.com/newyork/img/icon_hotel.png'
                    },

                var icon = customIcons[tag] || {};



                var myLatLng = new google.maps.LatLng(lat, lng);
                var beachMarker = new google.maps.Marker({
                    position: myLatLng,
                    map: uptownMap,
                    icon: icon.icon,
                });


                var htmlTmpl = {
                    hotel:
                    {
                        content: '<div class="infoWinWrapper"><strong><a href="' +
                            url + '">' + name +
                            '</a></strong><br />' + ename +
                            '<br />' + lead + '</div>'
                    },
                };


                var htmlTmpl = htmlTmpl[tag] || {};



                //click-listener for marker
                google.maps.event.addListener(beachMarker, 'click',
                    function () {
                        //update the content
                        infowindow.setContent(
                            '<div class="hook">' + htmlTmpl
                            .content + '</div>');
                        infowindow.open(uptownMap, this);
                        google.maps.event.addListenerOnce(
                            infowindow, 'domready',
                            function () {
                                var l = $('.hook')
                                    .parent()
                                    .parent()
                                    .parent()
                                    .siblings()
                                    .addClass("infoBox");

                            });

                    });

                //Creat a Tag
                san.putData(name, url, lat, lng, i);
            });
    },
    putData : function(name, url, lat, lng, num){
        var x = num;
        x += '';
        if(x.length == 1){
            var y = '0' + x;
        }else {
            y = x;
        }


        san.setEvent();
    },
    setEvent : function(){
        $("ul#area_list li a").bind('mouseover', function(){
            $(this).parent().find('span.lat').text();

            var point = new google.maps.LatLng(
                $(this).parent().find('span.lat').text(), 
                $(this).parent().find('span.lng').text()
            );

            uptownMap.setZoom(17);
            uptownMap.setCenter(point);

        });
        $("#btnResetZoom a").bind('click', function(){
            var point = new google.maps.LatLng(defLat, defLng);
            uptownMap.setZoom(15);
            uptownMap.setCenter(point);
            return false;
        });
    }

}


window.onload = san.init();

0 个答案:

没有答案