Get Lat/Long and put markers on gmap

Apr 21, 2008 at 2:56 PM
Hi All

This is the first time I am using Gmap control.
I have downloaded the map control

I added reference to map control.
I have few issues that I need to solve and I hope if anyone can help me with that.

I have addresses that need to be plotted on the gmap.
what I got was that I have to use latitude and longitude and then pass them to the gmarker to be plotted .

so my first question is how to calculate the latitude and longitude with the code ie on server side.
I have many addresses that I am storing in my database, so I thought of having extra columns for latitude and longitude and would store them along with the each address., but i dont know how to calculate the latitude and longitude.

second question is how to loop through and add markers based on my latitude and longitudes on gmap.

third ques: Do we have to pass the latitude and longitude to the gmap control initially ?

I hope if anyone can help me with this.
Thanks

Coordinator
Apr 21, 2008 at 4:53 PM
Hi,

  • Firts, you can set the address instead of latitude/longitude to GoogleMap control or any GoogleMarker added.
As you can see they have a porperty Address.
Check out this page
  • Second, you can loop through your your data and add markers to GoogleMap control Markers GoogleMap1.Markers.Add(marker)
Or you can bind GoogleMap control to any DataSource in order to provide you markers' data.
Check out this page
  • Third, yes you should set Latitude/Longitude or Address of GoogleMap control if you want to set it to specific location.

In order the get familiar with GoogleMap control download Sample Website from release files and check out the pages and samples there.
The code of sample pages I pointed above is available there. as well.

Hope this helps
Apr 21, 2008 at 5:35 PM
Edited Apr 21, 2008 at 6:49 PM
thanks for the reply.

As you said i can directly pass addresses to my marker. I did the same but it does not shows anything.
here is the code that i have:

for(int i=0;i<ds.Tables0.Rows.Count;i++)
{
GoogleMarker marker = new GoogleMarker();
marker.Address = ds.Tables0.Rowsi"Address".ToString();
marker.Text = ds.Tables0.Rowsi"Address".ToString();
GoogleMap1.Markers.Add(marker);
}

Is there something that I am missing in my above code.
I have two rows that are returned in my dataset above.
the addresses contained in the 2 rows are like this:
156 Prince Street, New York, NY 10018
300 Sand Lane, Staten Island, NY 10305

Thanks
Apr 21, 2008 at 8:11 PM
also i tried this:
for (int i = 0; i < ds.Tables0.Rows.Count; i++)
{
GoogleMarker marker = new GoogleMarker();
marker.Latitude = Convert.ToDouble(ds.Tables0.Rowsi"Latitude");
marker.Longitude = Convert.ToDouble(ds.Tables0.Rowsi"Longitude");
marker.Text = ds.Tables0.Rowsi"Address".ToString();

GoogleMap1.Markers.Add(marker);
}

the above code does work and i can see markers plotted but i have to manually calculate and add the latitude and longitude pts. Do you know a way of finding the latitude or longitude c# .
Or else if you can sugges why passing address to the markers directly as posted in my above post is not working.
Thanks
Coordinator
Apr 22, 2008 at 6:29 AM
Edited Apr 22, 2008 at 6:33 AM
Hi,

I suggest you take a look at Data Binding Sample or DataBinding.aspx page in the Sample Website, which you can download from release files.
Or you even can go here under Source Code tab, inside Sample Website folder then /map/DataBinding.aspx
There you'll find and example of how GoogleMap control is bound to ObjectDataSource and set the data fields and the rest is a work of the control.
You don't need to loop through you table rows (data items) in order to data bind markers.
In your case, if you using MS SQL, then you should use SqlDataSource and just set its ID in GoogleMap control DataSourceID.
In order GoogleMap control to know where to get data for markers you have to set data field names:
DataAddressField="YOURADDRESSCOLUMN_NAME"
DataLatitudeField="YOURLATITUDECOLUMN_NAME"
DataLongitudeField="YOULONGITUDECOLUMN_NAM"
DataTextField="YOUTEXTCOLUMN_NAME"
So, if you want to use Latitude/Langitude, then there is no need to set DataAddressField
If you keep you latitude and longitude data as double values in Database, then no conversion is required on data binding.
Here is how my sample page looks like:
<artem:GoogleMap ID="GoogleMap1" runat="server" Width="530px" Height="500px" Latitude="42.1229" Longitude="24.7879"
       Zoom="4" EnableScrollWheelZoom="true" BorderStyle="Solid" BorderColor="#999999" BorderWidth="1" DataSourceID="_odsMarkers"
       DataAddressField="Address" DataTextField="Description">
   </artem:GoogleMap>
   <asp:ObjectDataSource ID="_odsMarkers" runat="server" OldValuesParameterFormatString="{0}" SelectMethod="GetMarkersData"
       TypeName="Artem.GoogleMap.WebSite.DataSourceHelper"></asp:ObjectDataSource>

In your case, as I told you you should replace ObjectDataSource with the proper SqlDataSource and set its ID to DataSourceID of GoogleMap control.

That's it.

Regards
Apr 22, 2008 at 1:33 PM
Thanks for your reply.

I got what are you trying to explain, the only thing I am having problem is how to calculate the latitude and longitude.
I have a asp.net page which when open shows a google map with addresses plotted on it using markers.
I have used your control for that functionality. Your control is great and does what I am trying to achieve. The only problem I am not able to understand is how to calculate the latitude and longitude from a given address. If i have latitude and longitude pts then I can bind all my markers to it as:
marker.Latitude = ds.Tables0.Rowsi"Latitude";
marker.Longitude ds.Tables0.Rowsi"Longitude"
and this method works fine as long as i have those two cordinates.

You also said that
marker.Address = "695 Park Avenue, 68th & 69th, New York, NY";
should work, but i dont see any markers if I use the above code.

So if you can please suggest me how to calculate those two co ordinates.
Thanks