没有在谷歌地图中正确获得标记A和B.

时间:2013-10-10 02:14:47

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

我已经制作了一个应用程序,在google-maps中打印多个折线,每一行都以标记A作为起点,最后会有一个标记B,如 JSFIDDLE 。问题是,当我打印10-15行时,标记丢失,特别是标记B。

任何人都可以告诉我一些解决方案吗

查看我的 JSFIDDLE

我的代码如下所示

$(function(){

 var map    = new google.maps.Map(document.getElementById("map"), {
                  center: new google.maps.LatLng(11.275387916698238, 75.8015380957031),
                  zoom: 12
               }),



     routes = [
         {"origin":"mavoor road, kozhikode","destination":"p t usha road, kozhikode","status":"START"},
         {"origin":"vellimadukunnu, kozhikode","destination":"p t usha road, kozhikode","status":"START"}
         ,{"origin":"p t usha road, kozhikode","destination":"mavoor road, kozhikode","status":"START"},
         {"origin":"p t usha road, kozhikode","destination":"mankavu, kozhikode","status":"START"},
         {"origin":"p t usha road, kozhikode","destination":"ghandhi road, kozhikode","status":"START"},
         {"origin":"p t usha road, kozhikode","destination":"ghandhi road, kozhikode","status":"START"},
         {"origin":"p t usha road, kozhikode, Kerala, India","destination":"francis road, kozhikode, Kerala, India","status":"START"},
         {"origin":"p t usha road, kozhikode, Kerala, India","destination":"francis road, kozhikode, Kerala, India","status":"START"},
         {"origin":"p t usha road, kozhikode","destination":"cooperative hospital, eranjipalam, kozhikode","status":"START"},
         {"origin":"p t usha road, kozhikode","destination":"beach, kozhikode","status":"START"},
         {"origin":"p t usha road, kozhikode","destination":"azchavattam, kozhikode","status":"START"},
         {"origin":"p t usha road, kozhikode","destination":"aradhana tourist home, kallai road, kozhikode","status":"START"},
         {"origin":"IIM, Kozhikode","destination":"VELLIMADUKUNNU, KOZHIKODE","status":"START"},
         {"origin":"MANKAVU, KOZHIKODE","destination":"P T USHA ROAD, KOZHIKODE","status":"START"},
         {"origin":"P T Usha road, kozhikode, Kerala, India","destination":"Mavoor road, kozhikode, Kerala, India","status":"START"},
         {"origin":"P T Usha Road, Kozhikode","destination":"Mankavu, Kozhikode","status":"START"},
         {"origin":"P T USHA ROAD, KOZHIKODE","destination":"GANDHI ROAD, KOZHIKODE","status":"START"},
         {"origin":"P t usha road, kozhikide, kerala India","destination":"Francis road, kozhikide, kerala India","status":"START"},
         {"origin":"P T Usha Road, Kozhikode, Kerala, India","destination":"Francis Road, Kozhikode, Kerala, India","status":"START"},
         {"origin":"P T Usha Road, Kozhikode, Kerala, India","destination":"Francis Road, Kozhikode, Kerala, India","status":"START"}],

     rendererOptions = {
                preserveViewport: true,
                map:map,
                polylineOptions:{strokeColor:'#FF3300',
                                 strokeWeight: 5},        
                suppressMarkers:true,
                routeIndex:0
              },
     directionsService = new google.maps.DirectionsService();
    var i=0;
var infowindow = new google.maps.InfoWindow();
      $.each(routes,
        function(i,obj){//<--anonymous function

        var request = {
                origin: obj.origin,
                destination: obj.destination,
                travelMode: google.maps.TravelMode.DRIVING
              },

            directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
            directionsService.route(request, function(result, status) {

              if (status == google.maps.DirectionsStatus.OK) {

var lat = result.routes[0].legs[0].start_location.lat();
var lon = result.routes[0].legs[0].start_location.lng();

    var lat1 = result.routes[0].legs[0].end_location.lat();
var lon1 = result.routes[0].legs[0].end_location.lng();             



                  try{  

                  var marker1 = new google.maps.Marker({
    position: new google.maps.LatLng(lat, lon),
                      icon:'https://mts.googleapis.com/vt/icon/name=icons/spotlight/spotlight-waypoint-b.png&text=B&psize=16&font=fonts/Roboto-Regular.ttf&color=ff333333&ax=44&ay=48&scale=1',
        map: map
      });

                            google.maps.event.addListener(marker1, 'click', (function(marker1, i) {
        return function() {
          infowindow.setContent('hh');
          infowindow.open(map, marker1);
        }
      })(marker1, i));                      

                  }catch(e){alert(e)}






                       try{  

                  var marker2 = new google.maps.Marker({
    position: new google.maps.LatLng(lat1, lon1),
                      icon:'https://mts.googleapis.com/vt/icon/name=icons/spotlight/spotlight-waypoint-a.png&text=A&psize=16&font=fonts/Roboto-Regular.ttf&color=ff333333&ax=44&ay=48&scale=1',
        map: map
      });



                            google.maps.event.addListener(marker2, 'click', (function(marker2, i) {
        return function() {
          infowindow.setContent('hdddh');
          infowindow.open(map, marker2);
        }
      })(marker2, i));                                     

                  }catch(e){alert(e)}




                  directionsDisplay.setDirections(result);
              }
            });  
      i++;
        });});

1 个答案:

答案 0 :(得分:0)

缺少的标记位于其他标记之下(我在这个fiddle中使它们可拖动。另请注意,您正在进入配额/速率限制(获得OVER_QUERY_LIMIT的状态响应)。

var marker1 = new google.maps.Marker({
  position: new google.maps.LatLng(lat, lon),
  draggable: true,
  icon:'https://mts.googleapis.com/vt/icon/name=icons/spotlight/spotlight-waypoint-b.png&text=B&psize=16&font=fonts/Roboto-Regular.ttf&color=ff333333&ax=44&ay=48&scale=1',
  map: map
});

var marker2 = new google.maps.Marker({
  position: new google.maps.LatLng(lat1, lon1),
  draggable: true,
  icon:'https://mts.googleapis.com/vt/icon/name=icons/spotlight/spotlight-waypoint-a.png&text=A&psize=16&font=fonts/Roboto-Regular.ttf&color=ff333333&ax=44&ay=48&scale=1',
  map: map
});

这是一个有用的更改(因此您可以看到错误发生的时间):

  directionsDisplay.setDirections(result);
} else { 
  // output error status messages
  document.getElementById('info').innerHTML += "Directions Request["+i+"] Failed: "+status + "<br>"; 
}