2017-03-16 10 views
0

私は現在、Ajaxが自分のページでの実行を終了し、次にテキストをロードしてからオートコンプリートダイアログを表示するときに実行されている以下の機能を持っています。Google Maps Autocomplete keypress simulation

私は次の操作を行う必要があり
<script type="text/javascript"> $(document).ajaxComplete(function() { 
    jQuery('#facetwp-location').val('Richmond, Vic'); // populate the input box 
    google.maps.event.trigger(document.querySelector('#facetwp-location'), 'focus', {}); // force the autocomplete to show 
    }); 

</script> 

  1. (事前に書かれたテキストを置き換えます)バディXprofileフィールド
  2. からユーザーの位置情報が自動的にオートコンプリートの最初の項目を読み込む取得リスト(つまり、キーダウンをシミュレートし、表示されたリストにEnterキーを押してください)

もう一度、私のjQueryスキルは欠けており、 sの機能。

私はそれに取り組んで、そしてこれまでのところ、これを持っている - それはまだ

function get_user_location_for_facet_load(){ 
if (is_page(2101)) { 
?> 
<script type="text/javascript"> 
$(function() { 
$(document).ajaxStop(function() { 
jQuery('#facetwp-location').val('<?php 
$user_id = bp_loggedin_user_id(); 
echo xprofile_get_field_data('Suburb',$user_id); 
?>'); // populate the input box 
$('#facetwp-location').focus(); 
google.maps.event.trigger(document.querySelector('#facetwp-location'), 'focus', {}); // force the autocomplete to show 
$(".pac-container .pac-item:first").promise().done(function() { 
google.maps.event.trigger(document.querySelector('.pac-container .pac-item'), 'focus', {}); 
google.maps.event.trigger(document.querySelector('.pac-container .pac-item'), 'keydown', { 
     keyCode: 13 
    }); 

//var txt = $('#facetwp-location').val(); 
//alert(txt); 
}); 
}); 
}); 
</script> 
<?php 
} 

ない私はどの近づいていますかどうかわからを完了しますが、誰が助けることができる場合ではありません。

答えて

0

あなたのコードは、正しい見て、しかし、のような$.ready()でコードを囲むしようとしている

$(function() { // enclose your ajax complete in document ready function 
    $(document).ajaxComplete(function() { 
     jQuery('#facetwp-location').val('Richmond, Vic'); // populate the input box 
     google.maps.event.trigger(document.querySelector('#facetwp-location'), 'focus', {}); // force the autocomplete to show 
    }); 
}); 

また、同様にそのハンドラ内でGoogleマップを入力の上.keypress()を使用してトリガしてみ

$(function() { // enclose your ajax complete in document ready function 
    $(document).ajaxComplete(function() { 
     jQuery('#facetwp-location').val('Richmond, Vic').keypress(function(){ 
      google.maps.event.trigger(this, 'focus', {}); // force the autocomplete to show 
     }); 
     jQuery('#facetwp-location').trigger('keypress'); 
    }); 
}); 

、Googleコード

$(function() { 
 
    var lat = -33.8688, 
 
    lng = 151.2195, 
 
    latlng = new google.maps.LatLng(lat, lng), 
 
    image = 'http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png'; 
 

 
    var mapOptions = { 
 
     center: new google.maps.LatLng(lat, lng), 
 
     zoom: 13, 
 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
 
    }, 
 
    map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions), 
 
    marker = new google.maps.Marker({ 
 
     position: latlng, 
 
     map: map, 
 
     icon: image 
 
    }); 
 

 
    var input = document.getElementById('facetwp-location'); 
 
    var autocomplete = new google.maps.places.Autocomplete(input, { 
 
    types: ["geocode"] 
 
    }); 
 

 
    autocomplete.bindTo('bounds', map); 
 
    var infowindow = new google.maps.InfoWindow(); 
 

 
    google.maps.event.addListener(autocomplete, 'place_changed', function() { 
 
    infowindow.close(); 
 
    var place = autocomplete.getPlace(); 
 
    if (place.geometry.viewport) { 
 
     map.fitBounds(place.geometry.viewport); 
 
    } else { 
 
     map.setCenter(place.geometry.location); 
 
     map.setZoom(17); 
 
    } 
 

 
    moveMarker(place.name, place.geometry.location); 
 
    }); 
 

 
    $("#facetwp-location").focus(function() { 
 
    selectFirstResult(); 
 
    }); 
 

 

 
    function selectFirstResult() { 
 
    infowindow.close(); 
 
    $(".pac-container").hide(); 
 
    var firstResult = $('#facetwp-location').val(); 
 
    var geocoder = new google.maps.Geocoder(); 
 
    geocoder.geocode({ 
 
     "address": firstResult 
 
    }, function(results, status) { 
 
     if (status == google.maps.GeocoderStatus.OK) { 
 
     var lat = results[0].geometry.location.lat(), 
 
      lng = results[0].geometry.location.lng(), 
 
      placeName = results[0].address_components[0].long_name, 
 
      latlng = new google.maps.LatLng(lat, lng); 
 

 
     moveMarker(placeName, latlng); 
 
     } 
 
    }); 
 
    } 
 

 
    function moveMarker(placeName, latlng) { 
 
    marker.setIcon(image); 
 
    marker.setPosition(latlng); 
 
    infowindow.setContent(placeName); 
 
    infowindow.open(map, marker); 
 
    } 
 

 
    $('#facetwp-location').trigger('focus'); 
 
});
#map_canvas { 
 
    width: 100%; 
 
    height: 400px; 
 
    background-color: grey; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyCsaZtFeCU6YCKI_1D_tJ73JzARq9j6zaY&libraries=places"></script> 
 
<input id="facetwp-location" value="Richmond, Vic" type="text" size="50" /> 
 
<div id="map_canvas"></div>
012を更新しました

+0

これが本当の機能変更が追加されていませんし、オートコンプリートを提出し、私の更新の答えや代替オプションを試してみてください –

+0

検索できるようにするために任意のシミュレートされたキー入力が含まれていません。それはあなたのために働くことを願っています。 –

+0

これはまだ、ユーザーが何も押さなくてもやってみようとしているキー入力をブラウザに提出しません。これは、Google Maps Autocomplete APIに関する他の提案や、提出するために何をする必要があるか(つまり、下矢印キーを押してから、リスト内の最初の場所を選択するためにENTERを押してください) –