0

場所を特定するためのツールとしてグーグルマップを使おうとしています。私はその目的のためにjavaScriptapiv3を使用しています。ジオロケーションにもipinfodbapiを使用しています。APIの問題であると思われる問題が発生しましたが、mabyで何かを忘れています...コードの一部を次に示します。

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);
        });
    }
}

ジョブが終了するとページから削除されるポップアップdivを開くときに、loadGoogleMapsScript()の呼び出しから始めます。すべてが完璧に機能しますが、マップがおかしくなることがあります。マーカーの位置を変更しない場合、次回(同じセッションで)ポップアップ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;
}

ご覧のとおり、追加されたスクリプトタグを削除しようとしましたが、成功せず、問題が発生することがあります。私はしばらくの間これを解決しようとしていますが、成功しませんでした。誰かが以前にこの問題を抱えていましたか?この問題について他に不満を言う人は誰もいません。

4

2 に答える 2

0

Google の開発者には、デモのサイトがあり、役に立つかもしれないサイトがあります。

Google マップ アプリケーションを開発する前に、続行する前に基本を学習する必要があります。

于 2012-09-27T17:20:06.590 に答える
0

それは私の間違いでした...値が設定されるたびに、不要な文字から経度と緯度のデータを含む明確な文字列が必要です。今ではすべてが魅力として機能します。

function setLatLong(locationStr){
    if (null != locationStr && locationStr.length>2){
        latLng = ""+locationStr;
            latLng = latLng.replace(/\(/g,"");
            latLng = latLng.replace(/\)/g,"");
            latLng = latLng.replace(/ */g,"");
    }
...
于 2012-09-28T10:14:30.680 に答える