I am trying to pinpoint locations on google (or bing) maps, and I'm unable to do so in both. The code structure is similar in both, and I present the google maps attempt:
The objective: I have a linked list of Objects, each of which contain latitude and longitude, all of which have to be pinpointed.
Full ASP: http://pastie.org/7752124 (The repeater is to present the search results)
Relevant (in head):
<script type="text/javascript">
var map = null;
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(40.42, -98.737),
zoom: 4,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map-canvas"),
mapOptions);
//pushPin(25, 80);
}
function pushPin(lat, lon) {
alert(lat + " " + lon);
var myLatlng = new google.maps.LatLng(lat, lon);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: "Hello World!"
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
Codebehind: Activated when user clicks the search button:
protected void Button1_Click(object sender, EventArgs e)
{
List<Results> rlist = getResults(query);
String Locations = "";
foreach (Results res in rlist)
{
Page.ClientScript.RegisterStartupScript(
GetType(),
"MyKey",
"pushPin("+res.latitude+","+res.longitude+");",
true);
}
this.rptREsults.DataSource = rlist;
this.rptREsults.DataBind();
}
public class Results
{
public string filename { get; set; }
public string asciiname { get; set; }
public string latitude { get; set; }
public string longitude { get; set; }
}
The results are correct and do contain latitude and longitude, and when I put an alert in the pushPin method, only one alert box comes, but no pinpoint is created. Is there a better way to store and pinpoint a list of Latitudes and Longitudes?
The pushPin method works when I call it from another javascript method.
I tend to do it this way, this is a cut down version/example of some code Im using on another site but should give you enough to get things working:
var markers = new Array();
var markerLatLongs = new Array();
var sites = // JSON formatted version of your Results object(s)
function initialize() {
var mapOptions = {
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map'),
mapOptions);
createMarkers();
}
function createMarkers() {
for (var j = 0; j < sites.length; j++)
{
markerLatLongs[j] = new google.maps.LatLng(sites.latitude ,sites.longitude )
markers[j] = new google.maps.Marker({
position: markerLatLongs[j],
map: map,
title: sites[j].siteName
});
}
}
In order to solve the timing problem rap your calls to pushPin in another function, then add a call to the function wrapper from within initialize something like this:
protected void Button1_Click(object sender, EventArgs e)
{
List<Results> rlist = getResults(query);
String Locations = "";
StringBuilder pushPinCalls = new StringBuilder();
foreach (Results res in rlist)
{
pushPinCalls.AppendFormat("pushPin({0},{1});",res.latitude,res.longitude);
}
Page.ClientScript.RegisterStartupScript(
GetType(),
"MyKey",
string.Format("function pushPinManager() {{0}}",pushPinCalls.ToString());,
true);
this.rptREsults.DataSource = rlist;
this.rptREsults.DataBind();
}
and in the js:
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(40.42, -98.737),
zoom: 4,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map-canvas"),
mapOptions);
pushPinManager();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.