下面的代码将显示用户从网站上一页选择的城市、州。但是,地图首先加载 LatLong 位置,然后打开城市、州。如何消除 LatLong 变量使其不加载?只想展示城市、州。
<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://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=YOUR_KEY&sensor=false"></script>
<script type="text/javascript">
var geocoder;
var map;
var marker;
function initialize() {
var latlng = new google.maps.LatLng(-34.397, 150.644);
var mapOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
geocoder = new google.maps.Geocoder();
marker = new google.maps.Marker({
map: map,
draggable: true
});
showAddress('"<?php echo $_SESSION['city_name']; ?>"');
}
function showAddress(address) {
geocoder.geocode({'address': address}, function(results, status) {
if (status != google.maps.GeocoderStatus.OK) {
return;
}
if (results.length > 1) {
alert('Multiple addresses found; showing first one ...');
}
$.each(results, function(i, item) {
var location = new google.maps.LatLng(item.geometry.location.lat(), item.geometry.location.lng());
marker.setPosition(location);
map.setCenter(location);
return false;
});
});
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</html>