做一个简单silverlight Google Map 地图导航,有地名搜索,经度、纬度搜索

运行的效果是:

地名搜索

 void btnCustomSearch_Click(object sender, RoutedEventArgs e)
        {
            MyLayer.Children.Clear(); //清除所有圖釘
            if (txtAddress.Text.Trim() != "")
            {
                Address = txtAddress.Text.Trim();
                TrySearchByAddress();
            }
        }

经纬度搜索

 void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            MyLayer.Children.Clear(); //清除所有圖釘
            if (Latitude.Text != "" && Logitude.Text != "")
            {
                Microsoft.Maps.MapControl.Location center = new Microsoft.Maps.MapControl.Location(Convert.ToDouble(Latitude.Text), Convert.ToDouble(Logitude.Text));
                SetLocation(center);
            }
        }

加载中文地图

private void InitMapTileLayerChina()        

{             google_tileLayer = new MapTileLayer();           

// GoogleMapsRoadTileSource gts_google = new GoogleMapsRoadTileSource();//这个是加载英文地图            

GoogleTileSource gts_google = new GoogleTileSource();//这个是加载中文地图            

google_tileLayer.TileSources.Add(gts_google);

}

 //右鍵 打點
        private void MyMap_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            //MyLayer.Children.Clear(); //清除所有圖釘
            Location location = MyMap.ViewportPointToLocation(e.GetPosition(MyMap));
            this.SetLocation(location);
            e.Handled = true;
        }

弄好页面以及你所要的功能后添加页面代码,不添加会报错的

在XXXX.Web那里找到主页添加js,在<head></head>之间添加Js代码,详细代码如下:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>     <script type="text/javascript">         var geocoder;         var silGM = 'silverlightPlugin';         function SearchAddress(address) {             if (typeof (geocoder) == "undefined" || geocoder == null) {                 geocoder = new google.maps.Geocoder();                 //this.document.getElementById(silGM).content.GoogleMap.SearchAddressEvent = SearchAddress;             }             //var address = document.getElementById("address").value;             //alert(address.Address);             geocoder.geocode({ 'address': address }, function (results, status) {                 if (status == google.maps.GeocoderStatus.OK) {                     //alert(results[0].geometry.location.toString());                     GetDataSuccess(results[0].geometry.location.toString(), results[0].formatted_address);                 } else {                     //alert("Geocode was not successful for the following reason: " + status);                 }             });         }

        function GetDataSuccess(results, address) {             this.document.getElementById(silGM).content.GoogleMap.LoadData(results, address);         }

        function SerializationResult(results) {             var returnVal;             if (obj != undefined) {                 switch (obj.constructor) {                     case Array:                         var vArr = "[";                         for (var i = 0; i < obj.length; i++) {                             if (i > 0) vArr += ",";                             vArr += serialize(obj[i]);                         }                         vArr += "]"                         return vArr;                     case String:                         returnVal = escape("'" + obj + "'");                         return returnVal;                     case Number:                         returnVal = isFinite(obj) ? obj.toString() : null;                         return returnVal;                     case Date:                         returnVal = "#" + obj + "#";                         return returnVal;                     default:                         if (typeof obj == "object") {                             var vobj = []; for (attr in obj) {                                 if (typeof obj[attr] != "function") {                                     vobj.push('"' + attr + '":' + serialize(obj[attr]));                                 }                             }                             if (vobj.length > 0)                                 return "{" + vobj.join(",") + "}";                             else                                 return "{}";                         }                         else {                             return obj.toString();                         }                 }             }             return null;         }

        function initialize() {             //this.document.getElementById(silGM).content.GoogleMap.SearchAddressEvent = SearchAddress;         }

    </script>

在<body>的的里面添加object id="silverlightPlugin",大致如下:

<body>
    <form id="form1" runat="server" style="height:100%">
    <div id="silverlightControlHost">
        <object id="silverlightPlugin" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
    <param name="source" value="ClientBin/SilverlightGoogleMap.xap"/>
    <param name="onError" value="onSilverlightError" />
    <param name="background" value="white" />
    <param name="minRuntimeVersion" value="5.0.61118.0" />
    <param name="autoUpgrade" value="true" />

假如不添加无办法搜索会提示: search address Error Object reference not set to an instance of an anject.

不指定id="silverlightPlugin"报错信息是

 

 

 下载Demo的地址是

 

posted on 2012-08-01 16:03  linguorun  阅读(633)  评论(2)    收藏  举报

导航