我的地图v3距离计算给出了模糊和可能错误的结果

时间:2015-04-20 06:32:27

标签: javascript android html5 google-maps-api-3

我在android webview上使用网络语言工作android项目。我试图计算用户位置和其他位置之间的距离,但它不断给我大值和同一时间,所有位置搜索结果的距离值相同。我测试了其他应用程序的结果,但我自己似乎不同。其他所有事情都可以正常工作,除了所有地方的距离值都是相同的,不论距离是多少,并且总是与谷歌付费商店提供的其他类似应用的价值不同。我的代码如下所示。

    var map, places, infoWindow;
    var geocoder;
    var pos;

    function initialize() {
      geocoder = new google.maps.Geocoder();
      var myOptions = {
        zoom: 10,
        mapTypeControl: false,
        panControl: false,
        zoomControl: false,
        streetViewControl: false
      };

      map = new google.maps.Map(document.getElementById('map-canvas'), myOptions);

      // Try HTML5 geolocation
      if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
           pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
          map.setCenter(pos);
           }, function() {
          handleNoGeolocation(true);
        });
      } else {
        // Browser doesn't support Geolocation
        handleNoGeolocation(false);
      }


      infoWindow = new google.maps.InfoWindow({
          content: document.getElementById('info-content')
          });

      // Create the autocomplete object and associate it with the UI input control.
      // Restrict the search to the default country, and to place type "cities".
      places = new google.maps.places.PlacesService(map);
      google.maps.event.addListener(map, 'tilesloaded', function() {
      document.getElementById("loading").style.display = "none";
          });

     // Add a dom event listener to react when the user clicks the search button

     google.maps.event.addDomListener(document.getElementById('button'), 'click',
          onPlaceChanged);

    }

    function distance () {
        places.textSearch(search, function(results, status) {

        if (status == google.maps.places.PlacesServiceStatus.OK) {
            for (var i = 0; i < results.length; i++) {

             var service5 = new google.maps.DistanceMatrixService();
              service5.getDistanceMatrix(
        {
          origins: [pos],
          destinations: [results[i].geometry.location],
          travelMode: google.maps.TravelMode.DRIVING,
          unitSystem: google.maps.UnitSystem.METRIC,
          avoidHighways: false,
          avoidTolls: false
        }, callback);

        function callback(response, status) {
      if (status == google.maps.DistanceMatrixStatus.OK) {


         document.getElementById('iw-distance1').textContent  = response.rows[0].elements[0].distance.text;
         document.getElementById('iw-time').textContent  = response.rows[0].elements[0].duration.text; 
             }
           else {
           alert ("helo");
             } 
           } 
        }
      }
    } 
   } 
       function handleNoGeolocation(errorFlag) {
      if (errorFlag) {
        var content = 'Error: The Geolocation service failed.';
      } else {
        var content = 'Error: Doesn\'t support geolocation.';
      }

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

      map.setCenter(options.position);
    }

            <tr id="iw-website-row" class="iw_table_row">
              <td class="iw_attribute_name">distance:</td>
              <td id="iw-distance1"></td>
            </tr>
            <tr id="iw-website-row" class="iw_table_row">
              <td class="iw_attribute_name">time:</td>
              <td id="iw-time"></td>
            </tr>


         google.maps.event.addDomListener(window, 'load', initialize);

0 个答案:

没有答案