0

我刚注册寻求谷歌地图的帮助。我正在做一个项目,该项目允许我在我的网站上的谷歌地图中插入各种房屋广告。我不知道为什么那个位置永远不正确,起初我尝试使用下面的代码......

然后我不得不删除变量城市、街道、州和邮政编码,他们为什么要开战。但没有解决任何问题。因此,请转到链接以更好地了解情况。生成的代码位于地图 div 顶部的脚本标记中。

关联

<script>var defaultmapcenter = {mapcenter: "<?php echo $ct_options['ct_home_map_center']; ?>"}; google.maps.event.addDomListener(window, 'load', function(){ estateMapping.init_property_map(property_list, defaultmapcenter); });</script>

<script>
            var property_list = [];
            var default_mapcenter = [];

            <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); $count++; ?>

                var property = {
                    thumb: "<?php ct_first_image_tn_map() ?>",
                    price: "<?php currency(); ?><?php map_pin_price(); ?>",
                    fullPrice: "<?php currency(); ?><?php listing_price(); ?>",
                    bed: "<?php beds(); ?>",
                    bath: "<?php baths(); ?>",
                    size: "<?php echo get_post_meta($post->ID, "_ct_sqft", true); ?> <?php sqftsqm(); ?>",
                    street: "<?php the_title(); ?>",
                    city: "<?php city(); ?>",
                    state: "<?php state(); ?>",
                    zip: "<?php zipcode(); ?>",
                    latlong: "<?php echo get_post_meta(get_the_ID(), "_ct_latlng", true); ?>",
                    permalink: "<?php the_permalink(); ?>",
                    agentThumb: "<?php echo get_template_directory_uri(); ?>/img_resize/timthumb.php?src=<?php the_author_meta('ct_profile_url'); ?>&w=40&zc=1'",
                    agentName: "<?php the_author_meta('first_name'); ?> <?php the_author_meta('last_name'); ?>",
                    agentTagline: "<?php if(get_the_author_meta('tagline')) { the_author_meta('tagline'); } ?>",
                    agentPhone: "<?php if(get_the_author_meta('office')) { the_author_meta('office'); } ?>",
                    agentEmail: "<?php if(get_the_author_meta('email')) { the_author_meta('email'); } ?>",
                    isHome: "<?php if(is_home()) { echo "false"; } else { echo "true"; } ?>",
                    commercial: "<?php if(has_type('commercial')) { echo 'commercial'; } ?>"
                }
                property_list.push(property);

        <?php     
            endwhile; endif;
            wp_reset_query();
        ?>
            </script>
            <script>var defaultmapcenter = {mapcenter: "<?php echo $ct_options['ct_map_center']; ?>"}; google.maps.event.addDomListener(window, 'load', function(){ estateMapping.init_property_map(property_list, defaultmapcenter); });</script>



**Javascript:**
---------------

    var estateMapping = (function () {
        var self = {},
            marker_list = [],
            open_info_window = null,
            x_center_offset = 0, // x,y offset in px when map gets built with marker bounds
            y_center_offset = -100,
            x_info_offset = 0, // x,y offset in px when map pans to marker -- to accomodate infoBubble
            y_info_offset = -100;

        function build_marker(latlng, property) {
            var marker = new MarkerWithLabel({
                map: self.map, 
                draggable: false,
                flat: true,
                labelContent: property.price,
                labelAnchor: new google.maps.Point(22, 0),
                labelClass: "label", // the CSS class for the label
                labelStyle: {opacity: 1},
                icon: 'wp-content/themes/reale/images/blank.png',   
                position: latlng
                });

                self.bounds.extend(latlng);
                self.map.fitBounds(self.bounds);
                self.map.setCenter(convert_offset(self.bounds.getCenter(), x_center_offset, y_center_offset));

                var infoBubble = new InfoBubble({
                    maxWidth: 275,
                    content: contentString,
                    borderRadius: 4,
                    disableAutoPan: true
                });

                var residentialString = '';
                if(property['commercial'] != 'commercial') {
                    var residentialString='<p class="details">'+property.bed+'&nbsp;'+property.bath+'';
                }

                var contentString =
                '<div class="info-content">'+
                '<a href="'+property.permalink+'"><img class="left" src="'+property.thumb+'" /></a>'+
                '<div class="listing-details left">'+
                '<h3><a href="'+property.permalink+'">'+property.street+'</a></h3>'+
                '<p class="location">'+property.city+', '+property.state+'&nbsp;'+property.zip+'</p>'+
                '<p class="price"><strong>'+property.fullPrice+'</strong></p>'+residentialString+', '+property.size+'</p></div>'+
                '</div>';

                var tabContent =
                '<div class="info-content">'+
                '<img class="left" src="'+property.agentThumb+'" />'+
                '<div class="listing-details left">'+
                '<h3>'+property.agentName+'</h3>'+
                '<p class="tagline">'+property.agentTagline+'</p>'+
                '<p class="phone"><strong>Tel:</strong> '+property.agentPhone+'</p>'+
                '<p class="email"><a href="mailto:'+property.agentEmail+'">'+property.agentEmail+'</a></p>'+
                '</div>'+
                '</div>';

                infoBubble.addTab('Details', contentString);
                infoBubble.addTab('Contact Agent', tabContent);

                google.maps.event.addListener(marker, 'click', function() {
                    if(open_info_window) open_info_window.close();

                    if (!infoBubble.isOpen()) {
                        infoBubble.open(self.map, marker);
                        self.map.panTo(convert_offset(this.position, x_info_offset, y_info_offset));
                        open_info_window = infoBubble;
                    }
                });
        }

        function geocode_and_place_marker(property) {
           var geocoder = new google.maps.Geocoder();
           var address = property.street+', '+property.city+' '+property.state+', '+property.zip;

               //If latlong exists build the marker, otherwise geocode then build the marker
               if (property['latlong']) {
                   var lat = parseFloat(property['latlong'].split(',')[0]),
                        lng = parseFloat(property['latlong'].split(',')[1]);
                    var latlng = new google.maps.LatLng(lat,lng);
                    build_marker(latlng, property);

               } else {
                   geocoder.geocode({ address : address }, function( results, status ) {
                       if(status == google.maps.GeocoderStatus.OK) {
                            var latlng = results[0].geometry.location;
                            build_marker(latlng, property);
                        }
                    });
                }
        }

        function init_canvas_projection() {
            function CanvasProjectionOverlay() {}
            CanvasProjectionOverlay.prototype = new google.maps.OverlayView();
            CanvasProjectionOverlay.prototype.constructor = CanvasProjectionOverlay;
            CanvasProjectionOverlay.prototype.onAdd = function(){};
            CanvasProjectionOverlay.prototype.draw = function(){};
            CanvasProjectionOverlay.prototype.onRemove = function(){};

            self.canvasProjectionOverlay = new CanvasProjectionOverlay();
            self.canvasProjectionOverlay.setMap(self.map);
        }

        function convert_offset(latlng, x_offset, y_offset) {
            var proj = self.canvasProjectionOverlay.getProjection();
            var point = proj.fromLatLngToContainerPixel(latlng);
            point.x = point.x + x_offset;
            point.y = point.y + y_offset;
            return proj.fromContainerPixelToLatLng(point);
        }

        self.init_property_map = function (properties, defaultmapcenter) {
            var options = {
                zoom: 1,
                center: new google.maps.LatLng(defaultmapcenter.mapcenter),
                mapTypeId: google.maps.MapTypeId.ROADMAP, 
                disableDefaultUI: true,
                streetViewControl: false
            };

            self.map = new google.maps.Map( document.getElementById( 'map' ), options );
            self.bounds = new google.maps.LatLngBounds();
            init_canvas_projection();

            //wait for idle to give time to grab the projection (for calculating offset)
            var idle_listener = google.maps.event.addListener(self.map, 'idle', function() {
                for (i=0;i<properties.length;i++) {
                    geocode_and_place_marker(properties[i]);
                }
                google.maps.event.removeListener(idle_listener);
            });

        }

        return self;
    }());
4

3 回答 3

1

我从您的实时站点中获取了几个属性对象并简化了代码。这似乎可以正确定位标记。您的问题中有太多代码无法准确说明您出错的地方,但是如果您从这个演示开始,您可以慢慢添加您的功能并查看它在哪里中断。

演示: jsFiddle

输出:

输出

脚本:

var property_list = [
        {latlong: "36.738884,15.022705"},
        {latlong: "42.608127,14.067408"}
    ],
    options = {
        zoom: 4,
        center: new google.maps.LatLng( 36.73, 15.02 ),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    },
    map = new google.maps.Map( 
        document.getElementById( 'map-canvas' ), 
        options 
    );

for( var index = 0; index < property_list.length; index++ ) {
    var latlong = property_list[index]['latlong'].split(','),
        latlng = new google.maps.LatLng( latlong[0], latlong[1] ),
        marker = new google.maps.Marker( {position: latlng, map: map} );
    marker.setMap( map );
};

HTML:

<script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<div id="map-canvas"></div>

CSS:

#map-canvas { 
    height: 300px; 
    width: 500px;
}
于 2013-03-02T06:35:39.263 回答
0

您的标记和javascript代码没有任何问题,所有(除了一个)标记都放置在正确的位置。

问题是你的内容。您可能已经注意到 265k 标记的阴影比 1k 标记要暗得多。这是因为在该标记后面是 8 个其他标记,您的 9 个标记是用相等的 LatLng 定义的

只有 1 个标记未显示,它是带有空 latlong 的标记。由于缺少属性street,city,statezip.

于 2013-03-02T06:54:52.303 回答
0

替换这一行

var address = property.street+', '+property.city+' '+property.state+', '+property.zip+', '+property.country;

使用这些行删除具有空值的字段

        var city    = (property.city.trim() != '')?property.city.trim()+',':'';
        var state   = (property.state.trim() != '')?property.state.trim()+',':''; 
        var country = (property.country.trim() != '')?property.country.trim():'';
        var address = city+state+country;
于 2015-09-15T14:38:45.837 回答