C# 如何在运行时通过单击更改图标和多边形线的颜色
您已经根据v2编辑了脚本。我在使用附在第154行的脚本图片时遇到此错误C# 如何在运行时通过单击更改图标和多边形线的颜色,c#,asp.net,google-maps,polyline,C#,Asp.net,Google Maps,Polyline,您已经根据v2编辑了脚本。我在使用附在第154行的脚本图片时遇到此错误 GEvent.addListener(window.polyline, 'click', function() { window.polyline.setOptions(options: { strokeColor: 'blue' }); 我们如何通过点击正在运行的应用程序来改变图标和多段线的颜色?我的代码如下。它没有错误。我该怎么做 using Sy
GEvent.addListener(window.polyline, 'click', function() {
window.polyline.setOptions(options: { strokeColor: 'blue' });
我们如何通过点击正在运行的应用程序来改变图标和多段线的颜色?我的代码如下。它没有错误。我该怎么做
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace GoogleMap
{
// A sample project by Ghaffar khan
public partial class Map : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataSet1TableAdapters.LocationsTableAdapter da = new GoogleMap.DataSet1TableAdapters.LocationsTableAdapter();
DataSet1.LocationsDataTable table = new DataSet1.LocationsDataTable();
da.Fill(table);
BuildScript(table);
//GooglePolyline PL1 = new GooglePolyline();
}
private void BuildScript(DataTable tbl)
{
String Locations = "";
foreach (DataRow r in tbl.Rows)
{
// bypass empty rows
if (r["Latitude"].ToString().Trim().Length == 0)
continue;
string Latitude = r["Latitude"].ToString();
string Longitude = r["Longitude"].ToString();
// create a line of JavaScript for marker on map for this record
Locations += Environment.NewLine + " map.addOverlay(new GMarker(new GLatLng(" + Latitude + "," + Longitude + ")));";
}
// construct the final script
js.Text = @"<script type='text/javascript'>
function initialize() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById('map_canvas'));
map.setCenter(new GLatLng(45.05,7.6667), 2);
map.openInfoWindowHtml ( new GLatLng(45.05,7.6667), 'Serif<br>Pakistan');
" + Locations + @"
map.setUIToDefault();
// Create an array with points
var points = [
new GLatLng(24.85229, 67.01703),
new GLatLng(24.914463, 67.0965958),
new GLatLng(24.86588, 67.06089),
new GLatLng(24.9726753, 67.06638),
new GLatLng(24.840023, 67.24285),
new GLatLng(24.85229, 67.01703)
];
// Create a new polyline
var polyline = new GPolyline(points, '#ff0000', 5, 0.7);
// Add the polyline to the map using map.addOverlay()
map.addOverlay(polyline);
var marker = new GMarker(new GLatLng(45.05,7.6667))
//
}
}
</script> ";
}
}
}
使用系统;
使用系统集合;
使用系统配置;
使用系统数据;
使用System.Linq;
使用System.Web;
使用System.Web.Security;
使用System.Web.UI;
使用System.Web.UI.HTMLControl;
使用System.Web.UI.WebControl;
使用System.Web.UI.WebControl.WebParts;
使用System.Xml.Linq;
名称空间谷歌地图
{
//Ghaffar khan的示例项目
公共部分类映射:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
DataSet1TableAdapters.LocationStableApter da=new GoogleMap.DataSet1TableAdapters.LocationStableApter();
DataSet1.LocationsDataTable=新DataSet1.LocationsDataTable();
da.填写(表格);
构建脚本(表);
//GooglePolyline PL1=新的GooglePolyline();
}
私有void构建脚本(数据表tbl)
{
字符串位置=”;
foreach(待处理行中的数据行r)
{
//绕过空行
如果(r[“纬度”].ToString().Trim().Length==0)
继续;
字符串纬度=r[“纬度”]。ToString();
字符串经度=r[“经度”]。ToString();
//为该记录的映射上的标记创建一行JavaScript
Locations+=Environment.NewLine+“map.addOverlay(新的GMarker(新的GLatLng(“+纬度+”,“+经度+”));”;
}
//构建最终脚本
js.Text=@”
函数初始化(){
if(GBrowserIsCompatible()){
var map=newgmap2(document.getElementById('map_canvas');
赛特中心地图(新格拉特林(45.05,7.6667),2);
map.openInfoWindowHtml(新GLatLng(45.05,7.6667),“Serif
巴基斯坦”);
“+位置+@”
map.setUIToDefault();
//创建具有点的数组
变量点=[
新玻璃(24.85229,67.01703),
新玻璃(24.914463,67.0965958),
新玻璃(24.86588,67.06089),
新玻璃(24.9726753,67.06638),
新玻璃(24.840023,67.24285),
新玻璃(24.85229、67.01703)
];
//创建新的多段线
var polyline=新的GPolyline(点,#ff0000',5,0.7);
//使用map.addOverlay()将多段线添加到地图
map.addOverlay(多段线);
var标记=新的GMarker(新的GLatLng(45.05,7.6667))
//
}
}
";
}
}
}
//////根据GergL的说法,剧本是///////
js.Text = @"<script type='text/javascript'>
function initialize() {
if (GBrowserIsCompatible()) {
window.polyline = new GPolyline(points, '#ff0000', 5, 0.7);
var map = new GMap2(document.getElementById('map_canvas'));
map.setCenter(new GLatLng(45.05,7.6667), 2);
map.openInfoWindowHtml ( new GLatLng(45.05,7.6667), 'Serif<br>Pakistan');
" + Locations + @"
map.setUIToDefault();
google.maps.event.addListener(window.polyline, 'click', function() {
window.polyline.setOptions(options: { strokeColor: 'blue' });
});
// Create an array with points
var points = [
new GLatLng(24.85229, 67.01703),
new GLatLng(24.914463, 67.0965958),
new GLatLng(24.86588, 67.06089),
new GLatLng(24.9726753, 67.06638),
new GLatLng(24.840023, 67.24285),
new GLatLng(24.85229, 67.01703)
];
// Create a new polyline
var polyline = new GPolyline(points, '#ff0000', 5, 0.7);
// Add the polyline to the map using map.addOverlay()
map.addOverlay(polyline);
}
}
google.maps.event.addListener(window.polyline, 'click', function() {
window.polyline.setOptions(options: { strokeColor: 'blue' });
});
</script> ";
js.Text=@”
函数初始化(){
if(GBrowserIsCompatible()){
window.polyline=新的GPolyline(点,#ff0000',5,0.7);
var map=newgmap2(document.getElementById('map_canvas');
赛特中心地图(新格拉特林(45.05,7.6667),2);
map.openInfoWindowHtml(新GLatLng(45.05,7.6667),“Serif
巴基斯坦”);
“+位置+@”
map.setUIToDefault();
google.maps.event.addListener(window.polyline,'click',function(){
setOptions(选项:{strokeColor:'blue'});
});
//创建具有点的数组
变量点=[
新玻璃(24.85229,67.01703),
新玻璃(24.914463,67.0965958),
新玻璃(24.86588,67.06089),
新玻璃(24.9726753,67.06638),
新玻璃(24.840023,67.24285),
新玻璃(24.85229、67.01703)
];
//创建新的多段线
var polyline=新的GPolyline(点,#ff0000',5,0.7);
//使用map.addOverlay()将多段线添加到地图
map.addOverlay(多段线);
}
}
google.maps.event.addListener(window.polyline,'click',function(){
setOptions(选项:{strokeColor:'blue'});
});
";
但我得到了错误。我在脚本第47行使用了click方法,希望得到您的帮助
您已经根据v2编辑了脚本。我在使用附加的脚本pic时遇到此错误。您需要将
initialize()
函数中的polyline
变量指定给全局可访问的变量,然后使用其上的方法在单击事件处理程序(javascript)中设置新颜色
e、 g.如果您更改了initialise()
中的行:
var polyline = new GPolyline(points, '#ff0000', 5, 0.7);
为此:
window.polyline = new GPolyline(points, '#ff0000', 5, 0.7);
然后想在单击时将其更改为蓝色,您可以添加如下代码:
GEvent.addListener(window.polyline, 'click', function() {
window.polyline.setOptions(options: { strokeColor: 'blue' });
});
顺便说一句,我还没有测试过这个,但它应该能帮助你走上正确的道路
编辑:我认为您的脚本应该是这样的:编辑2:已更新以使用API v2,但请注意,Google强烈鼓励您在和上迁移到新的V3
js.Text=@”
函数初始化(){
if(GBrowserIsCompatible()){
var map=newgmap2(document.getElementById('map_canvas');
赛特中心地图(新格拉特林(45.05,7.6667),2);
map.openInfoWindowHtml(新GLatLng(45.05,7.6667),“Serif
巴基斯坦”);
“+位置+@”
map.setUIToDefault();
//使用poi创建一个数组
js.Text = @"<script type='text/javascript'>
function initialize() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById('map_canvas'));
map.setCenter(new GLatLng(45.05,7.6667), 2);
map.openInfoWindowHtml ( new GLatLng(45.05,7.6667), 'Serif<br>Pakistan');
" + Locations + @"
map.setUIToDefault();
// Create an array with points
var points = [
new GLatLng(24.85229, 67.01703),
new GLatLng(24.914463, 67.0965958),
new GLatLng(24.86588, 67.06089),
new GLatLng(24.9726753, 67.06638),
new GLatLng(24.840023, 67.24285),
new GLatLng(24.85229, 67.01703)
];
// Create a new polyline
window.polyline = new GPolyline(points, '#ff0000', 5, 0.7);
GEvent.addListener(window.polyline, 'click', function() {
window.polyline.setOptions(options: { strokeColor: 'blue' });
});
// Add the polyline to the map using map.addOverlay()
map.addOverlay(window.polyline);
}
}
</script> ";