WPF 集成google map v3

这次准备来一篇扫盲贴,废话不说了,直接进主题吧。

第一步:新建一个WPF 应用程序项目如图,

WPF 集成google map v3 - GIF - GIF

第二步:在项目里添加一个HTML文件,具体就是右键“解决方案资源管理器”中的项目名---“添加”----“新建项”----“HTML页”,如图,

WPF 集成google map v3 - GIF - GIF

第三步:在新增的html文件里敲进代码

<html>
 <head>
     <title></title>
  <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
  <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>        
        <script type ="text/javascript">
        var map;
        function InitializeMap() 
        {
            //var latlng = new google.maps.LatLng(-34.397, 150.644);
            var latlng = new google.maps.LatLng(22.84, 108.33);
            var myOptions = {
                zoom: 9,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            map = new google.maps.Map(document.getElementById("map"), myOptions);
            var marker = new google.maps.Marker
            (
                {
                    position: new google.maps.LatLng(22.84, 108.33),
                    map: map,
                    title: '南宁市'//这是鼠标悬停内容
                }
            );
            var infowindow = new google.maps.InfoWindow({
                content: '南宁市'//这是现实具体内容
            });
            google.maps.event.addListener(marker, 'click', function() {
                // Calling the open method of the infoWindow 
                infowindow.open(map, marker);
            });
            //getBoundary();
        }
        
    </script>
 </head>
 <body οnlοad="InitializeMap()">
 <div id ="map"   style="width: 100%; position: absolute; height: 100%">
    </div>
 </body>
</html>
第四步:在xaml文件中敲入代码

<Window x:Class="GMapTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <WebBrowser x:Name="webBrowser1"/>
    </Grid>
</Window>

第五步:在cs文件中敲入代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace GMapTest
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            Basic ds = new Basic(); 
            webBrowser1.Navigate(new Uri(System.Environment.CurrentDirectory + @"/HTMLPage1.htm", UriKind.RelativeOrAbsolute));
            webBrowser1.ObjectForScripting = ds;
        }
    }

    [System.Runtime.InteropServices.ComVisibleAttribute(true)]//将该类设置为com可访问  
    public class Basic
    {
        public static string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public void ClickEvent(string str)
        {
            this.Name = str;
        }
    }  
}

第六步:编译,运行,效果如图

WPF 集成google map v3 - GIF - GIF

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhenye1986

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值