Google maps API3, polyline is not in the center and not zoomed

こ雲淡風輕ζ 提交于 2019-12-12 05:30:01

问题


I got the answer on my question of how to get polyline in the center but here is the problem. When I want to see the map. Map is only initialized in the upper left corner of my div. And when I call this function:

setTimeout(function() {
            google.maps.event.trigger(map,'resize');
                    }, 200);

I can see the map in the entire div but the polyline is not zoomed and it is inside the upper left corner.

what should I do?

My code:

Global define:

var flightPlanCoordinates=[];
var flightPath;
var map;
var myLatLng = new google.maps.LatLng(0, 0);
var mapOptions = {
              zoom: 5,
          center: myLatLng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
                                          };

Later used code...

$.ajax({
                  type: 'POST',
                  url: 'history.php',
                  data: {
                  'd_year1':$('#select-choice-year1').val(),
                  'd_month1':   $('#select-choice-month1').val(),
                  'd_day1':$('#select-choice-day1').val(),
                  'd_year2':$('#select-choice-year2').val(),
                  'd_month2':   $('#select-choice-month2').val(),
                  'd_day2':$('#select-choice-day2').val()
                  },
                  success: function(data)//callback to be executed when the response has been received
                       {
                       if(data=="[]")
                       {

                            alert("None");

                       }else
                       {

                            data = JSON.parse(data);
                            for (var i=0;i<data.length;i++)
                            {

                                flightPlanCoordinates[i] = new google.maps.LatLng(data[i].x,data[i].y); 
                            }
                            map = new google.maps.Map(document.getElementById("map_find"),
                          mapOptions);
                        flightPath = new google.maps.Polyline({
                        path: flightPlanCoordinates,
                        strokeColor: "#8a2be2",
                        strokeOpacity: 1.0,
                        strokeWeight: 3
                      });

                    flightPath.setMap(map);
                    zoomToObject(flightPath);


                        }
                        }
                });

And the function for zoom and center the polyline:

function zoomToObject(obj){
    var bounds = new google.maps.LatLngBounds();
    var points = obj.getPath().getArray();
    for (var n = 0; n < points.length ; n++){
        bounds.extend(points[n]);
    }
    map.fitBounds(bounds);
    setTimeout(function() {

                        google.maps.event.trigger(map,'resize');
                    }, 200);
}

So how to really initialize the map to fit entire div and how to zoom and center polylines to fit the map?


回答1:


Change your zoom & center function to:

function zoomToObject(obj){
    var bounds = new google.maps.LatLngBounds();
    var points = obj.getPath().getArray();
    for (var n = 0; n < points.length ; n++){
        bounds.extend(points[n]);
    }
    map.fitBounds(bounds);
    setTimeout(function() {
                google.maps.event.trigger(map,'resize');
                map.fitBounds(bounds);
                }, 200);
}

Or trigger the map resize event before you call it (inside the AJAX callback).



来源:https://stackoverflow.com/questions/13032245/google-maps-api3-polyline-is-not-in-the-center-and-not-zoomed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!