ASP.NET Google Maps Javascript API V3 实战基础篇一地址解析

来源:互联网 发布:卷皮版淘宝客程序 编辑:程序博客网 时间:2024/05/16 09:01

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Geocoding_Sample.aspx.cs"
    Inherits="Samples.Services.Geocoding_Sample" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>地址解析</title>
    <style type="text/css">
        #maps
        {
            height: 450px;
        }
    </style>

    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&language=zh-CN"></script>

    <script language="javascript" type="text/javascript">

        var geocoder; //申明地址解析对象
        var map;
        function initialize() {
            geocoder = new google.maps.Geocoder(); //实例化地址解析对象
            var LatLng = new google.maps.LatLng(-34.397, 150.644);
            var Options = {
                zoom: 8, center: LatLng, mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            map = new google.maps.Map(document.getElementById("maps"), Options);
        }
        //地址解析函数 ,如果解析成功将地图中心设置到解析地址,并在该处创建标记
        function codeAddress() {
            var address = document.getElementById("address").value;
            if (geocoder) {
                geocoder.geocode(
                    { "address": address },
                    function(results, status) {
                        if (status == google.maps.GeocoderStatus.OK) {
                            map.setCenter(results[0].geometry.location);
                            var marker = new google.maps.Marker(
                            {
                                map: map, position: results[0].geometry.location
                            });
                        } else {
                            alert("不能进行地址解析,原因如下:" + status);
                        }
                    }
                );
            }

        }
        //将地图初始化函数绑定到window的load事件
        google.maps.event.addDomListener(window, "load", initialize);                
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="address" type="text" value="Sydney, NSW" />
        <input id="geocoding" type="button" value="解析" onclick="codeAddress();" />
    </div>
    <div id="maps">
    </div>
    </form>
</body>
</html>