我想在我的页面中实现谷歌地图。在我的页面中有一个文本框和一个按钮。用户需要输入他想要 map 的位置。然后单击按钮时, map 应显示在一个 div 中。
谁能给我提供这样做的 steos?
提前致谢。
请您参考如下方法:
如果您使用 google api js 文件,这将非常容易。作为引用,您可以使用此链接: http://code.google.com/apis/maps/documentation/javascript/reference.html
在 HEAD 部分添加 jQuery 和 GoogleMaps API 库:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script> <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>添加新标签以在 div 中创建 google maps 对象:
var geocoder; var map; $(document).ready(function() { /* Create the geocoder */ geocoder = new google.maps.Geocoder(); /* Some initial data for the map */ mapOptions = { zoom: 10, center: new google.maps.LatLng(48.13, 13.58), mapTypeId: google.maps.MapTypeId.TERRAIN }; map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); };为按钮添加点击处理程序:
$('#idMyButton').click(function() { if (geocoder) { var address = $('#idMyTextbox').val(); geocoder.geocode( { 'address': address}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { /* Position the map */ map.panTo(results[0].geometry.location); /* Put a marker */ new google.maps.Marker({ map: map, position: results[0].geometry.location, title: address, }); } else alert('Address not found'); }; } });
希望对您有所帮助。
