2016-03-30 2 views
-1

WordPressのテンプレートでGoogleマップの実装を変更しようとしましたが、マーカー付きの単一のマップ(動作する)ではなく、リストからの位置。クリックイベントはマーカーを移動し、地図を中央に置き、理想的には新しいズームレベルを設定する必要があります。私は数時間一緒に混乱してきたが、私のJavascriptの知識の欠如は途方に暮れている。WordPressテンプレートでmarker.setPositionとmap.panToを使用できません

有用な場合は、JSFiddleを作成しました。

本当に誰かが喜んで助けてくれることを願っています。前もって感謝します!

function pan(latlon) { 
 
    var coords = latlon.split(","); 
 
    var panPoint = new google.maps.LatLng(coords[0], coords[1]); 
 

 
\t \t marker.setPosition(panPoint); 
 
    map.panTo(panPoint); 
 
// \t \t map.setZoom(3); // This would have to be dynamic, based on data-zoom 
 
} 
 

 
google_api_map_init_259585579(); 
 
function google_api_map_init_259585579(){ 
 
var map; 
 
var coordData = new google.maps.LatLng(parseFloat(3.165612), parseFloat(101.6504025)); 
 
var marker; 
 

 
var styleArray = [ 
 
    { 
 
     "featureType": "landscape", 
 
     "stylers": [ 
 
      { 
 
       "saturation": -100 
 
      }, 
 
      { 
 
       "lightness": 65 
 
      }, 
 
      { 
 
       "visibility": "on" 
 
      } 
 
     ] 
 
    }, 
 
    { 
 
     "featureType": "poi", 
 
     "stylers": [ 
 
      { 
 
       "saturation": -100 
 
      }, 
 
      { 
 
       "lightness": 51 
 
      }, 
 
      { 
 
       "visibility": "simplified" 
 
      } 
 
     ] 
 
    }, 
 
    { 
 
     "featureType": "road.highway", 
 
     "stylers": [ 
 
      { 
 
       "saturation": -100 
 
      }, 
 
      { 
 
       "visibility": "simplified" 
 
      } 
 
     ] 
 
    }, 
 
    { 
 
     "featureType": "road.arterial", 
 
     "stylers": [ 
 
      { 
 
       "saturation": -100 
 
      }, 
 
      { 
 
       "lightness": 30 
 
      }, 
 
      { 
 
       "visibility": "on" 
 
      } 
 
     ] 
 
    }, 
 
    { 
 
     "featureType": "road.local", 
 
     "stylers": [ 
 
      { 
 
       "saturation": -100 
 
      }, 
 
      { 
 
       "lightness": 40 
 
      }, 
 
      { 
 
       "visibility": "on" 
 
      } 
 
     ] 
 
    }, 
 
    { 
 
     "featureType": "transit", 
 
     "stylers": [ 
 
      { 
 
       "saturation": -100 
 
      }, 
 
      { 
 
       "visibility": "simplified" 
 
      } 
 
     ] 
 
    }, 
 
    { 
 
     "featureType": "administrative.province", 
 
     "stylers": [ 
 
      { 
 
       "visibility": "off" 
 
      } 
 
     ] 
 
    }, 
 
    { 
 
     "featureType": "water", 
 
     "elementType": "labels", 
 
     "stylers": [ 
 
      { 
 
       "visibility": "on" 
 
      }, 
 
      { 
 
       "lightness": -25 
 
      }, 
 
      { 
 
       "saturation": -100 
 
      } 
 
     ] 
 
    }, 
 
    { 
 
     "featureType": "water", 
 
     "elementType": "geometry", 
 
     "stylers": [ 
 
      { 
 
       "hue": "#ffff00" 
 
      }, 
 
      { 
 
       "lightness": -25 
 
      }, 
 
      { 
 
       "saturation": -97 
 
      } 
 
     ] 
 
    } 
 
] 
 

 
function initialize() { 
 
    var mapOptions = { 
 
\t \t \t \t \t \t \t zoom: 5, 
 
\t \t \t \t \t \t \t center: coordData, 
 
\t \t \t \t \t \t \t scrollwheel: false, 
 
       \t \t \t styles: styleArray 
 
\t \t \t \t \t \t } 
 
    var map = new google.maps.Map(document.getElementById("map-canvas-259585579"), mapOptions); 
 

 
    var markerIcon = { 
 
\t \t \t     url: "http://maps.gstatic.com/mapfiles/markers2/marker_sprite.png", 
 
\t \t \t     size: new google.maps.Size(72, 74), 
 
\t \t \t     origin: new google.maps.Point(0,0), 
 
\t \t \t     anchor: new google.maps.Point(30, 74) 
 
\t \t \t    }; 
 

 

 
\t \t \t \t \t \t marker = new google.maps.Marker({ 
 
\t \t \t \t \t \t \t map:map, 
 
\t \t \t \t \t \t \t draggable:false, 
 
\t \t \t \t \t \t \t position: coordData, 
 
\t \t \t \t \t \t \t icon: markerIcon 
 
\t \t \t \t \t \t }); 
 

 
    $('.location').on('click', function() { 
 
     pan($(this).data('coords')); 
 
     $("#location-output").text($(this).data('location')); 
 
    }); 
 

 
} 
 

 
google.maps.event.addDomListener(window, 'load', initialize); 
 
}
html, body, #map-canvas-259585579 { 
 
    height: 570px; 
 
    width: 100%; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
 
<script src='//maps.googleapis.com/maps/api/js?v=3.exp&#038;sensor=false&#038;ver=4.4.2'></script> 
 
<ul> 
 
    <li><a class="location" data-zoom="5" data-coords="3.1656120,101.6504025" data-location="Kuala Lumpur">Malaysia</a></li> 
 
    <li><a class="location" data-zoom="12" data-coords="-6.2257931,106.8059866" data-location="Jakarta">Indonesia</a></li> 
 
    <li><a class="location" data-zoom="8" data-coords="25.1951156,55.2747310" data-location="Dubai">United Arab Emirates (UAE)</a></li> 
 
</ul> 
 

 
<div id="location-output"></div> 
 

 
<div class="google-map-api map-1"> 
 
    <div id="map-canvas-259585579" class="gmap"></div> 
 
</div>

答えて

0

を1つのオプションはとmap変数の両方をグローバルにすることです。

var map, marker; 
function pan(latlon) { 
    var coords = latlon.split(","); 
    var panPoint = new google.maps.LatLng(coords[0], coords[1]); 
    marker.setPosition(panPoint); 
    map.panTo(panPoint); 
} 

次に、あなたのinitialize関数内でグローバル値を初期化します。

function initialize() { 
    // ... 
    map = new google.maps.Map(document.getElementById("map-canvas-259585579"), mapOptions); 
    // ... 
    marker = new google.maps.Marker({ 
    map:map, 
    draggable:false, 
    position: coordData, 
    icon: markerIcon 
    }); 

updated fiddle

updated fiddle with marker shadow in correct place(マーカーの影はもはやネイティブのGoogleマップのJavaScript API v3ではによってサポートされています)

コードスニペット:

0123返信用回の

var map, marker; 
 

 
function pan(latlon) { 
 
    var coords = latlon.split(","); 
 
    var panPoint = new google.maps.LatLng(coords[0], coords[1]); 
 
    marker.setPosition(panPoint); 
 
    map.panTo(panPoint); 
 
} 
 

 
google_api_map_init_259585579(); 
 

 
function google_api_map_init_259585579() { 
 
    var coordData = new google.maps.LatLng(parseFloat(3.165612), parseFloat(101.6504025)); 
 

 

 
    function initialize() { 
 
    var mapOptions = { 
 
     zoom: 5, 
 
     center: coordData, 
 
     scrollwheel: false, 
 
     styles: styleArray 
 
    } 
 
    map = new google.maps.Map(document.getElementById("map-canvas-259585579"), mapOptions); 
 

 
    var markerIcon = { 
 
     url: "http://maps.gstatic.com/mapfiles/markers2/marker_sprite.png", 
 
     size: new google.maps.Size(72, 74), 
 
     origin: new google.maps.Point(0, 0), 
 
     anchor: new google.maps.Point(30, 74) 
 
    }; 
 

 

 
    marker = new google.maps.Marker({ 
 
     map: map, 
 
     draggable: false, 
 
     position: coordData, 
 
     icon: markerIcon 
 
    }); 
 

 
    $('.location').on('click', function() { 
 
     pan($(this).data('coords')); 
 
     $("#location-output").text($(this).data('location')); 
 
    }); 
 

 
    } 
 

 
    google.maps.event.addDomListener(window, 'load', initialize); 
 
} 
 
var styleArray = [{ 
 
    "featureType": "landscape", 
 
    "stylers": [{ 
 
    "saturation": -100 
 
    }, { 
 
    "lightness": 65 
 
    }, { 
 
    "visibility": "on" 
 
    }] 
 
}, { 
 
    "featureType": "poi", 
 
    "stylers": [{ 
 
    "saturation": -100 
 
    }, { 
 
    "lightness": 51 
 
    }, { 
 
    "visibility": "simplified" 
 
    }] 
 
}, { 
 
    "featureType": "road.highway", 
 
    "stylers": [{ 
 
    "saturation": -100 
 
    }, { 
 
    "visibility": "simplified" 
 
    }] 
 
}, { 
 
    "featureType": "road.arterial", 
 
    "stylers": [{ 
 
    "saturation": -100 
 
    }, { 
 
    "lightness": 30 
 
    }, { 
 
    "visibility": "on" 
 
    }] 
 
}, { 
 
    "featureType": "road.local", 
 
    "stylers": [{ 
 
    "saturation": -100 
 
    }, { 
 
    "lightness": 40 
 
    }, { 
 
    "visibility": "on" 
 
    }] 
 
}, { 
 
    "featureType": "transit", 
 
    "stylers": [{ 
 
    "saturation": -100 
 
    }, { 
 
    "visibility": "simplified" 
 
    }] 
 
}, { 
 
    "featureType": "administrative.province", 
 
    "stylers": [{ 
 
    "visibility": "off" 
 
    }] 
 
}, { 
 
    "featureType": "water", 
 
    "elementType": "labels", 
 
    "stylers": [{ 
 
    "visibility": "on" 
 
    }, { 
 
    "lightness": -25 
 
    }, { 
 
    "saturation": -100 
 
    }] 
 
}, { 
 
    "featureType": "water", 
 
    "elementType": "geometry", 
 
    "stylers": [{ 
 
    "hue": "#ffff00" 
 
    }, { 
 
    "lightness": -25 
 
    }, { 
 
    "saturation": -97 
 
    }] 
 
}]
html, 
 
body, 
 
.gmap, 
 
.google-map-api { 
 
    height: 100%; 
 
    width: 100%; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://maps.googleapis.com/maps/api/js"></script> 
 
<ul> 
 
    <li><a class="location" data-zoom="5" data-coords="3.1656120,101.6504025" data-location="Kuala Lumpur">Malaysia</a> 
 
    </li> 
 
    <li><a class="location" data-zoom="12" data-coords="-6.2257931,106.8059866" data-location="Jakarta">Indonesia</a> 
 
    </li> 
 
    <li><a class="location" data-zoom="8" data-coords="25.1951156,55.2747310" data-location="Dubai">United Arab Emirates (UAE)</a> 
 
    </li> 
 
</ul> 
 

 
<div id="location-output"></div> 
 

 
<div class="google-map-api map-1"> 
 
    <div id="map-canvas-259585579" class="gmap"></div> 
 
</div>

+0

優秀! JSFiddleの仕事をしました:)残念ながら、WPテンプレートにコードを実装しようとすると、私はまだエラーが出ます: "Uncaught TypeError:未定義のプロパティ 'split'を読み込めません。私はあなたの答えをどのような場合でも解決策としてマークしたと思います! – Marteyn

+0

これはおそらく、 'pan'関数を間違って呼び出すことを意味しています(または、あなたが書いたコードが少なくともどのように呼び出されるかとは異なります)。その引数は" latitude、longitude "という形式の文字列ではありません。 – geocodezip

0

変数mapとはpan()にアクセスすることはできません。

考えられる解決策:pan()への引数としてこれらの変数を渡す:

function pan(latlon,map,marker) { 
 
    var coords = latlon.split(","); 
 
    var panPoint = new google.maps.LatLng(coords[0], coords[1]); 
 

 
\t \t marker.setPosition(panPoint); 
 
    map.panTo(panPoint); 
 
// \t \t map.setZoom(3); // This would have to be dynamic, based on data-zoom 
 
} 
 

 
google_api_map_init_259585579(); 
 
function google_api_map_init_259585579(){ 
 
var map; 
 
var coordData = new google.maps.LatLng(parseFloat(3.165612), parseFloat(101.6504025)); 
 
var marker; 
 

 
var styleArray = [ 
 
    { 
 
     "featureType": "landscape", 
 
     "stylers": [ 
 
      { 
 
       "saturation": -100 
 
      }, 
 
      { 
 
       "lightness": 65 
 
      }, 
 
      { 
 
       "visibility": "on" 
 
      } 
 
     ] 
 
    }, 
 
    { 
 
     "featureType": "poi", 
 
     "stylers": [ 
 
      { 
 
       "saturation": -100 
 
      }, 
 
      { 
 
       "lightness": 51 
 
      }, 
 
      { 
 
       "visibility": "simplified" 
 
      } 
 
     ] 
 
    }, 
 
    { 
 
     "featureType": "road.highway", 
 
     "stylers": [ 
 
      { 
 
       "saturation": -100 
 
      }, 
 
      { 
 
       "visibility": "simplified" 
 
      } 
 
     ] 
 
    }, 
 
    { 
 
     "featureType": "road.arterial", 
 
     "stylers": [ 
 
      { 
 
       "saturation": -100 
 
      }, 
 
      { 
 
       "lightness": 30 
 
      }, 
 
      { 
 
       "visibility": "on" 
 
      } 
 
     ] 
 
    }, 
 
    { 
 
     "featureType": "road.local", 
 
     "stylers": [ 
 
      { 
 
       "saturation": -100 
 
      }, 
 
      { 
 
       "lightness": 40 
 
      }, 
 
      { 
 
       "visibility": "on" 
 
      } 
 
     ] 
 
    }, 
 
    { 
 
     "featureType": "transit", 
 
     "stylers": [ 
 
      { 
 
       "saturation": -100 
 
      }, 
 
      { 
 
       "visibility": "simplified" 
 
      } 
 
     ] 
 
    }, 
 
    { 
 
     "featureType": "administrative.province", 
 
     "stylers": [ 
 
      { 
 
       "visibility": "off" 
 
      } 
 
     ] 
 
    }, 
 
    { 
 
     "featureType": "water", 
 
     "elementType": "labels", 
 
     "stylers": [ 
 
      { 
 
       "visibility": "on" 
 
      }, 
 
      { 
 
       "lightness": -25 
 
      }, 
 
      { 
 
       "saturation": -100 
 
      } 
 
     ] 
 
    }, 
 
    { 
 
     "featureType": "water", 
 
     "elementType": "geometry", 
 
     "stylers": [ 
 
      { 
 
       "hue": "#ffff00" 
 
      }, 
 
      { 
 
       "lightness": -25 
 
      }, 
 
      { 
 
       "saturation": -97 
 
      } 
 
     ] 
 
    } 
 
] 
 

 
function initialize() { 
 
    var mapOptions = { 
 
\t \t \t \t \t \t \t zoom: 5, 
 
\t \t \t \t \t \t \t center: coordData, 
 
\t \t \t \t \t \t \t scrollwheel: false, 
 
       \t \t \t styles: styleArray 
 
\t \t \t \t \t \t } 
 
    var map = new google.maps.Map(document.getElementById("map-canvas-259585579"), mapOptions); 
 

 
    var markerIcon = { 
 
\t \t \t     url: "http://maps.gstatic.com/mapfiles/markers2/marker_sprite.png", 
 
\t \t \t     size: new google.maps.Size(72, 74), 
 
\t \t \t     origin: new google.maps.Point(0,0), 
 
\t \t \t     anchor: new google.maps.Point(30, 74) 
 
\t \t \t    }; 
 

 

 
\t \t \t \t \t \t marker = new google.maps.Marker({ 
 
\t \t \t \t \t \t \t map:map, 
 
\t \t \t \t \t \t \t draggable:false, 
 
\t \t \t \t \t \t \t position: coordData, 
 
\t \t \t \t \t \t \t icon: markerIcon 
 
\t \t \t \t \t \t }); 
 

 
    $('.location').on('click', function() { 
 
     pan($(this).data('coords'),map,marker); 
 
     $("#location-output").text($(this).data('location')); 
 
    }); 
 

 
} 
 

 
google.maps.event.addDomListener(window, 'load', initialize); 
 
}
html, body, #map-canvas-259585579 { 
 
    height: 570px; 
 
    width: 100%; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
 
<script src='//maps.googleapis.com/maps/api/js?v=3.exp&#038;sensor=false&#038;ver=4.4.2'></script> 
 
<ul> 
 
    <li><a class="location" data-zoom="5" data-coords="3.1656120,101.6504025" data-location="Kuala Lumpur">Malaysia</a></li> 
 
    <li><a class="location" data-zoom="12" data-coords="-6.2257931,106.8059866" data-location="Jakarta">Indonesia</a></li> 
 
    <li><a class="location" data-zoom="8" data-coords="25.1951156,55.2747310" data-location="Dubai">United Arab Emirates (UAE)</a></li> 
 
</ul> 
 

 
<div id="location-output"></div> 
 

 
<div class="google-map-api map-1"> 
 
    <div id="map-canvas-259585579" class="gmap"></div> 
 
</div>

+0

ありがとう!それは意味をなさない。私は推測していますが、スクリプト全体にいくつかの間違いがあるかもしれないので、私はまだ何かが起こるのを見ていません。私はコンソールをチェックして、 "Uncaught TypeError:未定義のプロパティ 'split'を読み取ることができません。私はここでメソッドとして正しく使用していると思っていました。 – Marteyn

関連する問題