1

Googleマップを使用して2つの場所間のルートを見つけています.ルートの中心緯度と経度を取得したい. ルートの中心を取得する方法を教えてください。ルートを取得するために以下のコードを使用しています。var directionDisplay; var directionService; var stepDisplay; var markerArray = []; var infoWindow; 変数サービス; var lat1 = 0; var lng1;

    function pre_initialize() {
        var mapOptions = {
            zoom: 6,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function (position) {
                var pos = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
                var infowindow = new google.maps.InfoWindow({
                    map: map,
                    position: pos,
                    content: 'Current Location.'
                });
                map.setCenter(pos);
            }, function () {
                handleNoGeolocation(true);
            });
        } else {
            // Browser doesn't support Geolocation
            handleNoGeolocation(false);
        }
    }
    function handleNoGeolocation(errorFlag) {
        if (errorFlag) {
            var content = 'Error: The Geolocation service failed.';
        } else {
            var content = 'Error: Your browser doesn\'t support geolocation.';
        }

        var options = {
            map: map,
            position: new google.maps.LatLng(60, 105),
            content: content
        };

        var infowindow = new google.maps.InfoWindow(options);
        map.setCenter(options.position);
    }
    function initialize() {
        var mapOptions = {
            zoom: 6,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);                      
        var addressfrom = document.getElementById("from").value;
        var addressto = document.getElementById("to").value;
        var geocoder = new google.maps.Geocoder();
        var coords = new google.maps.LatLng(0, 0);
        alert(lat1);
        coords = geocoder.geocode({ 'address': addressfrom }, function (results, status) {              
            results[0].geometry.location.lat();
            results[0].geometry.location.lng();
        });

        directionsService = new google.maps.DirectionsService();
        // Create a renderer for directions and bind it to the map.
        var rendererOptions = {
            map: map
        }
        directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions)

        // Instantiate an info window to hold step text.
        infoWindow = new google.maps.InfoWindow();
        stepDisplay = new google.maps.InfoWindow();
        calcRoute();
        google.maps.event.addListenerOnce(map, 'bounds_changed', performSearch);
    }



    function performSearch() {               
        var request = {
            bounds: map.getBounds(),
            radius: 100,
            types: ['hospital', 'cafe', 'restaurant', 'food', 'bar'],
            keyword: 'best view'
        };
        service = new google.maps.places.PlacesService(map);
        //service.nearbySearch(request, callback);      
        service.radarSearch(request, callback);
        //service.textSearch(request, callback);
    }
    function callback(results, status) {           
        if (status != google.maps.places.PlacesServiceStatus.OK) {
            alert(status);
            return;
        }
        for (var i = 0, result; result = results[i]; i++) {
                createMarker(result);
        }
    }    
    function createMarker(place) {
        var marker = new google.maps.Marker({
            map: map,                
            position: place.geometry.location,
            icon: 
            {
                // Star
                path: 'M 0,-24 6,-7 24,-7 10,4 15,21 0,11 -15,21 -10,4 -24,-7 -6,-7 z',
                fillColor: '#ff0000',
                fillOpacity: 1,
                scale: 1 / 4,
                strokeColor: '#bd8d2c',
                strokeWeight: 1
            }
        });

        google.maps.event.addListener(marker, 'click', function () {

            service.getDetails(place, function (result, status) {
                if (status != google.maps.places.PlacesServiceStatus.OK) {
                    alert(status);
                    return;
                }

                infoWindow.setContent(result.name);
                infoWindow.open(map, marker);
            });
        });
    }

    function calcRoute() {

        // First, remove any existing markers from the map.
        for (var i = 0; i < markerArray.length; i++) {
            markerArray[i].setMap(null);
        }

        // Now, clear the array itself.
        markerArray = [];
        var start = document.getElementById('from').value;
        var end = document.getElementById('to').value;
        var request = {
            origin: start,
            destination: end,
            travelMode: google.maps.TravelMode.WALKING
        };
        directionsService.route(request, function (response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                var warnings = document.getElementById('warnings_panel');
                warnings.innerHTML = '<b>' + response.routes[0].warnings + '</b>';
                directionsDisplay.setDirections(response);
                showSteps(response);
            }
        });
    }

    function showSteps(directionResult) {         
        var myRoute = directionResult.routes[0].legs[0];

        for (var i = 0; i < myRoute.steps.length; i++) {
            var marker = new google.maps.Marker({
                position: myRoute.steps[i].start_point,
                map: map
            });
            attachInstructionText(marker, myRoute.steps[i].instructions);
            markerArray[i] = marker;
        }
    }

    function attachInstructionText(marker, text) {
        google.maps.event.addListener(marker, 'click', function () {               
            stepDisplay.setContent(text);
            stepDisplay.open(map, marker);
        });
    }



</script> 
4

1 に答える 1

0

以下のコードを参照してください。

self.adjustPosition = function () {    
   var lat = 0, lng = 0;

   if (self.nearbyPlaces().length == 0) {
      return false;
   }

   for (var i = 0; i < self.nearbyPlaces().length; i++) {
       lat += self.nearbyPlaces()[i].latitude;
       lng += self.nearbyPlaces()[i].longitude;
   }

   lat = lat / self.nearbyPlaces().length;
   lng = lng / self.nearbyPlaces().length;

   self.map.setCenter(new window.google.maps.LatLng(lat, lng));
};
于 2013-07-01T06:39:33.093 に答える