1

I have home.php, on tag head load with jquery another page corpo_home.php:

<script type="text/javascript"> function Loadcorpo(P1){ $("#corpo").load("corpo_home.php?sq="+elecodice[P1]+"&an="+eleanno[P1]); } </script> 

On corpo_home.php I declare api key and function to load map:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=>123456&sensor=false"></script> <script type="text/javascript"> function initialize(P2){ var coordinate = P2.split(',') alert("before"); var latlng = new google.maps.LatLng(coordinate[0], coordinate[1]); alert("after"); var mapOptions = { center: latlng, zoom: 17, mapTypeId: google.maps.MapTypeId.HYBRID }; var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); var marker = new google.maps.Marker({ position: latlng, map: map, title:"" }); } }); </script> 

When the page index.php load I see alert "before" but not "after".

Can you help me?

2
  • Check developers console. Commented Dec 16, 2015 at 18:02
  • Where are you calling your initialize function? Commented Dec 16, 2015 at 18:07

2 Answers 2

1

I found many syntax errors:

  1. After the split, there's no ;.
  2. For some reason you closed the function with }); when only } is needed.
  3. The marker variable closes right after title:"" with }); so the curly bracket after it shouldn't be there.

Other than that, I don't see why it's not working. Please show where you're calling initialize();

For now, use this code instead:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=>123456&sensor=false"></script> <script type="text/javascript"> function initialize(lat, lng){ // edited on first edit alert("before"); var LatLng = { // edited on first edit lat: lat, lng: lng }; alert("after"); var mapOptions = { center: latlng, zoom: 17, mapTypeId: google.maps.MapTypeId.HYBRID }; var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); var marker = new google.maps.Marker({ position: latlng, map: map, title: "" }); } </script> 

- EDIT -

Alright, so you call the function with this:

<img onload="javascript:initialize('<?php echo $row_record['pzcoordi'];?>');" src="images/blank.gif"/> 

First, I would use a div instead of an img, but if it works, that's ok. I see your approach to pass a parameter and then split it on JavaScript. Pretty genius. But I see you are passing the parameter as a string. This won't work because the API expects them to be integer.

To diagnose better, I would love to see how you built those next and previous buttons, since they work fine, but I will just move on with my guess.

I suggest that you pass 2 parameters, the Lat and Lng coordinates, and to achieve that, you need to explode(); them in PHP:

<?php $array = explode(",", $row_record['pzcoordi']); $lat = $array[0]; $lng = $array[1]; ?> <img onload="initialize(<?php echo $lat; ?>, <?php echo $lng; ?>)" src="images/blank.gif"/> 

If this doesn't work for any reason, maybe you could wrap the parameters with single quotes to make them strings, but I wouldn't rely on that. And I know that explode(); returns strings, but they will only be strings for HTML and PHP, JavaScript will take them as integers because they won't be wrapped with single quotes. I hope this helps.

Sign up to request clarification or add additional context in comments.

8 Comments

@Roberta Please don't answer your own questions, comment on the answers like this. I can't comment on your question because I don't have enough reputation yet.
I tried using div instead img but never working. You're right about integers but also put fixed coordinates map load only the second time...
I made a small environment test to simplify web pages. Then I put map in the first program (home.php) and also the first time ( or when press F5 to refresh) work. The problem is put map in program corpo_home.php called by jquery. Why? Grrrrrrrr
Hummm problem is only on Chrome and Explorer, Firefox it's ok.
@Roberta Let me explain (and let me know if you prefer Italian, I'm Italian-Argentine). The map variable in the JavaScript will give the map options to any element with the id map_canvas. The only thing you need to do is create a <div> tag with that id, then you can add a class for CSS and in your JavaScript, add google.maps.event.addDomListener(window, 'load', initialize); after the function. And by the way, there is no need at all to use the key and function in another file
|
0

Thanks you for syntax corrections.

I call inizialize() in this way:

<img onload="javascript:initialize('<?php echo $row_record['pzcoordi'];?>');" src="images/blank.gif" /> 

The home.php have button previous and next, onclick corpo_home.php reload, read coordinates from database and show the maps.

The fist time map not load, if click on next button and then return back map load properly.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.