Google Places API不会为某些地方标记路线

时间:2013-12-14 18:58:24

标签: google-maps google-maps-api-3 google-places-api

我正在使用Google Places API标记2个地点之间的路线。它适用于大多数路线。但是,它并没有为少数路线标记路线。这是因为“directionsService.route”请求返回某些路由的“ZERO_RESULTS”状态。以下是它无法正常工作的输入之一。

来源A:

South End Circle, Basavanagudi, Bangalore, Karnataka, India

目的地B:

Jayanagar 4 Block, Jayanagar, Bangalore, Karnataka, India

中间点C:

Tata Silk Farm, Jayanagar, Bangalore, Karnataka, India

以下是代码:

<html>
<head>
<style type="text/css">
               body {
                       font-family: sans-serif;
                       font-size: 14px;
               }
</style>
       <title>Google Maps JavaScript API v3 Example: Places Autocomplete</title>
       <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>

        <script src="http://maps.googleapis.com/maps/api/js?sensor=false&amp;libraries=places,geometry" type="text/javascript"></script>

        <script type="text/javascript">
            var locations = new Array();
            var directionsDisplay;
            var directionsService = new google.maps.DirectionsService();

            function initialize1() {
                directionsDisplay = new google.maps.DirectionsRenderer();
                var chicago = new google.maps.LatLng(41.850033, -87.6500523);
                var mapOptions = {
                    zoom: 7,
                    center: chicago
                }
                map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
                directionsDisplay.setMap(map);
            }

            function initialize() {
                //routeBoxer = new RouteBoxer();

                var input = document.getElementById('searchTextFieldSource');
                var input1 = document.getElementById('searchTextFieldDestination');
                var input3 = document.getElementById('searchTextFieldIntermediate');

                var autocomplete = new google.maps.places.Autocomplete(input);
                var autocomplete1 = new google.maps.places.Autocomplete(input1);
                var autocomplete3 = new google.maps.places.Autocomplete(input3);
                google.maps.event.addListener(autocomplete3, 'place_changed', function () {
                    var place1 = autocomplete.getPlace();
                    document.getElementById('city1').value = place1.name;
                    var place1Lat = place1.geometry.location.lat();
                    var place1Lng = place1.geometry.location.lng();
                    document.getElementById('cityLat1').value = place1Lat;
                    document.getElementById('cityLng1').value = place1Lng;

                    var obj = new Object();
                    obj.city = place1.name;
                    obj.latitude = place1.geometry.location.lat();
                    obj.longitude = place1.geometry.location.lng();
                    locations.push(obj);


                    var place2 = autocomplete1.getPlace();
                    document.getElementById('city2').value = place2.name;
                    var place2Lat = place2.geometry.location.lat();
                    var place2Lng = place2.geometry.location.lng();
                    document.getElementById('cityLat2').value = place2Lat;
                    document.getElementById('cityLng2').value = place2Lng;

                    var obj = new Object();
                    obj.city = place2.name;
                    obj.latitude = place2.geometry.location.lat();
                    obj.longitude = place2.geometry.location.lng();
                    locations.push(obj);

                    //For intermediate point
                    var place3 = autocomplete3.getPlace();
                    document.getElementById('city3').value = place1.name;
                    var place3Lat = place3.geometry.location.lat();
                    var place3Lng = place3.geometry.location.lng();
                    document.getElementById('cityLat3').value = place3Lat;
                    document.getElementById('cityLng3').value = place3Lng;

                    directionsDisplay = new google.maps.DirectionsRenderer();
                    var startPlace = new google.maps.LatLng(place1Lat, place1Lng);

                    var mapOptions = {
                        zoom: 7,
                        center: startPlace
                    }

                    var map = new google.maps.Map(document.getElementById('map'), mapOptions);
                    directionsDisplay.setMap(map);

                    var start = $("#city1").val();
                    var end = $("#city2").val();

                    var request = {
                        origin: start,
                        destination: end,
                        travelMode: google.maps.TravelMode.DRIVING
                    };

                    var position = new google.maps.LatLng(place3Lat, place3Lng);

                    directionsService.route(request, function (result, status) {
                        if (status == google.maps.DirectionsStatus.OK) {
                            directionsDisplay.setDirections(result);

                            if (google.maps.geometry.poly.isLocationOnEdge(position,
                                new google.maps.Polyline({ path: google.maps.geometry.encoding.decodePath(result.routes[0].overview_polyline.points) }),
                                0.0050000000)) {
                                alert("within polyline");
                            }
                            else {
                                alert("not in polyline");
                            }

                            // Box around the overview path of the first route
                            /*var path = result.routes[0].overview_path;
                            boxes = routeBoxer.box(path, 0.25);
                            drawBoxes();
                            findPlaces(0);*/
                        }
                    });
                });
            }
            google.maps.event.addDomListener(window, 'load', initialize);

            function refreshMap(locations) {
                google.maps.visualRefresh = true;
                var map = new google.maps.Map(document.getElementById('map'), {
                    zoom: 10,
                    center: new google.maps.LatLng(locations[0].latitude, locations[0].longitude),
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                });

                var infowindow = new google.maps.InfoWindow();
                var marker, i;

                for (i = 0; i < locations.length; i++) {
                    marker = new google.maps.Marker({
                        position: new google.maps.LatLng(locations[i].latitude, locations[i].longitude),
                        map: map
                    });

                    google.maps.event.addListener(marker, 'click', (function (marker, i) {
                        return function () {
                            infowindow.setContent(locations[i].city);
                            infowindow.open(map, marker);
                        }
                    })(marker, i));
                }


            }
        </script>
        <body>
           <div>
                   <b>Source A:</b><input id="searchTextFieldSource" type="text" size="50" placeholder="Enter the source" autocomplete="on" runat="server" />  
                    <input type="text" id="city1" name="city1" />
                    <input type="text" id="cityLat1" name="cityLat1" />
                    <input type="text" id="cityLng1" name="cityLng1" />  
           </div>

            <div>
                   <b>Destination B:</b><input id="searchTextFieldDestination" type="text" size="50" placeholder="Enter the destination" autocomplete="on" runat="server" />  
                    <input type="text" id="city2" name="city2" />
                    <input type="text" id="cityLat2" name="cityLat2" />
                    <input type="text" id="cityLng2" name="cityLng2" />  
           </div>

            <div>
                   <b>Intermediate point C:</b><input id="searchTextFieldIntermediate" type="text" size="50" placeholder="Enter the destination" autocomplete="on" runat="server" />  
                    <input type="text" id="city3" name="city2" />
                    <input type="text" id="cityLat3" name="cityLat3" />
                    <input type="text" id="cityLng3" name="cityLng3" />  
           </div>

            <div id="map" style="width: 700px; height: 600px;"></div>
       </body>
</html>

以下是我提供上述位置时所获得的快照: places api problem

没有标记路线。可能是什么问题?

1 个答案:

答案 0 :(得分:1)

var start = $("#city1").val(); is "South End Circle"
var end = $("#city2").val(); is  "Jayanagar 4 Block"

路线服务部门无法找到这两个地点之间的路线。

也许您应该使用:

var start = place1.formatted_address;
var end = place2.formatted_address;

其中提供了您的问题表明您向路线服务提供的formatted_addresses:

  • South End Circle,Basavanagudi,Bangalore,Karnataka,India
  • Jayanagar 4 Block,Jayanagar,Bangalore,Karnataka,India
  • 塔塔丝绸农场,Jayanagar,班加罗尔,印度卡纳塔克邦

另一种选择是使用位置的坐标而不是格式化的地址。