2012-09-27 97 views
0

我想使用谷歌地圖作爲查明位置的工具。爲此,我使用了javaScript api v3。我也使用ipinfodb api進行地理定位。我跑進我susspect是一個API的問題一個問題,但莫比我忘了什麼東西...... 這裏是我的代碼的一部分:谷歌地圖API 3 MouseEvent latLng

var googleMap = null; // of type google.maps.Map 
var googleApiKey = <myGoogleApiKey>; 
var scriptLoaded = false; 
var latLng = null; // expecting format: "lat,lng" or "(lat,lng)" 
var marker = null; // of type google.maps.Marker 

function setLatLong(locationStr){ 
    if (null != locationStr && locationStr.length>2){ 
     latLng = ""+locationStr; 
    if (null != googleMap){ 
      var ll = latLng.split(","); 
      ll[0] = ll[0].replace(/\(/i,""); 
      ll[1] = ll[1].replace(/\)/i,""); 
      //TODO: write ll[0], and ll[1] somewhere 
     } 
    } 
} 

function loadGoogleMapsScript() { 
    if (!scriptLoaded){ 
     var script = document.createElement("script"); 
     script.type = "text/javascript"; 
     script.src = "https://maps.googleapis.com/maps/api/js?key="+ 
        googleApiKey+"&sensor=false&callback=initGoogleMaps"; 
     document.body.appendChild(script); 
     scriptLoaded = true; 
    } 
} 

function initGoogleMaps() { 
var llArray = null; 
if (null != latLng) llArray = latLng.split(","); 
else llArray = [0,0]; 

if (null == googleMap){ 
     var mapOptions = { 
     zoom: 13, 
     center: new google.maps.LatLng((llArray && llArray.length == 2)? parseFloat(llArray[0]) : null, 
             (llArray && llArray.length == 2)? parseFloat(llArray[1]) : null), 
     mapTypeId: google.maps.MapTypeId.ROADMAP, 
     draggable:true 
     }; 
     googleMap = new google.maps.Map(document.getElementById("theMapId"), mapOptions); 
     google.maps.event.addListener(googleMap, 'click', function(event) { 
      placeMarker(event.latLng); 
     setTimeout('placeMarker(googleMap.getCenter())',500); 
} else{ 
     googleMap.setZoom(13); 
     googleMap.setCenter((llArray && llArray.length == 2)? parseFloat(llArray[0]) : null, 
          (llArray && llArray.length == 2)? parseFloat(llArray[1]) : null); 
} 
} 

function placeMarker(location) { 
    if (!isNaN(location.lat()) && !isNaN(location.lng())) 
     setLatLong("("+location.lat()+","+location.lng()+")"); 
    else return null; 
    if (marker){ 
     marker.setMap(null); 
     marker.setMap(googleMap); 
     marker.setPosition(location); 
    } else{ 
     marker = new google.maps.Marker({ 
      position: location, 
      map:googleMap, 
      clickable:false, 
      draggable:true 
     }); 
     google.maps.event.addListener(marker, 'dragend', function(event) { 
      placeMarker(event.latLng); 
     }); 
    } 
} 

我開始loadGoogleMapsScript()當我打開的呼喚彈出的div在作業結束時從頁面中刪除。 一切工作完美,但有時,地圖變得瘋狂。如果我不更改標記的位置,下次(在同一會話中)我嘗試打開彈出div,與地圖的onclick事件相關的MouseEvent.lat()的值變爲NaN,並且標記輕微向頂部移動地圖。地圖無法正確拖動。 這是我使用彈出移除:

function removePopupDiv(){ 
    jQuery('.popup_div-area').remove(); 
//setTimeout("removeScriptBySrcContent('https://maps.g');removeScriptBySrcContent('.googleapis');",300); 
    scriptLoaded = false; 
    googleMap = null; 
    marker = null; 
} 

正如你看到的我甚至試圖刪除添加腳本標籤,但沒有成功,問題有時會出現。 我試圖解決這一段時間,但沒有成功。有沒有人有過這個問題?我找不到任何人抱怨這個問題。

回答

0

這是我的錯誤之前研究basics ...我應該有明確的字符串,從每一個值設置時間任何不必要的charachters經度和緯度數據。現在一切都充當魅力。

function setLatLong(locationStr){ 
    if (null != locationStr && locationStr.length>2){ 
     latLng = ""+locationStr; 
      latLng = latLng.replace(/\(/g,""); 
      latLng = latLng.replace(/\)/g,""); 
      latLng = latLng.replace(/ */g,""); 
    } 
... 
0

谷歌開發人員有一個演示網站,他們有one這可能是有用的。

開發谷歌地圖應用程序之前,你應該繼續

+0

謝謝,但這首先是我的錯誤。我已更正了一些代碼,現在它正在運行良好。 – Sasa