请参阅下面设置为纬度和经度的简单谷歌地图示例。此代码可以用于此目的,但需要用户选择的城市、州的 php echo 来代替 Lat Long 显示。任何人都可以概述使其执行的代码吗?希望与 Google Maps API v3 兼容。非常感谢。
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { height: 100% }
</style>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&sensor=SET_TO_TRUE_OR_FALSE">
</script>
<script type="text/javascript">
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(-34.397, 150.644),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</html>
过去,我使用以下代码来显示城市、州:
<script src="http://maps.google.com/maps?file=api&v=2&sensor=true&key=BQIAAAAmGS6pKWpRUcdw4oR9pzpvBQzjcAJnzRmzqULnEw6fdi4mPoYnxR1kxeM4lTW9o75OG9vfiUP8DKkig" type="text/javascript"></script>
<script type="text/javascript">
var map = null;
var geocoder = null;
function initialize() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map_canvas"));
map.setMapType(G_HYBRID_MAP);
disableDefaultUI: true;
map.enableScrollWheelZoom();
geocoder = new GClientGeocoder();
}
showAddress("<?php echo $_SESSION['city_name']; ?>");
}
function showAddress(address) {
if (geocoder) {
geocoder.getLatLng(
address,
function(point) {
if (!point) {
//alert(address + " not found");
} else {
map.setCenter(point, 13);
map.setZoom(13);
}
}
);
}
}
</script>
</head>
<body onload="initialize()" onunload="GUnload()">