百度地圖javascript API的使用

  最近作本身的網站,調用百度地圖的js API,在頁面上顯示一些座標點。遇到一個問題,就是當座標點超過10個時,convertor.translate(tempPointArr, 1, 5, translateCallback)執行完後,在translateCallback中數據的狀態不是0,應該就是GPS經緯度轉換沒有完成,致使座標點不會被添加到地圖上顯示。不知道緣由所在,看API文檔應該能找到答案,無奈裝的艾普寬帶,如今百度地圖的js API網頁打不開。把座標轉換分紅10個一組來作,能夠解決這個問題。函數

               var groups = 0;
                        //座標轉換完以後的回調函數
                        translateCallback = function (data){
                          if(data.status === 0) {
                            for (var i = 0; i < data.points.length; i++) {
                                var marker = new BMap.Marker(data.points[i]);
                                var label = new BMap.Label(groups * 10 + i,{offset:new BMap.Size(20,-10)});
                                marker.setLabel(label); //添加百度label
                                map.addOverlay(marker);
                            }
                            groups++;
                            map.setCenter(data.points[i-1]);
                            //alert("the center is the: " + (i - 1) + "th point");
                          }
                          else {
                            alert("data status is not 0");
                          }
                        }
                        setTimeout(function(){
                            var convertor = new BMap.Convertor();
                            var n = 10;
                            alert("the totall number of points is : " + pointArr.length);
                            var remain = pointArr.length % 10;
                            var rounds = (pointArr.length - remain) / 10;
                            alert(rounds + " groups of points and " + remain + " points to translate");
                            var tempPointArr = [];
                            var index = 0;
                            for(var r = 0; r < rounds; r++) {
                                tempPointArr = [];
                                for(var i = 0; i < n; i++) {
                                    alert("the index of point: " + index);
                                    tempPointArr.push(pointArr[index]);
                                    index++;
                                }
                                convertor.translate(tempPointArr, 1, 5, translateCallback);
                            }
                            tempPointArr = [];
                            for(var i = 0; i < remain; i++) {
                                alert("the index of point: " + index);
                                tempPointArr.push(pointArr[index]);
                                index++;
                            }
                            convertor.translate(tempPointArr, 1, 5, translateCallback);
                        }, 1000);
相關文章
相關標籤/搜索