我正在努力实现与uber.it相同的地图包含拾取位置标记,当我更改标记位置时,它显示我的fromaddress文本框上的当前地址...但是当我在fromaddress文本框上输入一些地址时我想要更改地图上的标记位置也是。
请任何人帮我做这个
<script type="text/javascript">
var map;
var marker;
var myLatlng = new google.maps.LatLng('<?php echo $static_lat; ?>','<?php echo $static_lng; ?>');
var geocoder = new google.maps.Geocoder();
var infowindow = new google.maps.InfoWindow();
function initialize(){
var mapOptions = {
zoom: 13,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map"), 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]) {
$('#fromlat,#fromlong').show();
$('#fromaddress').val(results[0].formatted_address);
$('#fromlat').val(marker.getPosition().lat());
$('#fromlong').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]) {
$('#fromaddress').val(results[0].formatted_address);
$('#fromlat').val(marker.getPosition().lat());
$('#fromlong').val(marker.getPosition().lng());
infowindow.setContent(results[0].formatted_address);
infowindow.open(map, marker);
}
}
});
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
答案 0 :(得分:2)
只需创建视图地图功能,并在按下按键后调用lat和long ...
<script type="text/javascript">
var map;
var marker;
var infowindow = new google.maps.InfoWindow();
var geocoder = new google.maps.Geocoder();
function initialize() {
var location = document.getElementById('PoolLocation');
var autocomplete = new google.maps.places.Autocomplete(location);
google.maps.event.addListener(autocomplete, 'place_changed', function () {
var place = autocomplete.getPlace();
//document.getElementById('PoolLocation').value = place.name;
document.getElementById('PoolLatitude').value = place.geometry.location.lat();
document.getElementById('PoolLongitude').value = place.geometry.location.lng();
viewMap(place.geometry.location.lat(),place.geometry.location.lng());
});
}
function viewMap(lat,lng){
var myLatlng = new google.maps.LatLng(lat,lng);
var myOptions = {
zoom: 14,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var marker = new google.maps.Marker({
draggable: true,
position: myLatlng,
map: map,
title: "Your location"
});
var formatted_address = 'Select Pickup location';
infowindow.setContent(formatted_address);
infowindow.open(map, marker);
google.maps.event.addListener(marker, 'dragend', function (event) {
geocodePosition(marker.getPosition());
document.getElementById("PoolLatitude").value = this.getPosition().lat();
document.getElementById("PoolLongitude").value = this.getPosition().lng();
});
function geocodePosition(pos) {
var geocoder= new google.maps.Geocoder();
geocoder.geocode({
latLng: pos
}, function(responses) {
if (responses && responses.length > 0) {
document.getElementById("PoolLocation").value = responses[0].formatted_address;
}
});
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>