Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

php - Distance between two addresses

I'm writing a booking web site in php and I would need a library or a remote service (similar to google maps api) that calculate the distance between 2 addresses.

Ideally I prefer road distance but I don't care too much about what kind of distance is.

Can you help me?

Thank you very much, every help will be welcome.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Google Maps API - Directions is a good place to start.

Sending off a request using the URL Pattern:

http://maps.google.com/maps/api/directions/xml?origin=[FROM_ADDRESS]&destination=[TO_ADDRESS]&sensor=false
// [FROM_ADDRESS] is a Google-Recognisable address for the Start
// [TO_ADDRESS] is a Google-Recognisable address for the End

Example - "How do I get to Carnegie Hall? (From Sony Music Entertainment)"

Start Address: 550 Madison Avenue, New York, NY, United States End Address: 881 7th Avenue, New York, NY, United States

The URL for the XML Directions from Google would be

http://maps.google.com/maps/api/directions/xml?origin=550+Madison+Avenue,+New+York,+NY,+United+States&destination=881+7th+Avenue,+New+York,+NY,+United+States&sensor=false

The result is:

<DirectionsResponse>
  <status>OK</status>
  <route>
    <summary>E 57th St</summary>
    <leg>
      <step>
        <travel_mode>DRIVING</travel_mode>
        <start_location>
          <lat>40.7612400</lat>
          <lng>-73.9731300</lng>
        </start_location>
        <end_location>
          <lat>40.7622900</lat>
          <lng>-73.9723600</lng>
        </end_location>
        <polyline>
          <points>wdxwF`{nbMqEyC</points>
          <levels>BB</levels>
        </polyline>
        <duration>
          <value>9</value>
          <text>1 min</text>
        </duration>
        <html_instructions>
          Head <b>northeast</b> on <b>Madison Ave</b> toward <b>E 56th St</b>
        </html_instructions>
        <distance>
          <value>133</value>
          <text>436 ft</text>
        </distance>
      </step>
      <step>
        <travel_mode>DRIVING</travel_mode>
        <start_location>
          <lat>40.7622900</lat>
          <lng>-73.9723600</lng>
        </start_location>
        <end_location>
          <lat>40.7655300</lat>
          <lng>-73.9800500</lng>
        </end_location>
        <polyline>
          <points>ikxwFfvnbMgS`o@</points>
          <levels>BB</levels>
        </polyline>
        <duration>
          <value>148</value>
          <text>2 mins</text>
        </duration>
        <html_instructions>
          Turn <b>left</b> at the 2nd cross street onto <b>E 57th St</b>
        </html_instructions>
        <distance>
          <value>741</value>
          <text>0.5 mi</text>
        </distance>
      </step>
      <step>
        <travel_mode>DRIVING</travel_mode>
        <start_location>
          <lat>40.7655300</lat>
          <lng>-73.9800500</lng>
        </start_location>
        <end_location>
          <lat>40.7651800</lat>
          <lng>-73.9803000</lng>
        </end_location>
        <polyline>
          <points>q_ywFhfpbMdAp@</points>
          <levels>BB</levels>
        </polyline>
        <duration>
          <value>39</value>
          <text>1 min</text>
        </duration>
        <html_instructions>
          Turn <b>left</b> at the 3rd cross street onto <b>7th Ave</b> <div style="font-size:0.9em">Destination will be on the left</div>
        </html_instructions>
        <distance>
          <value>45</value>
          <text>148 ft</text>
        </distance>
      </step>
      <duration>
        <value>196</value>
        <text>3 mins</text>
      </duration>
      <distance>
        <value>919</value>
        <text>0.6 mi</text>
      </distance>
      <start_location>
        <lat>40.7612400</lat>
        <lng>-73.9731300</lng>
      </start_location>
      <end_location>
        <lat>40.7651800</lat>
        <lng>-73.9803000</lng>
      </end_location>
      <start_address>550 Madison Ave, New York, NY 10022, USA</start_address>
      <end_address>881 7th Ave, New York, NY 10019, USA</end_address>
    </leg>
    <copyrights>Map data ?2010 Google, Sanborn</copyrights>
    <overview_polyline>
      <points>wdxwF`{nbMqEyCgS`o@dAp@</points>
      <levels>B@?B</levels>
    </overview_polyline>
  </route>
</DirectionsResponse>

So, the quickest route between those two points will have details of:

  • Duration in Seconds

    DirectionsResponse > route > leg > duration > value

  • Duration in Plain Text

    DirectionsResponse > route > leg > duration > text

  • Distance in Base Unit of Local Measurements (Feet or Metres)

    DirectionsResponse > route > leg > distance > value

  • Distance in Plain Text for Local Measurement (Miles or Kilometers)

    DirectionsResponse > route > leg > distance > text


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...