在這裏,我使用谷歌地圖API,在頁面加載時間地圖工作正常,但我的要求是當我點擊定位我按鈕時間地圖應顯示我試了很多,但我我不能夠做到,任何一個請更新我code.Morning在病房我努力,但我沒有得到我的回答,我是發展領域的新人 如何顯示谷歌地圖onclick按鈕
function addressinitialize() {
var input = document.getElementById('searchTextField');
var autocomplete = new google.maps.places.Autocomplete(input);
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
console.log('Place Object',place)
console.log(place.geometry.location.lat());
console.log(place.geometry.location.lng());
$('#latitude').val(place.geometry.location.lat());
$('#longitude').val(place.geometry.location.lng());
});
}
google.maps.event.addDomListener(window, 'load', addressinitialize);
function initialize() {
var input = document.getElementById('searchTextField');
var autocomplete = new google.maps.places.Autocomplete(input);
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
console.log('Place Object',place)
console.log(place.geometry.location.lat());
console.log(place.geometry.location.lng());
$('#latitude').val(place.geometry.location.lat());
$('#longitude').val(place.geometry.location.lng());
});
}
google.maps.event.addDomListener(window, 'load', initialize);
var lat = $('#latitude').val();
var lng = $('#longitude').val();
var map;
var marker;
var myLatlng = new google.maps.LatLng(lat,lng);
var geocoder = new google.maps.Geocoder();
var infowindow = new google.maps.InfoWindow();
function mapinitialize(){
var mapOptions = {
zoom: 18,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("myMap"), mapOptions);
marker = new google.maps.Marker({
map: map,
position: myLatlng,
draggable: true
});
geocoder.geocode({'latLng': myLatlng }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
$('#address').val(results[0].formatted_address);
$('#latitude').val(marker.getPosition().lat());
$('#longitude').val(marker.getPosition().lng());
infowindow.setContent(results[0].formatted_address);
infowindow.open(map, marker);
}
}
});
google.maps.event.addListener(marker, 'dragend', function() {
geocoder.geocode({'latLng': marker.getPosition()}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
$('#address').val(results[0].formatted_address);
$('#latitude').val(marker.getPosition().lat());
$('#longitude').val(marker.getPosition().lng());
infowindow.setContent(results[0].formatted_address);
infowindow.open(map, marker);
}
}
});
});
}
google.maps.event.addDomListener(window, 'load', mapinitialize);
function myFunction() {
var panPoint = new google.maps.LatLng(document.getElementById("latitude").value, document.getElementById("longitude").value);
map.panTo(panPoint);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBwuev6Dj8Xvo7fePYs2YJ8KA84xxBCIUo&libraries=places"></script>
<input type="PickupAddress" class="form-control" name="PickupAddress" placeholder="Sublocality" id="searchTextField" autocomplete="on" runat="server" required="" data-msg-required="Please enter Pick up Address" aria-required="true" > <button onclick="myFunction();document.getElementById('map').style.visibility = 'visible'">Locate Me</button>
<div class="col-md-8" style="padding-left: 0px;visibility:hidden" id="map">
<label>Locate your Flat-room-mate on the map:</label>
<br><br><br>
<div id="myMap" style="height:400px;width:100%;"></div>
<br/>
<div class="col-md-12" style="padding-left:0px;">
ADDRESS: <input id="address" name="address" type="text" style="width:100%;" class="form-control border" />
LATITUDE: <input type="text" name="latitude" id="latitude"> <br>
LONGITUDE: <input type="text" name="longitude" id="longitude">
</div>
<br>
</div>
您可以創建上的jsfiddle codpen.io或撥弄筆? – alt255
看到片段@ alt255 – Pedram