Marker to String and back

792 views
Skip to first unread message

Rick Donohoe

unread,
Feb 16, 2011, 10:29:25 AM2/16/11
to google-map...@googlegroups.com
Hey guys,

Page in question: www.my-walk.com/addwalk.php

Im using the following code to put my markers into a string so they can be easily stored in a databse:

 for (i = 0; (i < markersArray.length); i++)
      {
        markerString += markersArray[i].getPosition().toString() + "," ;
      }

With this code I usually get the following example of String:

(54.65495130292765, -3.354217564526365),(54.66627060518853, -3.3509559983642556),(54.659121939101354, -3.331043278637693),(54.65495130292765, -3.341686284008787),(54.65922123474505, -3.3641739243896462),(54.66279571629887, -3.3578224534423806),

Once I pull this String back out of MySql, how do I go about converting it back to Markers?

Also, I have found that every so often the Google Map won't display on my page, instead it is just a grey area where the map should be. To fix this I have to close Firefox and Reload all the open tabs.

Can anyone tell me why this is?

I Appreciate any help guys,

Rick

geoco...@gmail.com

unread,
Feb 16, 2011, 10:39:48 AM2/16/11
to Google Maps JavaScript API v3
On Feb 16, 7:29 am, Rick Donohoe <crazyra...@aol.com> wrote:
> Hey guys,
>
> Page in question:www.my-walk.com/addwalk.php
>
> Im using the following code to put my markers into a string so they can be
> easily stored in a databse:
>
>  for (i = 0; (i < markersArray.length); i++)
>       {
>         markerString += markersArray[i].getPosition().toString() + "," ;
>       }
>
> With this code I usually get the following example of String:
>
> (54.65495130292765, -3.354217564526365),(54.66627060518853,
> -3.3509559983642556),(54.659121939101354,
> -3.331043278637693),(54.65495130292765,
> -3.341686284008787),(54.65922123474505,
> -3.3641739243896462),(54.66279571629887, -3.3578224534423806),
>
> Once I pull this String back out of MySql, how do I go about converting it
> back to Markers?

How do you add your markers to start with?

addMarker(event.latLng);

event.latLng is a google.maps.LatLng object, you need to change your
string (which is a sequence of latitude and longitude values as
strings) into pairs of numbers (latitude and longitude) and pass those
into the google.maps.LatLng constructor.
http://code.google.com/apis/maps/documentation/javascript/reference.html#LatLng

Then use those resulting google.maps.LatLng objects as the argument to
call addMarker.

-- Larry

geoco...@gmail.com

unread,
Feb 16, 2011, 10:43:54 AM2/16/11
to Google Maps JavaScript API v3
On Feb 16, 7:39 am, "geocode...@gmail.com" <geocode...@gmail.com>
wrote:
> On Feb 16, 7:29 am, Rick Donohoe <crazyra...@aol.com> wrote:
> > Hey guys,
>
> > Page in question:www.my-walk.com/addwalk.php
>
> > Im using the following code to put my markers into a string so they can be
> > easily stored in a databse:
>
> >  for (i = 0; (i < markersArray.length); i++)
> >       {
> >         markerString += markersArray[i].getPosition().toString() + "," ;
> >       }
>
> > With this code I usually get the following example of String:
>
> > (54.65495130292765, -3.354217564526365),(54.66627060518853,
> > -3.3509559983642556),(54.659121939101354,
> > -3.331043278637693),(54.65495130292765,
> > -3.341686284008787),(54.65922123474505,
> > -3.3641739243896462),(54.66279571629887, -3.3578224534423806),
>
> > Once I pull this String back out of MySql, how do I go about converting it
> > back to Markers?
>
> How do you add your markers to start with?
>
>  addMarker(event.latLng);
>
> event.latLng is a google.maps.LatLng object, you need to change your
> string (which is a sequence of latitude and longitude values as
> strings) into pairs of numbers (latitude and longitude) and pass those
> into the google.maps.LatLng constructor.http://code.google.com/apis/maps/documentation/javascript/reference.h...
>
> Then use those resulting google.maps.LatLng objects as the argument to
> call addMarker.

Another option would be to return them from your database a json or
xml (or even kml), and then process that to recreate the markers.
That would be the way I would do it.

-- Larry

>
>
>
>
> > Also, I have found that every so often the Google Map won't display on my
> > page, instead it is just a grey area where the map should be. To fix this I
> > have to close Firefox and Reload all the open tabs.
>
> > Can anyone tell me why this is?
>
> > I Appreciate any help guys,
>
> > Rick- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -

Rick Donohoe

unread,
Feb 16, 2011, 12:27:06 PM2/16/11
to google-map...@googlegroups.com
Yes it is, to be exact:

function addMarker(location) {
          marker = new google.maps.Marker({
            position: location,
            map: map
          });      
          markersArray.push(marker);     
        } 

I remember seeing the coding I could do with somewhere, im just unsure of how to do actually convert the string to pairs of which I could pass back through the addMarker(-LatLng Pair-) method.

I asssume its take String.
Create a temporary String up to the "," point.
Pass this through addMarker(temporary String).
Repeat until ive reached the last "," point.

Does this sound/look right, and if so any tips or reference to manipulating the String like this?

Did you have any opinions on the map occasionaly not loading part Larry?

Thanks for your time so far,
Rick

geoco...@gmail.com

unread,
Feb 16, 2011, 1:21:14 PM2/16/11
to Google Maps JavaScript API v3
On Feb 16, 9:27 am, Rick Donohoe <crazyra...@aol.com> wrote:
> Yes it is, to be exact:
>
> function addMarker(location) {
>           marker = new google.maps.Marker({
>             position: location,
>             map: map
>           });      
>           markersArray.push(marker);      
>         }  
>
> I remember seeing the coding I could do with somewhere, im just unsure of
> how to do actually convert the string to pairs of which I could pass back
> through the addMarker(-LatLng Pair-) method.
>
> I asssume its take String.
> Create a temporary String up to the "," point.

Be sure to convert the resulting latitude and longitude into numbers
(parseFloat will work).

-- Larry

Rick Donohoe

unread,
Feb 17, 2011, 1:06:40 PM2/17/11
to Google Maps JavaScript API v3
Ok heres where Ive reached...

Im using a defined marker string becuase I was having issues carrying
the marker String as a session variable into the mySQL and back, but
that doesn't relate to this group.

function addMap(coordinates) {

var coordinateString = "(18.66774725247165, -3.3367449040771535),
(54.6671516, -3.3574301),(54.6750929147338, -3.3477312322021535),
(54.6750929147338, -3.377256989038091),(54.667052323738794,
-3.393908142602544),(54.6671516, -3.3574301),(54.6671516,
-3.3574301),";

coordinateString = coordinateString.replace(/[(]/g, "");
coordinateString = coordinateString.replace(/[)],/g, "&");

var i = 0;
var temp = [];
var temp2 = [];

//split marker string into individual markers
temp = coordinateString.split("&");

//split first marker into lat and lng values
temp2 = temp[0].split(",");
var lat = temp2[0].parseFloat();
alert(lat);
var lng = temp2[1].parseFloat();
alert(lng);

map.setCenter(lat, lng);

marker = new google.maps.Marker({
position: new google.maps.LatLng(lat, lng),
map: map
});
marker.setMap(map);

// -1 gets rid of the last string which is empty space
// splits rest of markers into lat lng
for (i = 1; i < temp.length-1; i++) {

temp2 = temp[i].split(",");
var lat = temp2[0].parseFloat();
var lng = temp2[1].parseFloat();

map.setCenter(lat, lng);

marker = new google.maps.Marker({
position: new google.maps.LatLng(lat, lng),
map: map
});
marker.setMap(map);
}
}

The issue is alert(lat) works, alert(lng) doesn't.
Why is this?
Also can you see any other problems??

Thankyou very much,

Rick
Reply all
Reply to author
Forward
0 new messages