1

パス マップとポイント間の距離を郵便番号または都市名で表示したいのですが、場所に出入りするように、マップがそれらの間に生成され、それらの間の合計距離が計算されます。

4

2 に答える 2

1

これは 3 つの手順で行うことができます
1) 郵便番号または郵便番号で地図を生成し
ます map1.php を作成します

    <html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps JavaScript API v3 Example:Post City or Zip</title>
  </head>
<body >
<form action="map2.php" method="post" >
Enter 1st zip code / city <input type="text" name="input1" id="input1"><br/>
Enter 2nd zip code / city <input type="text" name="input2" id="input2">
<input type="submit">
</form>
</body>
</html>

2) map2.php を作成し、マップを生成する

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps JavaScript API v3 Example: Directions Simple</title>
<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" />

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
  var directionDisplay;
  var directionsService = new google.maps.DirectionsService();
  var map;

  function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer();
    var chicago = new google.maps.LatLng(41.850033, -87.6500523);
    var myOptions = {
      zoom:7,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      center: chicago
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    directionsDisplay.setMap(map);
  }

  function calcRoute() {
    var start = '<?php echo $_POST['input1']; ?>';
    var end = '<?php echo $_POST['input2']; ?>';


    var request = {
        origin:start, 
        destination:end,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
      }
    });
  }

</script>
</head>
<body onLoad="initialize(),calcRoute()">
  <?php
  // calculate laglong by address and zip code
  function getLatLong($address){
    if (!is_string($address))die("All Addresses must be passed as a string");
    $_url = sprintf('http://maps.google.com/maps?output=js&q=%s',rawurlencode($address));
    $_result = false;
    if($_result = file_get_contents($_url)) {
        if(strpos($_result,'errortips') > 1 || strpos($_result,'Did you mean:') !== false) return false;
        preg_match('!center:\s*{lat:\s*(-?\d+\.\d+),lng:\s*(-?\d+\.\d+)}!U', $_result, $_match);
        $_coords['lat'] = $_match[1];
        $_coords['long'] = $_match[2];
    }
    return $_coords;
}
  $to = getLatLong($_POST['input1']);echo "<br/>";print_r($to);
  $from = getLatLong($_POST['input2']);echo "<br/>";print_r($from);
echo distance($to['lat'], $to['long'], $from['lat'], $from['long'], "m") . " miles";

//  // calculate distance by lat long
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
  $theta = $lon1 - $lon2;
  $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
  $dist = acos($dist);
  $dist = rad2deg($dist);
  $miles = $dist * 60 * 1.1515;
  $unit = strtoupper($unit);

  if ($unit == "K") {
    return ($miles * 1.609344);
  } else if ($unit == "N") {
      return ($miles * 0.8684);
    } else {
        return $miles;
      }
}
?>


<div id="map_canvas" style="top:30px;"></div>
</body>
</html>

地図を生成する getLatLong() 関数 ラグロンを検索し、距離関数を検索 都市または郵便番号間の距離を検索

于 2012-07-10T14:38:47.560 に答える
0

まず、gmaps API を使用して簡単に見つけることができる郵便番号の地理位置情報を見つける必要があります。次に、「distanceTo」メソッドを使用して、ポイント (ジオポイントと郵便番号で生成されたジオポイント) 間の距離を見つけます。distanceTo メソッドは、距離をメートル単位で提供します。

この投稿をチェック

于 2012-07-10T08:11:29.263 に答える