早上好,伙计们,
我正在创建一个基于GMaps Geolocation的应用程序。
我的数据库中有Lat和Long。 我创建了一个JSON,其中包含我需要传递给Javascript的数据,但是当我尝试在Javascript中调用JSON文件时,它没有添加标记..
CS代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using System.Data;
using MySql.Data.MySqlClient;
using System.Reflection;
namespace testeMapa
{
public partial class index : System.Web.UI.Page
{
private MySqlConnection mConn;
private MySqlDataAdapter mAdapter;
private DataSet mDataSet;
protected void Page_Load(object sender, EventArgs e)
{
mConn = new MySqlConnection("server=*****;User Id=*****;database=*****; password=*****");
mConn.Open();
MySqlCommand com = new MySqlCommand();
com.Connection = mConn;
com.CommandText = "Select idDado, latitudeEnd, longitudeEnd, enderecoDado from inserirDados";
MySqlDataReader dr = com.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
}
[WebMethod]
static public string Carregar(DataTable dt )
{
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
List<Dictionary<string, object>> parentRow = new List<Dictionary<string, object>>();
Dictionary<string, object> childRow;
foreach (DataRow row in dt.Rows)
{
childRow = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
childRow.Add(col.ColumnName, row[col]);
}
parentRow.Add(childRow);
}
var pontos = jsSerializer.Serialize(parentRow);
return pontos;
}
}
}
我的JSon采用以下格式:
[
{
"idDado": 11,
"latitudeEnd": "-22.8668838",
"longitudeEnd": "-43.307824100000005",
"enderecoDado": "R. Limeira, 16 - Tomás Coelho, Rio de Janeiro - RJ, 21370- 460, Brasil"
},
{
"idDado": 10,
"latitudeEnd": "-22.9501581",
"longitudeEnd": "-43.18422179999999",
"enderecoDado": "Botafogo - Botafogo, Rio de Janeiro - RJ, Brasil"
}
]
我的JavaScript:
var map;
var idInfoBoxAberto;
var infoBox = [];
var markers = [];
function initialize() {
var latlng = new google.maps.LatLng(-18.8800397, -47.05878999999999);
var options = {
zoom: 5,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("mapa"), options);
}
initialize();
function abrirInfoBox(id, marker) {
if (typeof (idInfoBoxAberto) == 'number' && typeof (infoBox[idInfoBoxAberto]) == 'object') {
infoBox[idInfoBoxAberto].close();
}
infoBox[id].open(map, marker);
idInfoBoxAberto = id;
}
function carregarPontos() {
$.ajax({
type: 'POST',
url: 'testeMapa/index.aspx/Carregar',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (pontos) {
var latlngbounds = new google.maps.LatLngBounds();
$(pontos).each(function (i) {
var lat = pontos[i].latitudeEnd;
var long = pontos[i].longitudeEnd;
var endereco = pontos[i].enderecoDado;
var idDados = pontos[i].idDado;
var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat, long),
title: "Meu ponto personalizado! :-D",
icon: 'img/marcador.png'
});
var myOptions = {
content: "<p>" + endereco + "</p>",
pixelOffset: new google.maps.Size(-150, 0)
};
infoBox[idDados] = new InfoBox(myOptions);
infoBox[idDados].marker = marker;
infoBox[idDados].listener = google.maps.event.addListener(marker, 'click', function (e) {
abrirInfoBox(idDados, marker);
});
markers.push(marker);
latlngbounds.extend(marker.position);
});
var markerCluster = new MarkerClusterer(map, markers);
map.fitBounds(latlngbounds);
} });
}
carregarPontos();
我是这个C#和JavaScript的新手......所以它可能有很多错误.. 地图正在显示,但没有添加任何积分。
你们能帮助我吗?
提前致谢!