Trouble passing markers

26 views
Skip to first unread message

Rick Donohoe

unread,
Jan 24, 2011, 7:34:42 AM1/24/11
to google-map...@googlegroups.com
Hey, Im trying to pass my markers array from one map o another map on the nextpage.

Link is http://my-walk.com/addwalk.php

Ive seen (and been told) of a few ways odf doing it, but have gone through with the one I found least confusing.

On the first page I have:

function saveMarkers()
    {     
      var packed = "";
      for (i = 0; (i < markersArray.length); i++) {
        if (i > 0) {
          packed += ",";
        }
        packed += escape(markersArray[i]);
      }
      document.markersArray.markersArray.value = packed;
      document.markersArray.submit();
    }

With Form:

<form name ="markersArray" action="addwalk2.php" method="post">
        <input type="hidden" name="markersArray" />
      </form>
      <a href="javascript:saveMarkers();">Proceed to Step 2</a>




On the next page:

   .....
$packed = $_POST['markersArray'];
  $markers = split(",", $packed);
  for ($i = 0; ($i < count($markers)); $i++) {   
    $markers[$i] = rawurldecode($markers[$i]);
    $markers[$i] = str_replace("\\", "\\\\", $markers[$i]);   
    $markers[$i] = str_replace("\"", "\\\"", $markers[$i]);
  }
?>
<script>
  var markersArrray = new Array (
<?php
  for ($i = 0; ($i < count($markers)); $i++) {
    if ($i > 0) {
      echo ",\n";
    }
    echo "    \"";
    echo $markers[$i];
    echo "\"";
  }
.......

I then have a function addMap() which is supposed to add these markers to the map on this second page:

function addMap() {

for (i in markersArray) {
          markersArray[i].setMap(map);
        }
}

But when I click on view source, there seems to be an issue with
var markersArrray = new Array (
object Object
object Object
)

Anyone have any ideas what the issue is here?

Thanks for your help guys,
Rick

Rossko

unread,
Jan 24, 2011, 9:25:00 AM1/24/11
to Google Maps JavaScript API v3
> Hey, Im trying to pass my markers array from one map to another map on the
> nextpage.

Without the context of your page, we'll have to make some guesses.
This is why the posting guidelines ask for a link.

>         packed += escape(markersArray[i]);

Let's guess that markersArray is an array of API marker objects.
'packed' will not represent them accurately as a bunch of strings
(have you inspected 'packed? I can't because I can't access your
page ...) I rather think it will just tell you that it is an object.

>     echo $markers[$i];

Okay, so you have php outputting some string values. You know they
are strings because you can only get strings from the output of an
echo.

>           markersArray[i].setMap(map);

These are strings ; they don't have a setMap method.

You can't pass javascript objects like this using GET/POST and php ;
what you can do is pass a bunch of strings representing key values of
your markers, and then have your second page examine these strings and
use them to reconstruct new marker objects.

Rick Donohoe

unread,
Jan 24, 2011, 9:54:28 AM1/24/11
to google-map...@googlegroups.com
Ok page 1 -

http://www.my-walk.com/addwalk.php

Page 2 -

http://www.my-walk.com/addwalk2.php

Bascially, I want to add markers on page 1 by clicking on the map, then press the 'proceed to page 2' link.
On page 2, I want to press 'tester' and see those same markers come up on the map.

The tester part is just to see that I can get them through the pages, ideally I want to pass it through 3 or 4 pages and then submit it to a MySQL database.

Hope that helps,
cheers for your time

Rick Donohoe

unread,
Jan 24, 2011, 10:00:46 AM1/24/11
to google-map...@googlegroups.com
Also, I remember the reason I tried this method is that I tried your

foreach(.....

method and I got an error with that just saying invalid argument.
Do you want me to re do the code back to this way, and post it back up with the error, then you could mayb help me there?

Paulo Fernandes

unread,
Jan 24, 2011, 7:53:22 AM1/24/11
to Google Maps JavaScript API v3
Hi

When you were adding the markers into a hidden field, try to get
marker.getPosition()

I didn't try but I think this will solve your problem.

[]s


On Jan 24, 10:34 am, Rick Donohoe <crazyra...@aol.com> wrote:
> Hey, Im trying to pass my markers array from one map o another map on the
> nextpage.
>
> Link ishttp://my-walk.com/addwalk.php

Paulo Fernandes

unread,
Jan 24, 2011, 9:55:32 AM1/24/11
to google-map...@googlegroups.com
Hi

My guess is try to get the position of the marker to put inside the hidden field

Something like this

marker.getPosition()

[]s


--
You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
To post to this group, send email to google-map...@googlegroups.com.
To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.


Rossko

unread,
Jan 24, 2011, 3:48:34 PM1/24/11
to Google Maps JavaScript API v3
> http://www.my-walk.com/addwalk.php

Map not working for me. Do you think these lines
var initialLocation = university;
var university = new google.maps.LatLng(52.94, -1.195);
might work better the other way round?


> The tester part is just to see that I can get them through the pages,
> ideally I want to pass it through 3 or 4 pages and then submit it to a MySQL
> database.
>
> Hope that helps,

Doesn't help me , but then I'm not the one with the problem. Does it
help you to solve it?

Here's an example of reading data from a MySQL database via php and
showing on a map
http://code.google.com/apis/maps/articles/phpsqlajax_v3.html

Here's an exmple of passing info to a MySQL database via php
http://code.google.com/apis/maps/articles/phpsqlinfo_v3.html

You can use the techniques there to push your data around from a
webpage to php and back to another webpage, without actually going
into MySQL in the interim. You don't have to use XML if you don't
want, it's only a means of shuffling string data around.

I think the bit you need to grasp is that you cannot just push a
javascript object from one webpage to another. You need a means to
transfer the details about the markers that you are really interested
in - marker position and colour, say - and have your second webpage
make its own objects based on that data.

You might do _something_ like
for (i = 0; (i < markersArray.length); i++) {
...
// where markersArray[i] is an API marker object
somedata += markersArray[i].getPosition().toString() + "," ;
...
}
to build up your collection of marker data before POSTing it to php.
You could in fact assemble your data as an array of marker details if
you wanted, that might make it easier to handle at the php end. It's
up to you to design the data interchange as you want it, but we might
imagine something like
[ 22.57, 71.0 , 'blue' , 'Descriptive text' ] ,
[ 30.6 , 69.5 , 'red' , 'More description' ]

> Also, I remember the reason I tried this method is that I tried your
> foreach(.....

Who "you"? I don't think you should use for-each at all, it often
produces unexpected results when used in an object-oriented
environment.

None of this is maps specific at all, it's about how to transfer
javascript values to php and vice-versa.

By the by, I looked into duplicate 'name' attributes in HTML tags,
<form name ="markersArray" ....
<input type="hidden" name="markersArray" ...
and it seems that is likely to confuse some scripts e.g.
document.getElementById('markersArray') is going to return an
indeterminate element in IE browser. I think it falls under "not
forbidden but unwise".

Reply all
Reply to author
Forward
0 new messages