Google Maps API标记未在地图上显示

时间:2015-05-19 12:15:38

标签: javascript google-maps google-maps-api-3

我的Google Maps API存在问题。我有一个开发人员创建的脚本已经消失了,不会回复我哈哈

几个星期前它正在工作,当我开始测试整个网站时突然停止了工作。我无法找到它的错误。我需要有人来帮助我,因为我是Google Maps API的新手。

脚本应该找到给定地址周围的地方。

它的工作方式是:

  • 该脚本从页面中获取地址
  • 使用Googles Geocode API将地址转换为获取lang / lat值
  • 然后使用Google Places API发布lang / lat以获取地址周围的位置。

我已经调试了脚本,看起来Geocode API正常工作,当我们查询Google Places API时,没有回复任何内容。

我正在使用最新的JQuery库和以下Google API库:https://maps.googleapis.com/maps/api/js?libraries=places

请找到以下代码:

    $(document).ready(function()
    {
        address = $("#address").text();
    });

    var map;
    var infowindow;
    var map;
    var map_street;
    var service;
    var service_local_info;
    var request_school;
    var request_restaurant;
    var request_healthcare;
    var request_foodstores;
    var restaurant_markers = [];
    var school_markers = [];
    var healthcare_markers = [];
    var foodstores_markers = [];
    var latitude;
    var longitude;
    var panorama;
    var panoramaOptions;
    var schoolName;
    var trafficLayer;
    var address = "";
    var infowindow;
    var counter = 0;

    function trafic()
    {
        if($("#trafic").is(":checked"))
        {
            trafficLayer = new google.maps.TrafficLayer();
            trafficLayer.setMap(map);   
        }
        else
        {
            trafficLayer.setMap(null);
        }

    }

    function street_view() 
    {
        panorama = new  google.maps.StreetViewPanorama(document.getElementById("street-view"), panoramaOptions);
        map.setStreetView(panorama);

    }

    function local_info()
    {

        $("#distance_school").html('<div class="breadcrumb"><i class="glyphicon glyphicon-certificate"></i> &nbsp;Schools</div>');
        $("#distance_restaurant").html('<div class="breadcrumb"><i class="glyphicon glyphicon-cutlery"></i> &nbsp;Restaurants</div>');
        $("#distance_healthcare").html('<div class="breadcrumb"><i class="glyphicon glyphicon-heart"></i> &nbsp;Health Care</div>');
        $("#distance_foodstores").html('<div class="breadcrumb"><i class="glyphicon glyphicon-glass"></i> &nbsp;Food Stores</div>');
        var location_local_info;
      geocoder = new google.maps.Geocoder();
      geocoder.geocode( { 'address': address}, function(results, status) 
      {
        if (status == google.maps.GeocoderStatus.OK) 
        {
          location_local_info = results[0].geometry.location;

          latitude = location_local_info.k;
          longitude = location_local_info.D;

          map = new google.maps.Map(document.getElementById('map2'), {
                center: location_local_info,
                zoom: 15
              });

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

                request_school = {
                location: location_local_info,
                radius: 500,
                types: ["school"] 
              };
            service.nearbySearch(request_school, school_local_info_callback);

            request_restaurant = {
                    location: location_local_info,
                    radius: 500,
                    types: ["restaurant"] 
                  };
                service.nearbySearch(request_restaurant, restaurant_local_info_callback);

                request_healthcare = {
                    location: new google.maps.LatLng(latitude, longitude),
                    radius: 500,
                    types: ["health"] 
                  };
                service.nearbySearch(request_healthcare, healthcare_local_info_callback);

                request_foodstores = {
                    location: new google.maps.LatLng(latitude, longitude),
                    radius: 500,
                    types: ["food"] 
                  };
                service.nearbySearch(request_foodstores, foodstores_local_info_callback);   
            }
            else
            {
                alert("error in location");
            }

        });
    }

    function healthcare()
    {
        if($("#healthcare").is(":checked"))
        {               
            request_healthcare = {
                location: new google.maps.LatLng(latitude, longitude),
                radius: 500,
                types: ["health"]
              };
            service.nearbySearch(request_healthcare, healthcare_marker);
        }
        else
        {
            for(i=0; i<healthcare_markers.length; i++)
            {
                healthcare_markers[i].setMap(null);
            }
        }
    }

    function foodstores() 
    {
        if($("#foodstores").is(":checked"))
        {
            request_foodstores = {
                location: new google.maps.LatLng(latitude, longitude),
                radius: 500,
                types: ["food"]
              };
            service.nearbySearch(request_foodstores, foodstores_marker);
        }
        else
        {
            for(i=0; i<foodstores_markers.length; i++)
            {
                if(foodstores_markers[i])
                {
                    foodstores_markers[i].setMap(null);
                }
            }
        }
    }

    function school()
    {
        if($("#school").is(":checked"))
        {
            request_school = {
                location: new google.maps.LatLng(latitude, longitude),
                radius: 500,
                types: ["school"] //school,
              };
            service.nearbySearch(request_school, school_marker);
        }
        else
        {
            for(i=0; i<school_markers.length; i++)
            {
                school_markers[i].setMap(null);
            }
        }

    }
    function restaurant()
    {
        if($("#restaurant").is(":checked"))
        {
            request_restaurant = 
            {
                location: new google.maps.LatLng(latitude, longitude),
                radius: 500,
                types: ["restaurant"] //school,
            };
            service.nearbySearch(request_restaurant, restaurant_marker);
        }
        else
        {
            for(i=0; i<restaurant_markers.length; i++)
            {
                restaurant_markers[i].setMap(null);
            }
        }


    }

    function initialize() 
    {
        $('#restaurant').attr('checked', false);
        $('#school').attr('checked', false);
        $('#trafic').attr('checked', false);
        $('#healthcare').attr('checked', false);
        $('#foodstores').attr('checked', false);

      var location;;

      geocoder = new google.maps.Geocoder();
      geocoder.geocode( { 'address': address}, function(results, status) 
      {
        if (status == google.maps.GeocoderStatus.OK) 
        {
          location = results[0].geometry.location;
          latitude = location.k;
          longitude = location.D;

          map = new google.maps.Map(document.getElementById('map2'), {
                center: location,
                zoom: 15
              });

          map_street = new google.maps.Map(document.getElementById('street-view'), {
                center: location,
                zoom: 15
              });

          var marker = new google.maps.Marker({
            map: map,
            animation: google.maps.Animation.BOUNCE,
            //icon:"restaurant.jpg",
            position: location
          });

               service = new google.maps.places.PlacesService(map);
               panoramaOptions = {
                  position: location,
                  pov: {
                    heading: 34,
                    pitch: 10
                  }
                };
        }
        else
        {
            alert("error in location");
        }
      });

    }

    function restaurant_marker(results, status) {
      if (status == google.maps.places.PlacesServiceStatus.OK) 
      {
        for (var i = 0; i < results.length; i++) 
        {
            restaurant_markers[i] = new google.maps.Marker({
                map: map,
                //icon:"restaurant.jpg",
                position: results[i].geometry.location
              });

            google.maps.event.addListener(restaurant_markers[i], 'click', function() 
            {
                if(infowindow)
                {
                    infowindow.close();
                }
                for(var j=0; j<results.length; j++)
                {
                    if(results[j].geometry.location.k == this.position.k && results[j].geometry.location.D == this.position.D )
                    {
                        infowindow = new google.maps.InfoWindow({
                              content: "<div style='overflow-y:none;'><b>Name</b> :    "+results[j].name+"<br><b>Address</b> : "+results[j].vicinity+"</div>",
                              maxWidth: 1000,
                              maxHeight: 3000
                          });
                        infowindow.open(map,this);
                    }
                }
            });
        }
      }
    }

    function school_marker(results, status) 
    {
      if (status == google.maps.places.PlacesServiceStatus.OK) 
      { 

        for (var i = 0; i < results.length; i++) 
        {
            school_markers[i] = new google.maps.Marker({
            map: map,
            //icon:"school.png",
            position: results[i].geometry.location
          });

            google.maps.event.addListener(school_markers[i], 'click', function() 
            {
                if(infowindow)
                {
                    infowindow.close();
                }
                for(var j=0; j<results.length; j++)
                {
                    if(results[j].geometry.location.k == this.position.k && results[j].geometry.location.D == this.position.D )
                    {
                        infowindow = new google.maps.InfoWindow({
                              content: "<div style='overflow-y:none;'><b>Name</b> :    "+results[j].name+"<br><b>Address</b> : "+results[j].vicinity+"</div>",
                              maxWidth: 1000,
                              maxHeight: 3000
                          });
                        infowindow.open(map,this);
                    }
                }
            });

        }
      }
    }

    function healthcare_marker(results, status) 
    {
      if (status == google.maps.places.PlacesServiceStatus.OK) 
      { 

        for (var i = 0; i < results.length; i++) 
        {
            healthcare_markers[i] = new google.maps.Marker({
            map: map,
            //icon:"school.png",
            position: results[i].geometry.location
          });

            google.maps.event.addListener(healthcare_markers[i], 'click', function() 
            {
                if(infowindow)
                {
                    infowindow.close();
                }
                for(var j=0; j<results.length; j++)
                {
                    if(results[j].geometry.location.k == this.position.k && results[j].geometry.location.D == this.position.D )
                    {
                        infowindow = new google.maps.InfoWindow({
                              content: "<div style='overflow-y:none;'><b>Name</b> :    "+results[j].name+"<br><b>Address</b> : "+results[j].vicinity+"</div>",
                              maxWidth: 1000,
                              maxHeight: 3000
                          });
                        infowindow.open(map,this);
                    }
                }
            });

        }
      }
    }

    function foodstores_marker(results, status) 
    {
      if (status == google.maps.places.PlacesServiceStatus.OK) 
      { 

        for (var i = 0; i < results.length; i++) 
        {
            if(results[i].types.indexOf("restaurant") == -1)
            {
                foodstores_markers[i] = new google.maps.Marker({
                    map: map,
                    //icon:"school.png",
                    position: results[i].geometry.location
                  });

                google.maps.event.addListener(foodstores_markers[i], 'click', function() 
                {
                    if(infowindow)
                    {
                        infowindow.close();
                    }
                    for(var j=0; j<results.length; j++)
                    {
                        if(results[j].types.indexOf("restaurant") == -1)
                        {
                            if(results[j].geometry.location.k == this.position.k && results[j].geometry.location.D == this.position.D )
                            {
                                infowindow = new google.maps.InfoWindow({
                                      content: "<div style='overflow-y:none;'><b>Name</b> :    "+results[j].name+"<br><b>Address</b> : "+results[j].vicinity+"</div>",
                                      maxWidth: 1000,
                                      maxHeight: 3000
                                  });
                                infowindow.open(map,this);
                            }
                        }
                    }
                });
            }
        }
      }
    }

    function school_local_info_callback(results, status) 
    {

      if (status == google.maps.places.PlacesServiceStatus.OK) 
      { 
        for (var i = 0; i < results.length && i < 6; i++) 
        {
            calculate_distance_school(results[i].geometry.location, results[i].name);   
        }
      }
    }

    function restaurant_local_info_callback(results, status) 
    {

      if (status == google.maps.places.PlacesServiceStatus.OK) 
      {
        for (var i = 0; i < results.length && i < 6; i++) 
        {   
            calculate_distance_restaurant(results[i].geometry.location, results[i].name);
        }
      }
    }

    function healthcare_local_info_callback(results, status) 
    {

      if (status == google.maps.places.PlacesServiceStatus.OK) 
      {
        for (var i = 0; i < results.length && i < 6; i++) 
        {   
            calculate_distance_healthcare(results[i].geometry.location, results[i].name);
        }
      }
    }

    function foodstores_local_info_callback(results, status) 
    {

      if (status == google.maps.places.PlacesServiceStatus.OK) 
      {
        for (var i = 0; i < results.length; i++) 
        {
            if(results[i].types.indexOf("restaurant") == -1)
            {
                calculate_distance_foodstores(results[i].geometry.location, results[i].name);
                counter++;
                if(counter == 6)
                    break;
            }
        }
        counter = 0;
      }
    }

    function calculate_distance_school(origin, name) 
    {
      var service = new google.maps.DistanceMatrixService();
      service.getDistanceMatrix(
        {
          origins: [origin],
          destinations: [new google.maps.LatLng(latitude, longitude)],
          travelMode: google.maps.TravelMode.DRIVING,
          unitSystem: google.maps.UnitSystem.IMPERIAL,
          avoidHighways: false,
          avoidTolls: false
        }, function (response, status)
            {
                if (status == google.maps.DistanceMatrixStatus.OK) 
                {
                    var result = response.rows[0].elements;
                    $("#distance_school").append("<div class=\"col-xs-12 col-sm-4 col-md-4 local-body\"><h4>"+name+"</h4><p><strong>Distance :</strong> "+result[0].distance.text+"les</p></div>");
                }
                else
                {
                    alert(status);
                }
            });
    }


    function calculate_distance_restaurant(origin, name) 
    {
      var service = new google.maps.DistanceMatrixService();
      service.getDistanceMatrix(
        {
          origins: [origin],
          destinations: [new google.maps.LatLng(latitude, longitude)],
          travelMode: google.maps.TravelMode.DRIVING,
          unitSystem: google.maps.UnitSystem.IMPERIAL,
          avoidHighways: false,
          avoidTolls: false
        }, function (response, status)
            {
                if (status == google.maps.DistanceMatrixStatus.OK) 
                {
                    var result = response.rows[0].elements;
                    $("#distance_restaurant").append("<div class=\"col-xs-12 col-sm-4 col-md-4 local-body\"><h4>"+name+"</h4><p><strong>Distance :</strong> "+result[0].distance.text+"les</p></div>");
                }
                else
                {
                    alert(status);
                }
            });
    }

    function calculate_distance_healthcare(origin, name) 
    {
      var service = new google.maps.DistanceMatrixService();
      service.getDistanceMatrix(
        {
          origins: [origin],
          destinations: [new google.maps.LatLng(latitude, longitude)],
          travelMode: google.maps.TravelMode.DRIVING,
          unitSystem: google.maps.UnitSystem.IMPERIAL,
          avoidHighways: false,
          avoidTolls: false
        }, function (response, status)
            {
                if (status == google.maps.DistanceMatrixStatus.OK) 
                {
                    var result = response.rows[0].elements;
                    $("#distance_healthcare").append("<div class=\"col-xs-12 col-sm-4 col-md-4 local-body\"><h4>"+name+"</h4><p><strong>Distance :</strong> "+result[0].distance.text+"les</p></div>");
                }
                else
                {
                    alert(status);
                }
            });
    }

    function calculate_distance_foodstores(origin, name) 
    {
      var service = new google.maps.DistanceMatrixService();
      service.getDistanceMatrix(
        {
          origins: [origin],
          destinations: [new google.maps.LatLng(latitude, longitude)],
          travelMode: google.maps.TravelMode.DRIVING,
          unitSystem: google.maps.UnitSystem.IMPERIAL,
          avoidHighways: false,
          avoidTolls: false
        }, function (response, status)
            {
                if (status == google.maps.DistanceMatrixStatus.OK) 
                {
                    var result = response.rows[0].elements;
                    $("#distance_foodstores").append("<div class=\"col-xs-12 col-sm-4 col-md-4 local-body\"><h4>"+name+"</h4><p><strong>Distance :</strong> "+result[0].distance.text+"les</p></div>");
                }
                else
                {
                    alert(status);
                }
            });
    }

1 个答案:

答案 0 :(得分:0)

我没有验证,但你应该尝试每个请求使用一个服务:

service = new google.maps.places.PlacesService(map);
...
service.nearbySearch(request_school, school_local_info_callback);

service2 = new google.maps.places.PlacesService(map);
...
service2.nearbySearch(request_restaurant, restaurant_local_info_callback);