2016-05-24 3 views
0
でGoogleプレイスAPIを使用して近くの場所

PHP/Wordpressの

$(document).ready(function() { 
 
    var image_src = "images/"; 
 
    var map; 
 
    var infowindow; 
 
    var bounds = new google.maps.LatLngBounds(); 
 

 
    var PlaceArray = ["restaurant", "cafe", "bar", "grocery_or_supermarket", "parks", "school", "shopping_mall", "movie_theater", "bank", "university"]; 
 
    var PlaceCounter = 0; 
 

 
    function initialize() { 
 
    "use strict"; 
 
    var pyrmont = new google.maps.LatLng(40.5456, -74.4608); 
 

 
    map = new google.maps.Map(document.getElementById('googleMapNearestPlaces'), { 
 
     center: pyrmont, 
 
     zoom: 14, 
 
     icon: 'images/map_marker.png', 
 
     scrollwheel: false, 
 
     rankby: 'distance', 
 
     styles: [{ 
 
     "featureType": "all", 
 
     "elementType": "geometry", 
 
     "stylers": [{ 
 
      "color": "#00c775" 
 
     }] 
 
     }, { 
 
     "featureType": "all", 
 
     "elementType": "labels.text.fill", 
 
     "stylers": [{ 
 
      "gamma": 0.01 
 
     }, { 
 
      "lightness": 20 
 
     }] 
 
     }, { 
 
     "featureType": "all", 
 
     "elementType": "labels.text.stroke", 
 
     "stylers": [{ 
 
      "saturation": -31 
 
     }, { 
 
      "lightness": -33 
 
     }, { 
 
      "weight": 2 
 
     }, { 
 
      "gamma": 0.8 
 
     }] 
 
     }, { 
 
     "featureType": "all", 
 
     "elementType": "labels.icon", 
 
     "stylers": [{ 
 
      "visibility": "off" 
 
     }] 
 
     }, { 
 
     "featureType": "administrative", 
 
     "elementType": "all", 
 
     "stylers": [{ 
 
      "visibility": "on" 
 
     }] 
 
     }, { 
 
     "featureType": "administrative", 
 
     "elementType": "labels", 
 
     "stylers": [{ 
 
      "visibility": "off" 
 
     }] 
 
     }, { 
 
     "featureType": "landscape", 
 
     "elementType": "all", 
 
     "stylers": [{ 
 
      "visibility": "simplified" 
 
     }, { 
 
      "color": "#ffffff" 
 
     }] 
 
     }, { 
 
     "featureType": "landscape", 
 
     "elementType": "geometry", 
 
     "stylers": [{ 
 
      "lightness": 30 
 
     }, { 
 
      "saturation": 30 
 
     }] 
 
     }, { 
 
     "featureType": "poi", 
 
     "elementType": "all", 
 
     "stylers": [{ 
 
      "visibility": "off" 
 
     }] 
 
     }, { 
 
     "featureType": "poi", 
 
     "elementType": "geometry", 
 
     "stylers": [{ 
 
      "saturation": 20 
 
     }] 
 
     }, { 
 
     "featureType": "poi.park", 
 
     "elementType": "geometry", 
 
     "stylers": [{ 
 
      "lightness": 20 
 
     }, { 
 
      "saturation": -20 
 
     }] 
 
     }, { 
 
     "featureType": "road", 
 
     "elementType": "all", 
 
     "stylers": [{ 
 
      "visibility": "off" 
 
     }] 
 
     }, { 
 
     "featureType": "road", 
 
     "elementType": "geometry", 
 
     "stylers": [{ 
 
      "lightness": 10 
 
     }, { 
 
      "saturation": -30 
 
     }] 
 
     }, { 
 
     "featureType": "road", 
 
     "elementType": "geometry.stroke", 
 
     "stylers": [{ 
 
      "saturation": 25 
 
     }, { 
 
      "lightness": 25 
 
     }] 
 
     }, { 
 
     "featureType": "transit", 
 
     "elementType": "all", 
 
     "stylers": [{ 
 
      "visibility": "off" 
 
     }] 
 
     }, { 
 
     "featureType": "water", 
 
     "elementType": "all", 
 
     "stylers": [{ 
 
      "lightness": "0" 
 
     }, { 
 
      "visibility": "on" 
 
     }, { 
 
      "color": "#00c775" 
 
     }, { 
 
      "gamma": "1" 
 
     }, { 
 
      "weight": "1" 
 
     }] 
 
     }, { 
 
     "featureType": "water", 
 
     "elementType": "labels", 
 
     "stylers": [{ 
 
      "visibility": "off" 
 
     }] 
 
     }] 
 
    }); 
 

 
    var marker = new google.maps.Marker({ 
 
     position: pyrmont, 
 
     icon: 'images/map_marker.png' 
 
    }); 
 
    marker.setMap(map); 
 
    var request = { 
 
     location: pyrmont, 
 
     radius: 5000, 
 
     types: ["restaurant", "cafe", "bar", "grocery_or_supermarket", "parks", "school", "shopping_mall", "movie_theater", "bank", "university"], 
 
    }; 
 

 
    infowindow = new google.maps.InfoWindow(); 
 
    var service = new google.maps.places.PlacesService(map); 
 
    service.nearbySearch(request, callback); 
 
    } 
 

 
    function callback(results, status) { 
 
    "use strict"; 
 
    if (status == google.maps.places.PlacesServiceStatus.OK) { 
 
     for (var i = 0; i < results.length; i++) { 
 
     createMarker(results[i]); 
 
     } 
 
    } 
 
    } 
 

 
    function createMarker(place) { 
 
    "use strict"; 
 
    var place_icon; 
 
    place_icon = "images/map-icons/" + place.types['0'] + ".png"; 
 

 
    var PlaceType = place.types['0']; 
 

 
    if (jQuery.inArray(PlaceType, PlaceArray) == -1) { 
 
     return; 
 
    } 
 

 
    PlaceArray = jQuery.grep(PlaceArray, function(value) { 
 
     return value != PlaceType; 
 
    }); 
 

 
    var Distance = distance(place.geometry.location.lat(), place.geometry.location.lng()); 
 

 
    function distance(latitude2, longitude2) { 
 
     var lat1 = "40.5456"; 
 
     var lon1 = "-74.4608"; 
 
     var lat2 = latitude2; 
 
     var lon2 = longitude2; 
 

 
     var radlat1 = Math.PI * lat1/180; 
 
     var radlat2 = Math.PI * lat2/180; 
 
     var radlon1 = Math.PI * lon1/180; 
 
     var radlon2 = Math.PI * lon2/180; 
 
     var theta = lon1 - lon2; 
 
     var radtheta = Math.PI * theta/180; 
 
     var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta); 
 
     dist = Math.acos(dist); 
 
     dist = dist * 180/Math.PI; 
 
     dist = dist * 60 * 1.1515; 
 

 
     dist = dist * 1.609344; 
 

 
     return Math.round(dist * 100)/100; 
 
    } 
 

 
    if (PlaceType == "cafe") { 
 
     PlaceType = "Cofee"; 
 
    } 
 
    if (PlaceType == "grocery_or_supermarket") { 
 
     PlaceType = "Groceries"; 
 
    } 
 
    if (PlaceType == "shopping_mall") { 
 
     PlaceType = "Shopping"; 
 
    } 
 
    if (PlaceType == "movie_theater") { 
 
     PlaceType = "Entertainment"; 
 
    } 
 
    if (PlaceType == "bank") { 
 
     PlaceType = "Errands"; 
 
    } 
 

 
    jQuery("#near-by-place-detail").append("<ul><li class='left'><p><b>" + PlaceType.charAt(0).toUpperCase() + PlaceType.substr(1) + "</b></p><span>" + place.name + "</span></li><li class='right'><label>" + Distance + " Km</label></li></ul>"); 
 

 
    var placeLoc = place.geometry.location; 
 
    var marker = new google.maps.Marker({ 
 
     map: map, 
 
     position: place.geometry.location, 
 
     icon: place_icon, 
 
    }); 
 

 
    google.maps.event.addListener(marker, 'click', function() { 
 
     infowindow.setContent(place.name); 
 
     infowindow.open(map, this); 
 
    }); 
 

 
    bounds.extend(marker.position); 
 

 
    //now fit the map to the newly inclusive bounds 
 
    map.fitBounds(bounds); 
 

 
    //(optional) restore the zoom level after the map is done scaling 
 
    var listener = google.maps.event.addListener(map, "idle", function() { 
 
     map.setZoom(12); 
 
     google.maps.event.removeListener(listener); 
 
    }); 
 

 
    } 
 
    google.maps.event.addDomListener(window, 'load', initialize); 
 
});
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3&libraries=places"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
 
<section class="near_by_places"> 
 
    <div class="container"> 
 
    <h2>Near By Places</h2> 
 
    <div class="row"> 
 
     <div id="near-by-place-detail" class="col-xs-12 col-sm-4 col-md-4"></div> 
 
     <div class="near-by-place-map col-xs-12 col-sm-8 col-md-8"> 
 
     <div id="googleMapNearestPlaces" style="width:100%;height:100%;"></div> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</section>

は、このコードは動作していない取得し、マップが場所によって近くにロードされていません。私は上記のコードを試しましたが、場所によって近くにロードされません。誰もがワードプレスの場所で近くに読み込む方法を知っています。私はGoogle Apiライブラリも使用しました。

+0

ワードプレスであることはあなたは$がundefined'ある 'エラーを得ることはありません...またはあなたがコンソールに見ていることに驚きましたか? – charlietfl

+0

はい、私はすでにコンソールでチェックしています。そして、wordpressは私にエラーを与えていません。私もjqueryで$を修正しました。しかし、地図は場所によって近く見つけられません。何か解決策はありますか? – Dipika

+0

すでにスクリプトファイルを追加していますが、@dingo_d ....は機能していません。 – Dipika

答えて

1

$(document).ready(function(){ 
 
\t var image_src = "images/"; 
 
\t var map; 
 
\t var infowindow; 
 
\t var bounds = new google.maps.LatLngBounds(); 
 

 
\t var PlaceArray = [ "restaurant", "cafe", "bar", "grocery_or_supermarket", "parks", "school", "shopping_mall", "movie_theater", "bank", "university"]; 
 
\t var PlaceCounter = 0; 
 

 
\t function initialize() { 
 
\t \t "use strict"; 
 
\t \t var pyrmont = new google.maps.LatLng(40.5456, -74.4608); 
 

 
\t \t map = new google.maps.Map(document.getElementById('googleMapNearestPlaces'), { 
 
\t \t \t center: pyrmont, 
 
\t \t \t zoom: 14, 
 
\t \t \t icon:'images/map_marker.png', 
 
\t \t \t scrollwheel: false, 
 
\t \t \t rankby: 'distance', 
 
\t \t \t styles: [{"featureType":"all","elementType":"geometry","stylers":[{"color":"#00c775"}]},{"featureType":"all","elementType":"labels.text.fill","stylers":[{"gamma":0.01},{"lightness":20}]},{"featureType":"all","elementType":"labels.text.stroke","stylers":[{"saturation":-31},{"lightness":-33},{"weight":2},{"gamma":0.8}]},{"featureType":"all","elementType":"labels.icon","stylers":[{"visibility":"off"}]},{"featureType":"administrative","elementType":"all","stylers":[{"visibility":"on"}]},{"featureType":"administrative","elementType":"labels","stylers":[{"visibility":"off"}]},{"featureType":"landscape","elementType":"all","stylers":[{"visibility":"simplified"},{"color":"#ffffff"}]},{"featureType":"landscape","elementType":"geometry","stylers":[{"lightness":30},{"saturation":30}]},{"featureType":"poi","elementType":"all","stylers":[{"visibility":"off"}]},{"featureType":"poi","elementType":"geometry","stylers":[{"saturation":20}]},{"featureType":"poi.park","elementType":"geometry","stylers":[{"lightness":20},{"saturation":-20}]},{"featureType":"road","elementType":"all","stylers":[{"visibility":"off"}]},{"featureType":"road","elementType":"geometry","stylers":[{"lightness":10},{"saturation":-30}]},{"featureType":"road","elementType":"geometry.stroke","stylers":[{"saturation":25},{"lightness":25}]},{"featureType":"transit","elementType":"all","stylers":[{"visibility":"off"}]},{"featureType":"water","elementType":"all","stylers":[{"lightness":"0"},{"visibility":"on"},{"color":"#00c775"},{"gamma":"1"},{"weight":"1"}]},{"featureType":"water","elementType":"labels","stylers":[{"visibility":"off"}]}] \t \t 
 
\t \t }); 
 

 
\t \t var marker=new google.maps.Marker({position:pyrmont,icon:'images/map_marker.png'}); 
 
\t \t marker.setMap(map); 
 
\t \t var request = { 
 
\t \t \t location: pyrmont, 
 
\t \t \t radius: 5000, 
 
\t \t \t types: [ "restaurant", "cafe", "bar", "grocery_or_supermarket", "parks", "school", "shopping_mall", "movie_theater", "bank", "university"], 
 
\t \t }; 
 

 
\t \t infowindow = new google.maps.InfoWindow(); 
 
\t \t var service = new google.maps.places.PlacesService(map); 
 
\t \t service.nearbySearch(request, callback); 
 
\t } 
 

 
\t function callback(results, status) { 
 
\t \t "use strict"; 
 
\t \t if (status == google.maps.places.PlacesServiceStatus.OK) { 
 
\t \t \t for (var i = 0; i < results.length; i++) { 
 
\t \t \t \t createMarker(results[i]); 
 
\t \t \t } 
 
\t \t } 
 
\t } 
 

 
\t function createMarker(place) { 
 
\t \t "use strict"; 
 
\t \t var place_icon; 
 
\t \t place_icon = "images/map-icons/" + place.types['0'] + ".png"; 
 
\t \t 
 
\t \t var PlaceType = place.types['0']; 
 

 
\t \t if(jQuery.inArray(PlaceType, PlaceArray) == -1){ 
 
\t \t \t return; 
 
\t \t } 
 
\t \t 
 
\t \t PlaceArray = jQuery.grep(PlaceArray, function(value) { 
 
\t \t return value != PlaceType; 
 
\t \t }); 
 

 
\t \t var Distance = distance(place.geometry.location.lat(),place.geometry.location.lng()); 
 

 
\t \t function distance(latitude2, longitude2) { 
 
\t \t \t var lat1 = "40.5456"; 
 
\t \t \t var lon1 = "-74.4608"; 
 
\t \t \t var lat2 = latitude2; 
 
\t \t \t var lon2 = longitude2; 
 
\t \t \t 
 
\t \t \t var radlat1 = Math.PI * lat1/180; 
 
\t \t \t var radlat2 = Math.PI * lat2/180; 
 
\t \t \t var radlon1 = Math.PI * lon1/180; 
 
\t \t \t var radlon2 = Math.PI * lon2/180; 
 
\t \t \t var theta = lon1-lon2; 
 
\t \t \t var radtheta = Math.PI * theta/180; 
 
\t \t \t var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta); 
 
\t \t \t dist = Math.acos(dist); 
 
\t \t \t dist = dist * 180/Math.PI; 
 
\t \t \t dist = dist * 60 * 1.1515; 
 

 
\t \t \t dist = dist * 1.609344; 
 
\t \t \t 
 
\t \t \t return Math.round(dist * 100)/100; 
 
\t \t } 
 
\t \t \t 
 
\t \t if(PlaceType == "cafe"){ 
 
\t \t \t PlaceType = "Cofee"; 
 
\t \t } 
 
\t \t if(PlaceType == "grocery_or_supermarket"){ 
 
\t \t \t PlaceType = "Groceries"; 
 
\t \t } 
 
\t \t if(PlaceType == "shopping_mall"){ 
 
\t \t \t PlaceType = "Shopping"; 
 
\t \t } 
 
\t \t if(PlaceType == "movie_theater"){ 
 
\t \t \t PlaceType = "Entertainment"; 
 
\t \t } 
 
\t \t if(PlaceType == "bank"){ 
 
\t \t \t PlaceType = "Errands"; 
 
\t \t } 
 

 
\t \t jQuery("#near-by-place-detail").append("<ul><li class='left'><p><b>"+PlaceType.charAt(0).toUpperCase() + PlaceType.substr(1)+"</b></p><span>"+place.name+"</span></li><li class='right'><label>"+Distance+" Km</label></li></ul>"); 
 

 
\t \t var placeLoc = place.geometry.location; 
 
\t \t var marker = new google.maps.Marker({ 
 
\t \t \t map: map, 
 
\t \t \t position: place.geometry.location, 
 
\t \t \t icon:place_icon, 
 
\t \t }); 
 

 
\t \t google.maps.event.addListener(marker, 'click', function() { 
 
\t \t \t infowindow.setContent(place.name); 
 
\t \t \t infowindow.open(map, this); 
 
\t \t }); 
 

 
\t \t bounds.extend(marker.position); 
 

 
\t \t //now fit the map to the newly inclusive bounds 
 
\t \t map.fitBounds(bounds); 
 

 
\t \t //(optional) restore the zoom level after the map is done scaling 
 
\t \t var listener = google.maps.event.addListener(map, "idle", function() { 
 
\t \t  map.setZoom(12); 
 
\t \t  google.maps.event.removeListener(listener); 
 
\t \t }); 
 

 
\t } 
 
\t google.maps.event.addDomListener(window, 'load', initialize); 
 
});
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
 

 
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script> 
 
<section class="near_by_places"> 
 
\t \t \t <div class="container"> 
 
\t \t \t \t <h2>Near By Places</h2> 
 
\t \t \t \t <div class="row"> 
 
\t \t \t \t \t <div id="near-by-place-detail" class="col-xs-12 col-sm-4 col-md-4"></div> 
 
\t \t \t \t \t <div class="near-by-place-map col-xs-12 col-sm-8 col-md-8"> 
 
\t \t \t \t \t \t <div id="googleMapNearestPlaces" style="width:100%;height:100%;"></div> 
 
\t \t \t \t \t </div> 
 
\t \t \t \t </div> 
 
\t \t \t </div> 
 
</section>

+0

説明を追加して、何を変更したので、他の人が同じ問題を抱えているかを知ることができますか? –

+0

私は2つのスクリプトを追加しています。一つはjquery用、もう一つはGoogleマップ用です。 – sneha

関連する問題