谷歌地图显示一些未定义的信息

时间:2014-04-15 18:12:54

标签: javascript google-maps

我有以下功能在地图和信息窗口显示标记。该窗口显示所有其他信息,但显示电话,格式化和International_formated_phonenumber是undefine。我正在使用信息窗口

将标记存储在标记数组中
var geocoder;
var map;
var markers = Array();
var infos = Array();
var icon_url;
var main_icon_url;

function initialize() {
    // prepare Geocoder
    geocoder = new google.maps.Geocoder();

    // set initial position (New York)

    var myLatlng = new google.maps.LatLng(40.7143528, -74.0059731);
    main_icon_url = document.getElementById("mainPin").value;

    var myOptions = { // default map options
        zoom: 15,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById('gmap_canvas'), myOptions);

    var address = document.getElementById("address").value;

    // script uses our 'geocoder' in order to find location by address name
    geocoder.geocode({
        'address': address
    }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) { // and, if everything is ok

            // we will center map
            var addrLocation = results[0].geometry.location;
            map.setCenter(addrLocation);

            // store current coordinates into hidden variables
            document.getElementById('lng').value = results[0].geometry.location.lng();
            document.getElementById('lat').value = results[0].geometry.location.lat();


            // and then - add new custom marker
            var addrMarker = new google.maps.Marker({
                position: addrLocation,
                map: map,
                title: results[0].formatted_address,
                icon: main_icon_url
            });
        } else {
            alert('Geocode was not successful for the following reason: ' + status);

        }
    });
    service = new google.maps.places.PlacesService(map);
}

// clear overlays function

function clearOverlays() {
    if (markers) {
        for (var i = 0; i < markers.length; i++) {
            markers[i].setMap(null);
        }
        markers = [];
        infos = [];
    }
}

// clear infos function
function clearInfos() {
    if (infos) {
        for (var i = 0; i < infos.length; i++) {
            if (infos[i].getMap()) {
                infos[i].close();
            }
        }
    }
}


// find address function
function findAddress() {
    var address = document.getElementById("address").value;

    // script uses our 'geocoder' in order to find location by address name
    geocoder.geocode({
        'address': address
    }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) { // and, if everything is ok

            // we will center map
            var addrLocation = results[0].geometry.location;
            map.setCenter(addrLocation);

            // store current coordinates into hidden variables
            document.getElementById('lat').value = results[0].geometry.location.lat();
            document.getElementById('lng').value = results[0].geometry.location.lng();

            // and then - add new custom marker
            var addrMarker = new google.maps.Marker({
                position: addrLocation,
                map: map,
                title: results[0].formatted_address,
                icon: 'marker.png'
            });
        } else {
            alert('Geocode was not successful for the following reason: ' + status);
        }
    });
    service = new google.maps.places.PlacesService(map);
}

// find custom places function
function findPlaces(place, url) {


    var radioius = 600;
    var place1 = "";
    var place2 = "";
    var palce3 = "";
    if (place == 'gas_station') {
        radioius = 2000;

    } else if (place == 'movie_theater') {
        place1 = 'zoo';
        place2 = 'night_club';
        palce3 = "bowling_alley";
    }

    // prepare variables (filter)
    icon_url = url;

    var lat = document.getElementById('lat').value;
    var lng = document.getElementById('lng').value;
    var cur_location = new google.maps.LatLng(lat, lng);

    // prepare request to Places
    var request = {
        location: cur_location,
        radius: radioius,

        types: [place, place1, place2, palce3]


    };


    // send request

    service.search(request, createMarkers);
}

// create markers (from 'findPlaces' function)
function createMarkers(results, status) {
    if (status == google.maps.places.PlacesServiceStatus.OK) {

        // if we have found something - clear map (overlays)
        clearOverlays();

        // and create new markers by search result
        for (var i = 0; i < results.length; i++) {
            createMarker(results[i]);
        }
    } else if (status == google.maps.places.PlacesServiceStatus.ZERO_RESULTS) {
        alert('Sorry, nothing is found');
    }
}

// creare single marker function
function createMarker(obj) {

    // prepare new Marker object
    var mark = new google.maps.Marker({
        position: obj.geometry.location,
        map: map,
        title: obj.name,
        icon: icon_url
    });
    markers.push(mark);

    // prepare info window
    var infowindow = new google.maps.InfoWindow({
        content: '<span style="padding: 0px; text-align:left" align="left"><h3>' + obj.name + '</h3>' + obj.vicinity + '<br />' + obj.formatted_phone_number + '<br />'
    });

    // add event handler to current marker
    google.maps.event.addListener(mark, 'click', function () {
        clearInfos();
        infowindow.open(map, mark);
    });
    infos.push(infowindow);
}

// initialization
google.maps.event.addDomListener(window, 'load', initialize);
$(document).ready(function (e) {
    $('#list-menu a').click(function () {
        $(this).addClass('active');
        $('#list-menu a').not(this).removeClass('active');
    });
});

0 个答案:

没有答案