以下内容转自 数析学院,只节选了部分,有需要的同学可以直接查看原文
1、建立公路旅游路线的列表
第一步是决定在公路旅行时,你想在哪里停。
确保您首先查看 Google Maps 上的所有位置,以便你具有正确的地址、城市、州等。如果用于查看位置的名字在 Google Maps 上无法起效,则它在这里也无法起效。
将所有路标添加到下面的列表中,确保它们的格式与下面的示例相同。
技术说明:由于Google Maps API的日常使用限制,您最多只能有70个路标。如果您想添加更多的路标,您将不得不为了增加的API限制值,向Google支付费用
- all_waypoints = ["USS Alabama, Battleship Parkway, Mobile, AL",
- "Grand Canyon National Park, Arizona",
- "Toltec Mounds, Scott, AR",
- "San Andreas Fault, San Benito County, CA",
- "Cable Car Museum, 94108, 1201 Mason St, San Francisco, CA 94108",
- "Pikes Peak, Colorado",
- "The Mark Twain House & Museum, Farmington Avenue, Hartford, CT",
- "New Castle Historic District, Delaware",
- "White House, Pennsylvania Avenue Northwest, Washington, DC",
- "Cape Canaveral, FL",
- "Okefenokee Swamp Park, Okefenokee Swamp Park Road, Waycross, GA",
- "Craters of the Moon National Monument & Preserve, Arco, ID",
- "Lincoln Home National Historic Site Visitor Center, 426 South 7th Street, Springfield, IL",
- "West Baden Springs Hotel, West Baden Avenue, West Baden Springs, IN",
- "Terrace Hill, Grand Avenue, Des Moines, IA",
- "C. W. Parker Carousel Museum, South Esplanade Street, Leavenworth, KS",
- "Mammoth Cave National Park, Mammoth Cave Pkwy, Mammoth Cave, KY",
- "French Quarter, New Orleans, LA",
- "Acadia National Park, Maine",
- "Maryland State House, 100 State Cir, Annapolis, MD 21401",
- "USS Constitution, Boston, MA",
- "Olympia Entertainment, Woodward Avenue, Detroit, MI",
- "Fort Snelling, Tower Avenue, Saint Paul, MN",
- "Vicksburg National Military Park, Clay Street, Vicksburg, MS",
- "Gateway Arch, Washington Avenue, St Louis, MO",
- "Glacier National Park, West Glacier, MT",
- "Ashfall Fossil Bed, Royal, NE",
- "Hoover Dam, NV",
- "Omni Mount Washington Resort, Mount Washington Hotel Road, Bretton Woods, NH",
- "Congress Hall, Congress Place, Cape May, NJ 08204",
- "Carlsbad Caverns National Park, Carlsbad, NM",
- "Statue of Liberty, Liberty Island, NYC, NY",
- "Wright Brothers National Memorial Visitor Center, Manteo, NC",
- "Fort Union Trading Post National Historic Site, Williston, North Dakota 1804, ND",
- "Spring Grove Cemetery, Spring Grove Avenue, Cincinnati, OH",
- "Chickasaw National Recreation Area, 1008 W 2nd St, Sulphur, OK 73086",
- "Columbia River Gorge National Scenic Area, Oregon",
- "Liberty Bell, 6th Street, Philadelphia, PA",
- "The Breakers, Ochre Point Avenue, Newport, RI",
- "Fort Sumter National Monument, Sullivan's Island, SC",
- "Mount Rushmore National Memorial, South Dakota 244, Keystone, SD",
- "Graceland, Elvis Presley Boulevard, Memphis, TN",
- "The Alamo, Alamo Plaza, San Antonio, TX",
- "Bryce Canyon National Park, Hwy 63, Bryce, UT",
- "Shelburne Farms, Harbor Road, Shelburne, VT",
- "Mount Vernon, Fairfax County, Virginia",
- "Hanford Site, Benton County, WA",
- "Lost World Caverns, Lewisburg, WV",
- "Taliesin, County Road C, Spring Green, Wisconsin",
- "Yellowstone National Park, WY 82190"]
接下来,你必须使用 Google Maps API 注册此脚本,以便他们知道谁在用数百个 Google Maps 路由请求冲击他们的服务器。
1) 用你的 Google 账户使用 Google Maps Distance Matrix API。 Google 说明 here.
2) 复制和粘贴它们生成的 API key 到下面的代码中。
- import googlemaps
- gmaps = googlemaps.Client(key="PASTE YOUR API KEY HERE")
现在,我们将查询 Google Maps API,寻找所有路标之间的最短路线。
这相当于在 Google 地图网站上进行 Google 地图路线查询,除了现在我们正在使用代码自动执行数百次查询。
如果您在此部分遇到错误,那么很有可能意味着在 Google Maps 上找不到你输入的路标。另一个可能的错误的原因是,如果在这些点之间不可能开车,例如,寻找夏威夷和佛罗里达之间的行车路线将会返回错误,直到我们发明飞行汽车。
2、收集所有路标之间最短路线的距离
- from itertools import combinations
- waypoint_distances = {}
- waypoint_durations = {}
- for (waypoint1, waypoint2) in combinations(all_waypoints, 2):
- try:
- route = gmaps.distance_matrix(origins=[waypoint1],
- destinations=[waypoint2],
- mode="driving", # "walking" 为步行路线,
- # "bicycling" 为骑行路线
- language="English",
- units="metric")
- # "distance" 单位为“米”
- distance = route["rows"][0]["elements"][0]["distance"]["value"]
- # "duration" 单位为“秒”
- duration = route["rows"][0]["elements"][0]["duration"]["value"]
- waypoint_distances[frozenset([waypoint1, waypoint2])] = distance
- waypoint_durations[frozenset([waypoint1, waypoint2])] = duration
-
- except Exception as e:
- print("Error with finding the route between %s and %s." % (waypoint1, waypoint2))
现在我们有所有路点之间的路线,让我们将它们保存到一个文本文件中,这样我们也不用再麻烦 Google 了。
- ith open("my-waypoints-dist-dur.tsv", "w") as out_file:
- out_file.write("\t".join(["waypoint1",
- "waypoint2",
- "distance_m",
- "duration_s"]))
-
- for (waypoint1, waypoint2) in waypoint_distances.keys():
- out_file.write("\n" +
- "\t".join([waypoint1,
- waypoint2,
- str(waypoint_distances[frozenset([waypoint1, waypoint2])]),
- str(waypoint_durations[frozenset([waypoint1, waypoint2])])]))
以上内容节选自 数析学院,原文内容较多,先搬运到这,后续有时间再补充,有需要的同学可以先直接到 数析学院 最新课程查看原文


雷达卡




京公网安备 11010802022788号







