以下是一個簡單的示例代碼,其中包括使用C#調用高德地圖API,并在多個標識中點擊其中一個標識時打開相應的新窗口的代碼。
首先,需要添加以下命名空間:
using System;
using System.Windows.Forms;
using System.Net;
using Newtonsoft.Json.Linq;
然后,在類中定義一個WebBrowser控件:
private WebBrowser webBrowser1;
接下來,可以在窗體加載時初始化WebBrowser控件:
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1 = new WebBrowser();
webBrowser1.Dock = DockStyle.Fill;
this.Controls.Add(webBrowser1);
string url = "http://webapi.amap.com/maps?v=1.4.14&key=your_key";
webBrowser1.Navigate(url);
}
注意:在上面的代碼中,將“your_key”替換為您的高德地圖API密鑰。
然后,可以在調用API時添加標識,并將其與相應的新窗口鏈接關聯:
private void AddMarker(JObject location, string name, string address, string phone, string url)
{
string lng = location["lng"].ToString();
string lat = location["lat"].ToString();
string markerUrl = string.Format("http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png");
string markerId = string.Format("marker_{0}", markerCount);
markerCount++;
string markerScript = string.Format("var marker_{0} = new AMap.Marker({{position: new AMap.LngLat({1}, {2}), map: map, icon: '{3}'}});",
markerId, lng, lat, markerUrl);
string infoScript = string.Format("var info_{0} = new AMap.InfoWindow({{content: '<div class=\"info\"><h4>{1}</h4><p>{2}</p><p>電話:{3}</p><p><a href=\"{4}\">查看詳情</a></p></div>'}});",
markerId, name, address, phone, url);
string clickScript = string.Format("AMap.event.addListener(marker_{0}, 'click', function() {{ info_{0}.open(map, marker_{0}); }});",
markerId);
webBrowser1.Document.InvokeScript("eval", new object[] { markerScript + infoScript + clickScript });
}
在上面的代碼中,通過調用AddMarker函數來添加標識,其中“location”是包含經度和緯度信息的JObject對象,“name”、“address”、“phone”和“url”分別是標識的名稱、地址、電話和鏈接。然后,使用AMap.Marker和AMap.InfoWindow來創建標識和信息窗口,分別將它們的_id設置為“marker_id”和“info_id”。最后,通過將監聽器添加到標識中的click事件來顯示信息窗口。
完整的示例代碼如下:
using System;
using System.Windows.Forms;
using System.Net;
using Newtonsoft.Json.Linq;
namespace AMapDemo
{
public partial class Form1 : Form
{
private WebBrowser webBrowser1;
private int markerCount = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1 = new WebBrowser();
webBrowser1.Dock = DockStyle.Fill;
this.Controls.Add(webBrowser1);
string url = "http://webapi.amap.com/maps?v=1.4.14&key=your_key";
webBrowser1.Navigate(url);
}
private void AddMarker(JObject location, string name, string address, string phone, string url)
{
string lng = location["lng"].ToString();
string lat = location["lat"].ToString();
string markerUrl = string.Format("http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png");
string markerId = string.Format("marker_{0}", markerCount);
markerCount++;
string markerScript = string.Format("var marker_{0} = new AMap.Marker({{position: new AMap.LngLat({1}, {2}), map: map, icon: '{3}'}});",
markerId, lng, lat, markerUrl);
string infoScript = string.Format("var info_{0} = new AMap.InfoWindow({{content: '<div class=\"info\"><h4>{1}</h4><p>{2}</p><p>電話:{3}</p><p><a href=\"{4}\">查看詳情</a></p></div>'}});",
markerId, name, address, phone, url);
string clickScript = string.Format("AMap.event.addListener(marker_{0}, 'click', function() {{ info_{0}.open(map, marker_{0}); }});",
markerId);
webBrowser1.Document.InvokeScript("eval", new object[] { markerScript + infoScript + clickScript });
}
private void button1_Click(object sender, EventArgs e)
{
string keyword = textBox1.Text;
string url = string.Format("http://restapi.amap.com/v3/place/text?key=your_key&keywords={0}&city=全國", keyword);
WebClient client = new WebClient();
string result = client.DownloadString(url);
JObject json = JObject.Parse(result);
JArray pois = (JArray)json["pois"];
foreach (JObject poi in pois)
{
JObject location = (JObject)poi["location"];
string name = poi["name"].ToString();
string address = poi["address"].ToString();
string phone = poi["tel"].ToString();
string url1 = poi["url"].ToString();
AddMarker(location, name, address, phone, url1);
}
}
}
}
注意:在最后的button1_Click函數中,將“your_key”替換為您的高德地圖API密鑰,并將“textbox1”替換為您的文本框。