下载地址:https://www.pan38.com/dow/share.php?code=JCnzE 提取密码:4960
这些代码展示了如何模拟GPS位置变化和Android设备上的虚拟定位功能。第一个模块模拟了城市间的移动轨迹,第二个模块则提供了ADB命令来修改Android设备的位置。请注意实际使用时需要相应的权限和开发人员选项设置。
import random
import time
import json
import os
from datetime import datetime
from geopy.distance import geodesic
from faker import Faker
class LocationSimulator:
def init(self):
self.current_location = (39.9042, 116.4074) # 北京默认坐标
self.fake = Faker('zh_CN')
self.history = []
self.config = self.load_config()
def load_config(self):
try:
with open('config.json', 'r') as f:
return json.load(f)
except:
default_config = {
"speed_limit": 120, # km/h
"update_interval": 5, # seconds
"max_distance": 100 # km
}
with open('config.json', 'w') as f:
json.dump(default_config, f)
return default_config
def generate_random_location(self):
lat = self.current_location[0] + random.uniform(-0.5, 0.5)
lon = self.current_location[1] + random.uniform(-0.5, 0.5)
return (lat, lon)
def move_to_location(self, target_location):
distance = geodesic(self.current_location, target_location).km
steps = int(distance / (self.config['speed_limit'] / 3600 * self.config['update_interval']))
if steps == 0:
self.current_location = target_location
return
lat_step = (target_location[0] - self.current_location[0]) / steps
lon_step = (target_location[1] - self.current_location[1]) / steps
for i in range(steps):
self.current_location = (
self.current_location[0] + lat_step,
self.current_location[1] + lon_step
)
self.record_location()
time.sleep(self.config['update_interval'])
def record_location(self):
record = {
"timestamp": datetime.now().isoformat(),
"latitude": self.current_location[0],
"longitude": self.current_location[1],
"address": self.fake.address()
}
self.history.append(record)
return record
def save_history(self):
with open('location_history.json', 'w') as f:
json.dump(self.history, f, indent=2)
def get_city_location(self, city_name):
# 模拟城市坐标查询
cities = {
"北京": (39.9042, 116.4074),
"上海": (31.2304, 121.4737),
"广州": (23.1291, 113.2644),
"深圳": (22.5431, 114.0579),
"成都": (30.5728, 104.0668)
}
return cities.get(city_name, None)
def simulate_trip(self, destinations):
for dest in destinations:
target = self.get_city_location(dest)
if target:
print(f"开始前往{dest}...")
self.move_to_location(target)
print(f"已到达{dest}")
time.sleep(10)
self.save_history()
print("行程模拟完成")
if name == "main":
simulator = LocationSimulator()
trip_plan = ["北京", "上海", "广州", "深圳"]
simulator.simulate_trip(trip_plan)