我已在我提供的管理面板中集成了谷歌地图 字段:城市名称,地址,纬度,经度, 按钮靠近地址:查看地图
我允许用户使用可拖动标记手动选择他们的坐标点。
我使用了symfony2.3&树枝模板
代码:
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false&key={{ google_api_key }}&libraries=places">
</script>
<script type="text/javascript">
function callGmap(latitude, longitude, coordinatescall) {
var zoomlevel = 8;
var myLatlng = new google.maps.LatLng(latitude, longitude);
mapOptions = {
zoom: zoomlevel,
disableDefaultUI: true,
zoomControl: true,
center: new google.maps.LatLng(myLatlng.lat(), myLatlng.lng()),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
gmap = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var marker = new google.maps.Marker({
position: myLatlng,
});
marker.setMap(gmap);
google.maps.event.trigger(gmap, 'resize');
gmap.setZoom(zoomlevel);
google.maps.event.addListener(marker, 'dragend', function (marker) {
var latLng = marker.latLng;
$("#collegelife_commonbundle_university_latitude").val(latLng.lat());
$("#collegelife_commonbundle_university_longitude").val(latLng.lng());
});
}
function initialize() {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({address: "{{ address }}"}, function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
var latitude, longitude;
latitude = results[0].geometry.location.lat();
longitude = results[0].geometry.location.lng();
callGmap(latitude, longitude, false);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
$(function () {
$("#fModal").on("show", function () {
var addresscall = "";
var coordinatescall = false;
alert("resetmarker");
var cityname = $("#collegelife_commonbundle_university_cityid option:selected").text();
var address = $("#collegelife_commonbundle_university_address").val();
var latitude = $("#collegelife_commonbundle_university_latitude").val();
var longitude = $("#collegelife_commonbundle_university_longitude").val();
if (null !== latitude && null !== longitude && latitude !== "" && longitude !== "") {
coordinatescall = true;
} else if (null !== address && address !== "") {
addresscall = address;
} else if (null !== cityname && cityname !== "") {
addresscall = cityname;
} else {
addresscall = {{ google_default_pin_location }};
}
if (coordinatescall === true) {
alert('coordinates call');
callGmap(latitude, longitude, true);
} else if (addresscall != "") {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({address: addresscall}, function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
var latitude, longitude;
latitude = results[0].geometry.location.lat();
longitude = results[0].geometry.location.lng();
callGmap(latitude, longitude, false);
}
});
}
});
});
</script>