使用Google Maps API根据地址显示住宅的街景视图

时间:2014-07-08 06:26:47

标签: google-maps google-street-view

我正在尝试使用Google地图根据地址显示房屋的街景。我已根据此jsfiddle创建了tutorial。小提琴正在显示默认的初始地址,但我无法弄清楚当按下按钮时如何将新地址传递到街景视图代码。

这是HTML:

<h3>Enter an Address to view the street view</h3>
<form action="/" method="POST">
    <textarea name='new_address' class="address">150 Glen Road, Toronto</textarea>
    <input type="submit" id="change_street" value="Change Street View Address" />
</form>
<div class="map_container">
    <div id="map_canvas_cont">
        <div id="map_canvas"></div>
    </div>
    <div id="pano_cont">
        <div id="pano"></div>
    </div>
</div>

这是javascript:

var panorama;
var addLatLng;
var showPanoData;
var panorama;

function load_map_and_street_view_from_address(address) {              
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({
        'address': address
    }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var gps = results[0].geometry.location;
            create_map_and_streetview(gps.lat(), gps.lng(), 'map_canvas', 'pano');
        }
    });
}

function create_map_and_streetview(lat, lng, map_id, street_view_id) {

    panorama = new google.maps.StreetViewPanorama(document.getElementById("pano"));
    addLatLng = new google.maps.LatLng(lat, lng);
    var service = new google.maps.StreetViewService();
    service.getPanoramaByLocation(addLatLng, 50, showPanoData);

    var myOptions = {
        zoom: 14,
        center: addLatLng,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        backgroundColor: 'transparent',
        streetViewControl: false,
        keyboardShortcuts: false
    }

    var map = new google.maps.Map(document.getElementById(map_id), myOptions);
    var marker = new google.maps.Marker({
        map: map,
        animation: google.maps.Animation.DROP,
        position: addLatLng
    });

}

function showPanoData(panoData, status) {
    if (status != google.maps.StreetViewStatus.OK) {
        $('#pano').html('No StreetView Picture Available').attr('style', 'text-align:center;font-weight:bold').show();
        return;
    }
    var angle = computeAngle(addLatLng, panoData.location.latLng);

    var panoOptions = {
        position: addLatLng,
        addressControl: false,
        linksControl: false,
        panControl: false,
        zoomControlOptions: {
            style: google.maps.ZoomControlStyle.SMALL
        },
        pov: {
            heading: angle,
            pitch: 10,
            zoom: 1
        },
        enableCloseButton: false,
        visible: true
    };

    panorama.setOptions(panoOptions);
}

function computeAngle(endLatLng, startLatLng) {
    var DEGREE_PER_RADIAN = 57.2957795;
    var RADIAN_PER_DEGREE = 0.017453;

    var dlat = endLatLng.lat() - startLatLng.lat();
    var dlng = endLatLng.lng() - startLatLng.lng();
    var yaw = Math.atan2(dlng * Math.cos(endLatLng.lat() * RADIAN_PER_DEGREE), dlat) * DEGREE_PER_RADIAN;
    return wrapAngle(yaw);
}

function wrapAngle(angle) {
    if (angle >= 360) {
        angle -= 360;
    } else if (angle < 0) {
        angle += 360;
    }
    return angle;
}

$(document).ready(function () {
    load_map_and_street_view_from_address("150 Glen Road, Toronto");

    $('#change_street').click(function () {
       alert("How do I pass the new address to the street view code?");
    });

});

有人可以向我解释如何解决此问题jsfiddle,以便我可以按下按钮传递新地址吗?谢谢。

1 个答案:

答案 0 :(得分:3)

可能有多种解决方案,我建议存储地图,标记和全景实例,然后您只需要在进一步调用时更新其属性,而不是创建新实例。

按钮点击调用该功能:

load_map_and_street_view_from_address($('textarea[name="new_address"]').val());

替换create_map_and_streetview&amp; showPanoData

function create_map_and_streetview(lat, lng, map_id, street_view_id) {
    var goo=google.maps,
        map=$('#'+map_id),
        pano=document.getElementById("pano"),
        addLatLng = new goo.LatLng(lat, lng),
        myOptions = {
        zoom: 14,
        center: addLatLng,
        mapTypeId: goo.MapTypeId.ROADMAP,
        backgroundColor: 'transparent',
        streetViewControl: false,
        keyboardShortcuts: false
    }
    if(!map.data('gmap')){
      //store the instances per $.data
      map.data('gmap',new goo.MVCObject());
      map.data('gmap').set('panorama',new goo.StreetViewPanorama(pano));
      map.data('gmap').set('service',new goo.StreetViewService());
      map.data('gmap').set('map',new goo.Map(map[0], myOptions));
      map.data('gmap').set('marker',new goo.Marker({
                                                  map: map.data('gmap').get('map'),
                                                  animation: goo.Animation.DROP,
                                                  position: addLatLng
                                                })
                      );
    }else{
      map.data('gmap').get('map').setCenter(addLatLng);
      map.data('gmap').get('marker').setPosition(addLatLng);
      //always create a new panorama
      //otherwise the panorama will be broken as soon as there is no picture available 
      map.data('gmap').set('panorama',new goo.StreetViewPanorama(pano));
    }

    map.data('gmap').get('service')
      .getPanoramaByLocation(addLatLng, 50, function(panoData, status){ 
         if (status != google.maps.StreetViewStatus.OK) {
            $('#pano').html('No StreetView Picture Available')
              .attr('style', 'text-align:center;font-weight:bold').show();
            return;
        }
      var angle = computeAngle(addLatLng, panoData.location.latLng);

      var panoOptions = {
        position: addLatLng,
        addressControl: false,
        linksControl: false,
        panControl: false,
        zoomControlOptions: {
            style: google.maps.ZoomControlStyle.SMALL
        },
        pov: {
            heading: angle,
            pitch: 10,
            zoom: 1
        },
        enableCloseButton: false,
        visible: true
      };

      map.data('gmap').get('panorama').setOptions(panoOptions);
    });

}

演示:http://jsfiddle.net/R59mB/4/