2016-04-09 6 views
1

8個のPHPファイルを含むメインのPHPファイルがあります。私はブートストラップのクラス "Modal"を使用し、私は各モーダルでGoogleマップを使用します。だから私はGoogleマップを表示するための異なるIDを持っていますが、機能は同じです。それは、各モーダルポップアップの1つが、機能が解雇され、Googleマップを示すことを意味します。私は各IDに8つの異なる機能を実装しています。異なるIDを持つ1つの関数を起動する方法

私が望むのは、1つの機能だけで実装する方法はありますか?だから、もっとはっきり言うこと

id = 1とモーダルポップアップあれば、それは<div id="location_incident1">にしてid = 2とモーダルポップアップ場合は、Googleマップを示し、それは<div id="location_incident2">でグーグルマップを示しており、id = 3とモーダルポップアップあれば、それは<div id="location_incident3">でグーグルマップを示し、そうです。 、あなたは関数の外map3marker3、...変数を保存する必要がない場合は

function locationOfIncident3(current) 
{ 
    var lat = current.coords.latitude; 
    latIncident = lat; 
    var lon = current.coords.longitude; 
    lonIncident = lon; 
    var mapOption={ 
     center: new google.maps.LatLng(lat, lon), 
     zoom: 15, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    map3 = new google.maps.Map(document.getElementById('location_incident3'), mapOption); 

    marker3 = new google.maps.Marker({ 
     position: new google.maps.LatLng(lat, lon), 
     map: map3, 
     draggable: true, 
     title: 'Location of Incident' 
    }); 

    google.maps.event.addListener(marker3, 'dragend', function(res){ 
     latIncident = res.latLng.lat(); 
     lonIncident = res.latLng.lng(); 
     getAddress3(latIncident, lonIncident); 
    }); 
} 
+2

あるSimpyは、2番目のパラメータとして関数にIDを渡す... – CBroe

+0

何らかの理由 'latIncident '、' map3'、...は、 'var map3'とは対照的に、グローバル変数である必要があります、...? – Aprillion

答えて

0

は、ローカル変数を使用する必要があり、すなわちvar mapvar market

はここに簡単に私のコードです。 ..

番の文字列は次のように+連結演算子を使用して動的に作成することができます。

function locationOfIncident(n, current) { 
    ... 
    var map = new google.maps.Map(document.getElementById('location_incident' + n), mapOption); 
    ... 
} 

そしてlocationOfIncident1に、locationOfIncident2は、...正規表現を使用して、テキストエディタに置き換えるあなたが電話をリファクタリングすることができます:locationOfIncident(\d+)\s*\( =>locationOfIncident\($1,

関連する問題