D
D
Dmitry2015-11-17 18:39:25
Google
Dmitry, 2015-11-17 18:39:25

What could be causing this error in Google Maps API 3?

Experimental site
The site implemented Google maps via API 3. On Monday, all the markers were in place and showed information, today everything is empty. The site is powered by WPress. The console writes an error: Uncaught SyntaxError: Unexpected token , . When you click on the error, the following line appears:

<script>
            var flat = {
                name: "1 комн. кв. на Ленина 18",
        coord: new google.maps.LatLng(, ),
                href: "http://kv43sutki.ru/?p=118",
                marker:null,
                iw:null
            }

            flat.marker     = new google.maps.Marker({ position: flat.coord, map: map, title: flat.name });
            flat.iw         = new google.maps.InfoWindow({ content: flat.name+"<br/><a href='" + flat.href + "' target=\"_blank\">Подробнее...</a>" });

            arrFlats.push(flat);
        </script>

I use this template to display the map on the page:
<div class="content-wrapper without-featured-image">
<script>
    var arrFlats = [];
</script>
     <div id="map_canvas" style="width:100%; height:500px"></div>

    <script type="text/javascript" charset="utf-8">
        var map;
    
        function initialize(arrFlats) {
            arrMark = [];
        var latlng = new google.maps.LatLng(58.609079, 49.670920);
        var myOptions = {
            zoom: 13,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    
        for(var i=0; i<arrFlats.length; i++) {
                var point = arrFlats[i].coord;


                var marker = new google.maps.Marker({ position:  arrFlats[i].coord, map: map, title:  arrFlats[i].name });
                    
                var contentString = arrFlats[i].name+"<br/><a href='" + arrFlats[i].href + "' target=\"_blank\"><?php print(iconv("windows-1251", "UTF-8", "подробнее...")) ?></a>";
                makeInfoWin(marker, contentString, map);
            }

        }

        function makeInfoWin(marker, data) {
            var infowindow = new google.maps.InfoWindow({ content: data });
      infowindow.open(map,marker);
            google.maps.event.addListener(marker, 'click', function() {
                
            });  

        }
    
    </script>

<?php
$args = array(
        'numberposts'       => 100,
        'orderby'           => 'post_date',
        'order'             => 'DESC',
        'post_type'         => 'post',
        'post_status'       => 'publish',
        'suppress_filters'  => true
    );
$posts = get_posts($args );

    $r = curl_init(); 
    curl_setopt($r, CURLOPT_NOPROGRESS, 0); 
    curl_setopt($r, CURLOPT_RETURNTRANSFER, 1); 
    
foreach( $posts as $post ){
        
    setup_postdata($post);

    $addrs = get_post_meta($post->ID, 'city', true).','.get_post_meta($post->ID, 'street', true).','.get_post_meta($post->ID, 'house', true);           
  $html_query = "http://maps.googleapis.com/maps/api/geocode/json?address=".$addrs."&sensor=false&language=ru";
    curl_setopt($r, CURLOPT_URL, $html_query); 


    $html_res1 = curl_exec($r); 
  sleep(1);
  $html_res1 = file_get_contents(urldecode($html_query));   
    
  
  
  //print_r($html_res1);
  //die();
  $cart = json_decode( $html_res1 );
  $coordinates_lat = $cart->results[0]->geometry->location->lat;
    $coordinates_lng = $cart->results[0]->geometry->location->lng;

   //print("$addr1251 $coordinates_lat $coordinates_lng<br/>"); 
    
    
?>


    <script>
            var flat = {
                name: "<?php print($post->post_title) ?>",
        coord: <?php print("new google.maps.LatLng($coordinates_lat, $coordinates_lng)") ?>,
                href: "<?php print ($post->guid) ?>",
                marker:null,
                iw:null
            }

            flat.marker     = new google.maps.Marker({ position: flat.coord, map: map, title: flat.name });
            flat.iw         = new google.maps.InfoWindow({ content: flat.name+"<br/><a href='" + flat.href + "' target=\"_blank\">подробнее...</a>" });

            arrFlats.push(flat);
        </script>


      <?php

}
wp_reset_postdata();

           ?>
<script>
    initialize(arrFlats);

</script>



    </div>

What could be the reason why the coordinates are not transmitted

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Lavrentiev, 2015-11-17
@lavrentiev

Check if your handler gives out the coordinates, and then check if everything is correct when receiving them in the JS code.
but in general, the advice is to use api.jquery.com/jquery.getjson and create new labels already with the result obtained and not like you have right now!

D
Dmitry, 2015-11-17
@xavibeat_dk

Here is the answer to the address: Kirov, Enthusiastov, 4

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question